array element isexist ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zulander@gmail.com

    #1

    array element isexist ?

    is there a way to find out if the element exist ?

    dim myarray() as string
    dim mytxt as string

    mytxt ="Superman1,Sup erman2,Superman 3,Superman4"

    myarray=mytxt.s plit(",")

    debug.print(mya rray(4))

    theelemet myarray(4) doesn't exist how do i find out if it exist or not
    with out doing ubound or GetUpperBound
    thank you

  • tommaso.gastaldi@uniroma1.it

    #2
    Re: array element isexist ?

    Hi zulander (nice movie!)

    Indexes start from 0. ubound is the last index in the array (3 in your
    case):0,1,2,3
    (that's count -1)

    zulander@gmail. com ha scritto:
    is there a way to find out if the element exist ?
    >
    dim myarray() as string
    dim mytxt as string
    >
    mytxt ="Superman1,Sup erman2,Superman 3,Superman4"
    >
    myarray=mytxt.s plit(",")
    >
    debug.print(mya rray(4))
    >
    theelemet myarray(4) doesn't exist how do i find out if it exist or not
    with out doing ubound or GetUpperBound
    thank you

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: array element isexist ?

      <zulander@gmail .comschrieb:
      is there a way to find out if the element exist ?
      >
      dim myarray() as string
      You may want to use the array's 'IndexOf' method.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: array element isexist ?

        | theelemet myarray(4) doesn't exist how do i find out if it exist or not
        | with out doing ubound or GetUpperBound

        Without using ubound or GetUpperBound?? Have you tried Length?

        Generally I use GetUpperBound or Length to find the upper bound of an
        array...

        --
        Hope this helps
        Jay B. Harlow [MVP - Outlook]
        ..NET Application Architect, Enthusiast, & Evangelist
        T.S. Bradley - http://www.tsbradley.net


        <zulander@gmail .comwrote in message
        news:1153342671 .471587.267520@ b28g2000cwb.goo glegroups.com.. .
        | is there a way to find out if the element exist ?
        |
        | dim myarray() as string
        | dim mytxt as string
        |
        | mytxt ="Superman1,Sup erman2,Superman 3,Superman4"
        |
        | myarray=mytxt.s plit(",")
        |
        | debug.print(mya rray(4))
        |
        | theelemet myarray(4) doesn't exist how do i find out if it exist or not
        | with out doing ubound or GetUpperBound
        | thank you
        |


        Comment

        • GhostInAK

          #5
          Re: array element isexist ?

          Hello zulander@gmail. com,

          Most array's in .NET are 0-based. Hoever it is possible to create arrays
          which start at a different index. So GetUpperBound() and GetLowerBound()
          are the best methods for determining your bounds. Any other method is just
          guessing.

          -Boo
          is there a way to find out if the element exist ?
          >
          dim myarray() as string
          dim mytxt as string
          mytxt ="Superman1,Sup erman2,Superman 3,Superman4"
          >
          myarray=mytxt.s plit(",")
          >
          debug.print(mya rray(4))
          >
          theelemet myarray(4) doesn't exist how do i find out if it exist or
          not
          with out doing ubound or GetUpperBound
          thank you

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: array element isexist ?

            Zulander,

            The way I would use it.

            \\\
            Dim myarray() As String
            Dim mytxt As String
            mytxt = "Superman1,Supe rman2,Superman3 ,Superman4"
            myarray = mytxt.Split("," c)
            If myarray.Length < 4 Then
            Debug.Print(mya rray(4))
            End If
            ///

            I hope this helps,

            Cor

            <zulander@gmail .comschreef in bericht
            news:1153342671 .471587.267520@ b28g2000cwb.goo glegroups.com.. .
            is there a way to find out if the element exist ?
            >
            dim myarray() as string
            dim mytxt as string
            >
            mytxt ="Superman1,Sup erman2,Superman 3,Superman4"
            >
            myarray=mytxt.s plit(",")
            >
            debug.print(mya rray(4))
            >
            theelemet myarray(4) doesn't exist how do i find out if it exist or not
            with out doing ubound or GetUpperBound
            thank you
            >

            Comment

            • tommaso.gastaldi@uniroma1.it

              #7
              Re: array element isexist ?

              hi GhostInAK

              can you make an example of what you mean by starting
              at a different index?

              -tom

              PS. btw array elements are not sexist :)

              GhostInAK ha scritto:
              Hello zulander@gmail. com,
              >
              Most array's in .NET are 0-based. Hoever it is possible to create arrays
              which start at a different index.
              >
              -Boo
              >
              is there a way to find out if the element exist ?

              dim myarray() as string
              dim mytxt as string
              mytxt ="Superman1,Sup erman2,Superman 3,Superman4"

              myarray=mytxt.s plit(",")

              debug.print(mya rray(4))

              theelemet myarray(4) doesn't exist how do i find out if it exist or
              not
              with out doing ubound or GetUpperBound
              thank you

              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: array element isexist ?

                Tommaso,
                Try something like:

                Dim list As Array
                Dim lengths() As Integer = {10, 10}
                Dim lowerBounds() As Integer = {1, 1}
                list = Array.CreateIns tance(GetType(I nteger), lengths, lowerBounds)
                Dim values(,) As Integer = DirectCast(list , Integer(,))

                For x As Integer = values.GetLower Bound(0) To
                values.GetUpper Bound(0)
                For y As Integer = values.GetLower Bound(1) To
                values.GetUpper Bound(1)
                values(x, y) = x * y
                Next
                Next

                ' this causes an exception!
                Dim value As Integer = values(0, 0)

                What I think misleads people is that most .NET languages (such as C# & VB)
                only support creating 0 based arrays in their "native" syntax. If you use
                the runtime itself, you can create an array with any lower bound...

                --
                Hope this helps
                Jay B. Harlow [MVP - Outlook]
                ..NET Application Architect, Enthusiast, & Evangelist
                T.S. Bradley - http://www.tsbradley.net


                <tommaso.gastal di@uniroma1.itw rote in message
                news:1153380365 .967421.23170@s 13g2000cwa.goog legroups.com...
                | hi GhostInAK
                |
                | can you make an example of what you mean by starting
                | at a different index?
                |
                | -tom
                |
                | PS. btw array elements are not sexist :)
                |
                | GhostInAK ha scritto:
                |
                | Hello zulander@gmail. com,
                | >
                | Most array's in .NET are 0-based. Hoever it is possible to create
                arrays
                | which start at a different index.
                | >
                | -Boo
                | >
                | is there a way to find out if the element exist ?
                |
                | dim myarray() as string
                | dim mytxt as string
                | mytxt ="Superman1,Sup erman2,Superman 3,Superman4"
                |
                | myarray=mytxt.s plit(",")
                |
                | debug.print(mya rray(4))
                |
                | theelemet myarray(4) doesn't exist how do i find out if it exist or
                | not
                | with out doing ubound or GetUpperBound
                | thank you
                |


                Comment

                • tommaso.gastaldi@uniroma1.it

                  #9
                  Re: array element isexist ?

                  Thanks Jay. It helps a lot !

                  I did not know that, and it's good to learn it can be done this way.

                  I also think, however, they while a rich language such as vb.net allows
                  to do many things, many of them should be accurately avoided in order
                  not to have troubles.

                  Actually I regard the path of learning a language a kind of selective
                  process where one, by learning from experience, learns to distinguish
                  what lead to good and maintenable programs from what instead
                  calls only for troubles. Sometimes this process can be very painful
                  as we may be spending days just for 1 bug.

                  During the years I have changed a lot my way of programming
                  and I am continuosly getting rid of (what prove to be) bad habits.

                  I see it like a kind of puzzle, ... at the end there aren't actually
                  that many ways to put the parts together :)

                  -tom

                  Jay B. Harlow [MVP - Outlook] ha scritto:
                  Tommaso,
                  Try something like:
                  >
                  Dim list As Array
                  Dim lengths() As Integer = {10, 10}
                  Dim lowerBounds() As Integer = {1, 1}
                  list = Array.CreateIns tance(GetType(I nteger), lengths, lowerBounds)
                  Dim values(,) As Integer = DirectCast(list , Integer(,))
                  >
                  For x As Integer = values.GetLower Bound(0) To
                  values.GetUpper Bound(0)
                  For y As Integer = values.GetLower Bound(1) To
                  values.GetUpper Bound(1)
                  values(x, y) = x * y
                  Next
                  Next
                  >
                  ' this causes an exception!
                  Dim value As Integer = values(0, 0)
                  >
                  What I think misleads people is that most .NET languages (such as C# & VB)
                  only support creating 0 based arrays in their "native" syntax. If you use
                  the runtime itself, you can create an array with any lower bound...
                  >
                  --
                  Hope this helps
                  Jay B. Harlow [MVP - Outlook]
                  .NET Application Architect, Enthusiast, & Evangelist
                  T.S. Bradley - http://www.tsbradley.net
                  >
                  >
                  <tommaso.gastal di@uniroma1.itw rote in message
                  news:1153380365 .967421.23170@s 13g2000cwa.goog legroups.com...
                  | hi GhostInAK
                  |
                  | can you make an example of what you mean by starting
                  | at a different index?
                  |
                  | -tom
                  |
                  | PS. btw array elements are not sexist :)
                  |
                  | GhostInAK ha scritto:
                  |
                  | Hello zulander@gmail. com,
                  | >
                  | Most array's in .NET are 0-based. Hoever it is possible to create
                  arrays
                  | which start at a different index.
                  | >
                  | -Boo
                  | >
                  | is there a way to find out if the element exist ?
                  |
                  | dim myarray() as string
                  | dim mytxt as string
                  | mytxt ="Superman1,Sup erman2,Superman 3,Superman4"
                  |
                  | myarray=mytxt.s plit(",")
                  |
                  | debug.print(mya rray(4))
                  |
                  | theelemet myarray(4) doesn't exist how do i find out if it exist or
                  | not
                  | with out doing ubound or GetUpperBound
                  | thank you
                  |

                  Comment

                  Working...