index was outside the bounds

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cm9kY2hhcg==?=

    #1

    index was outside the bounds

    hey all,
    i have several delimited strings all of which have different amount of
    elements is there a way to prevent the exception without a try catch block if
    i try to check an element that doesn't exist?

    thanks,
    rodchar
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: index was outside the bounds

    On Aug 5, 10:03 am, rodchar <rodc...@discus sions.microsoft .comwrote:
    hey all,
    i have several delimited strings all of which have different amount of
    elements is there a way to prevent the exception without a try catch block if
    i try to check an element that doesn't exist?
    >
    thanks,
    rodchar
    Hi,

    Of course, you can check if the length of the array is bigger than the
    index you are trying to reach

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: index was outside the bounds

      On Aug 5, 10:03 am, rodchar <rodc...@discus sions.microsoft .comwrote:
      hey all,
      i have several delimited strings all of which have different amount of
      elements is there a way to prevent the exception without a try catch block if
      i try to check an element that doesn't exist?
      >
      thanks,
      rodchar
      sorry, fast keys :)

      something like:
      if ( elements.Length >4 )
      fifthValue = elements[4];

      Comment

      • =?Utf-8?B?cmF1bGF2aQ==?=

        #4
        RE: index was outside the bounds

        tip:
        (this work for me)

        if you can pad your var with the delimiter char
        if * is the delimeter and max expected array count = 100

        then, you will always get 100 values (after split)

        string s = "a*b*c..."; //string var
        s += new string("*",100) ; //change 100 with the max number expected
        string [] ss = s.Split('*');




        "rodchar" wrote:
        hey all,
        i have several delimited strings all of which have different amount of
        elements is there a way to prevent the exception without a try catch block if
        i try to check an element that doesn't exist?
        >
        thanks,
        rodchar

        Comment

        • =?Utf-8?B?cm9kY2hhcg==?=

          #5
          Re: index was outside the bounds

          thanks everyone for the help,
          rod.

          "Ignacio Machin ( .NET/ C# MVP )" wrote:
          On Aug 5, 10:03 am, rodchar <rodc...@discus sions.microsoft .comwrote:
          hey all,
          i have several delimited strings all of which have different amount of
          elements is there a way to prevent the exception without a try catch block if
          i try to check an element that doesn't exist?

          thanks,
          rodchar
          >
          Hi,
          >
          Of course, you can check if the length of the array is bigger than the
          index you are trying to reach
          >

          Comment

          Working...