Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. vb.net 2010 code check if values exist

vb.net 2010 code check if values exist

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcof
    wrote on last edited by
    #1

    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?

    L Richard DeemingR 2 Replies Last reply
    0
    • D dcof

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      String.IndexOf Method (String) (System)[^]

      Speed of sound - 1100 ft/sec Speed of light - 186,000 mi/sec Speed of stupid - instantaneous.

      1 Reply Last reply
      0
      • D dcof

        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?

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        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 calling Substring. Also, IndexOf already returns an Integer. There's no need to call CInt.

        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 If

        Your 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

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups