vb.net 2010 code check if values exist
-
In a vb.net 2010 application, I have the following code that works sometimes:
Dim txtOrigAddress As String = _attendanceLetterOrig.Substring(CInt(_attendanceLetterOrig.IndexOf("ADDR_BEG")), _attendanceLetterOrig.IndexOf("ADDR_END") - _attendanceLetterOrig.IndexOf("ADDR_BEG"))
Dim txtOrigAddress As String = _attendanceLetterOrig.Substring(CInt(_attendanceLetterOrig.IndexOf("ADDR_NEXT")),4)The problem is the folllowing values do not exist:
_attendanceLetterOrig.IndexOf("ADDR_BEG")
_attendanceLetterOrig.IndexOf("ADDR_END")
_attendanceLetterOrig.Substring(CInt"ADDR_NEXT")What kind of an edit can I use to check if the 3 values listed above actually exist? I want to prevent application errors. Would you show me the code to see if the values really exist?
-
In a vb.net 2010 application, I have the following code that works sometimes:
Dim txtOrigAddress As String = _attendanceLetterOrig.Substring(CInt(_attendanceLetterOrig.IndexOf("ADDR_BEG")), _attendanceLetterOrig.IndexOf("ADDR_END") - _attendanceLetterOrig.IndexOf("ADDR_BEG"))
Dim txtOrigAddress As String = _attendanceLetterOrig.Substring(CInt(_attendanceLetterOrig.IndexOf("ADDR_NEXT")),4)The problem is the folllowing values do not exist:
_attendanceLetterOrig.IndexOf("ADDR_BEG")
_attendanceLetterOrig.IndexOf("ADDR_END")
_attendanceLetterOrig.Substring(CInt"ADDR_NEXT")What kind of an edit can I use to check if the 3 values listed above actually exist? I want to prevent application errors. Would you show me the code to see if the values really exist?
-
In a vb.net 2010 application, I have the following code that works sometimes:
Dim txtOrigAddress As String = _attendanceLetterOrig.Substring(CInt(_attendanceLetterOrig.IndexOf("ADDR_BEG")), _attendanceLetterOrig.IndexOf("ADDR_END") - _attendanceLetterOrig.IndexOf("ADDR_BEG"))
Dim txtOrigAddress As String = _attendanceLetterOrig.Substring(CInt(_attendanceLetterOrig.IndexOf("ADDR_NEXT")),4)The problem is the folllowing values do not exist:
_attendanceLetterOrig.IndexOf("ADDR_BEG")
_attendanceLetterOrig.IndexOf("ADDR_END")
_attendanceLetterOrig.Substring(CInt"ADDR_NEXT")What kind of an edit can I use to check if the 3 values listed above actually exist? I want to prevent application errors. Would you show me the code to see if the values really exist?
If the substring isn't found,
IndexOf
returns-1
. You'll need to find and store each index first, then check that they're not-1
before callingSubstring
. Also,IndexOf
already returns anInteger
. There's no need to callCInt
.Dim startIndex As Integer = _attendanceLetterOrig.IndexOf("ADDR_BEG")
Dim endIndex As Integer = _attendanceLetterOrig.IndexOf("ADDR_END", startIndex)
If startIndex <> -1 AndAlso endIndex <> -1 Then
Dim txtOrigAddress As String = _attendanceLetterOrig.Substring(startIndex, endIndex - startIndex)
End IfYour second example makes no sense. The
IndexOf
method returns the index of the start of the matched substring, if found. So_attendanceLetterOrig.IndexOf("ADDR_NEXT")
returns the index of"A"
, and_attendanceLetterOrig.Substring(..., 4)
will always return"ADDR"
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer