UBound behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Honek

    #1

    UBound behaviour

    I have the following code below.

    The thing is, even if txtCCEmailAddre ss.Text is empty (no text), CCRecipeant
    will return a Ubound() higher than 0 and still go in the loop.

    If there's nothing to split how can CCRecipeant be a positive UBound value?
    'Get the number of specified CC recipeants

    CCRecipeant = txtCCEmailAddre ss.Text.Split(" ;")

    'Now we need to add each one

    For intCounter = 0 To CCRecipeant.Get UpperBound(0)

    If CCRecipeant.Get UpperBound(0) >= 0 Then

    ..........some code here

    End If

    Next



    Thanks,

    Adam


  • Fred

    #2
    Re: UBound behaviour

    Hi,

    [color=blue]
    >From .NET documentation.[/color]

    Function Split(
    ByVal Expression As String,
    Optional ByVal Delimiter As String = " ",
    Optional ByVal Limit As Integer = -1,
    Optional ByVal Compare As CompareMethod = CompareMethod.B inary
    ) As String()


    If Expression is a zero-length string (""), the Split function returns
    an array of length one, containing an empty string.

    Can you test txtCCEmailAddre ss.Text for length first?

    Fred

    Comment

    • Göran Andersson

      #3
      Re: UBound behaviour

      Adam Honek wrote:[color=blue]
      > I have the following code below.
      >
      > The thing is, even if txtCCEmailAddre ss.Text is empty (no text), CCRecipeant
      > will return a Ubound() higher than 0 and still go in the loop.[/color]

      No, it wont. If the string is empty, the array will contain one item, so
      UBound will return the value zero. That will make the loop iterate once.
      [color=blue]
      > If there's nothing to split how can CCRecipeant be a positive UBound value?
      > 'Get the number of specified CC recipeants[/color]

      There is always something to split. A string that doesn't contain a
      separator contains one item. It doesn't matter if the string is empty or
      not.

      "asdf;asdf" -> two items: "asdf", "asdf"
      "asdf;a" -> two items: "asdf", "a"
      "asdf;" -> two items: "asdf", ""

      "asdf" -> one item: "asdf"
      "a" -> one item: "a"
      "" -> one item: ""
      [color=blue]
      > CCRecipeant = txtCCEmailAddre ss.Text.Split(" ;")
      >
      > 'Now we need to add each one
      >
      > For intCounter = 0 To CCRecipeant.Get UpperBound(0)
      >
      > If CCRecipeant.Get UpperBound(0) >= 0 Then
      >
      > .........some code here
      >
      > End If
      >
      > Next[/color]

      Comment

      Working...