VB and ArrayLists

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crokett
    The Full Monte
    • Jan 2003
    • 10627
    • Mebane, NC, USA.
    • Ryobi BT3000

    #1

    VB and ArrayLists

    Long story short I am trying to use an ArrayList instead of an array to try and avoid some ReDim statements with the Preserve option on. However, once I put something in the array list I can't get it back out.

    I have this code:


    Code:
    [SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] VSANList [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ArrayList
    [/SIZE] 
    [SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] VSAN
    [/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] vsanName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
    [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] vsanNum [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
    [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] domainID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff].....[/COLOR][/SIZE]
    [SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
    [SIZE=2][COLOR=#0000ff]End Class
    [/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2]
    [/SIZE]
    Now I can do this:
    Code:
    [SIZE=2]VSANList(0) = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] VSAN[/SIZE]
    [SIZE=2][/SIZE]


    But when I try to do this:
    Code:
    [SIZE=2]VSANList(0).vsanName = "tester"[/SIZE]
    [SIZE=2][/SIZE]


    I get a complier error that 'option strict disallows late binding'. Microsoft's answer is to turn strict off. I do not want to - I have it on for a reason.

    This:
    Code:
    [SIZE=2]Dim aVSAN As New VSAN[/SIZE]
    [SIZE=2]aVSAN = VSANList.Item(0)[/SIZE]
    [SIZE=2][/SIZE]


    Throws a compiler error that can't convert from System.Object to VSAN.

    So what to do?
    David

    The chief cause of failure in this life is giving up what you want most for what you want at the moment.
  • Hoakie
    Established Member
    • Feb 2007
    • 382
    • Iowa
    • Craftsman 21829

    #2
    The problem is that you can put ANY datatype into the array list and it stores it as an Object. This allows you the ability to store a list of anything together in one list a string, an integer, a dataset. IMO not very practical. The down side is when retrieving the records, they are all stored as the base object type so they need to be converted into what datatype you want on the way out.

    Basically you have two options

    1) Convert the objects as they come out --
    dim name as string = ctype(VSANList.item(0), string)

    or

    2) In VS2005 you have the option to use Generic Lists to strongly type your List. The generic list behaves like a plain arraylist, however you can define the datatype the list can hold. Since it can only hold one type, you don't have to convert on the way back out as such

    Dim VSANList as System.Collections.Generic.List(Of VSAN)
    VSANList.Add (New VSAN)

    dim name as string = VSANList.Item(0).vsamName


    I think the second option is most desirable for your application
    Last edited by Hoakie; 10-12-2007, 04:28 PM.
    John
    To invent, you need a good imagination and a pile of junk. ~ Edison

    Comment

    • crokett
      The Full Monte
      • Jan 2003
      • 10627
      • Mebane, NC, USA.
      • Ryobi BT3000

      #3
      Thanks John. That is the answer I needed. I never would have taken this thing on if I knew it was going to be this complicated.

      Makes for an interesting function declaration tho:

      PublicFunction setVSANInfo(ByVal swType AsString, ByVal tempVSAN As System.Collections.Generic.List(Of VSAN)) As System.Collections.Generic.List(Of VSAN)


      Last edited by crokett; 10-12-2007, 06:44 PM.
      David

      The chief cause of failure in this life is giving up what you want most for what you want at the moment.

      Comment

      • Hoakie
        Established Member
        • Feb 2007
        • 382
        • Iowa
        • Craftsman 21829

        #4
        You can simplifiy it if you put this at the top of your code, before the class declaration.

        Imports System.Collections.Generic

        "Imports" basically it allows you to bypass the full namespace declaration on in that document. Doing this would simplify your function to

        Public Function setVSANInfo(ByVal swType As String, ByVal tempVSAN As List(Of VSAN)) As List(Of VSAN)


        However, I am curious as to what happens in the function. Passing in a temp list and returning another?
        John
        To invent, you need a good imagination and a pile of junk. ~ Edison

        Comment

        • crokett
          The Full Monte
          • Jan 2003
          • 10627
          • Mebane, NC, USA.
          • Ryobi BT3000

          #5
          Originally posted by Hoakie
          However, I am curious as to what happens in the function. Passing in a temp list and returning another?
          The function steps through a file and fills the list that is passed in then returns it. The result is stored in a VSAN list which is an attribute of a fabric class. Although now that I think about it if I passed it ByRef instead of ByVal would it not fill my actual VSAN list?
          David

          The chief cause of failure in this life is giving up what you want most for what you want at the moment.

          Comment

          • Alex Franke
            Veteran Member
            • Feb 2007
            • 2641
            • Chapel Hill, NC
            • Ryobi BT3100

            #6
            Originally posted by crokett
            Although now that I think about it if I passed it ByRef instead of ByVal would it not fill my actual VSAN list?
            Yes. In fact it will work either way. When you pass ByVal, you're actually passing the reference by value. You're not creating a copy of it and passing the copy. This is probably what you want:

            Public Sub setVSANInfo(swType As String, tempVSAN As List(Of VSAN))

            Don't worry about the ByVals and ByRefs.

            ...or just let the function create and return the list.

            Public Function setVSANInfo(swType As String) As List(Of VSAN))

            EDIT: Let me elaborate a bit, because this can be confusing...
            A List (or most other objects) is called a reference type and it's allocated and stored on the heap, while a memory reference to it is managed on the stack. So for any reference type you're passing around among functions and subs you're actually just passing around a memory location. You can choose to pass that memory location ByVal or ByRef. If you pass it ByVal, you're creating another variable on the stack to hold a copy of that memory location. (If you change it -- e.g. set it to null/Nothing -- the original will not be modified, just the copy will. If you pass it ByRef, then you're passing the original reference to the memory location of your object to the function or sub. If you set it to null/Nothing, then you're setting the original object to null/nothing and freeing the memory.

            So in your case, regardless of whether you're passing it ByVal or ByRef, you're still working with the same object in memory.
            Last edited by Alex Franke; 10-12-2007, 09:46 PM. Reason: Changed "Function" to "Method" in code. Whoops!
            online at http://www.theFrankes.com
            while ( !( succeed = try() ) ) ;
            "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

            Comment

            • Hoakie
              Established Member
              • Feb 2007
              • 382
              • Iowa
              • Craftsman 21829

              #7
              Ahhh, I see. I personally am not a fan of passing in paramters used as variables in the calling routine.

              If I were doing this, I'd just create a new temp list in your function...fill it... and return it. Then in the calling method, you can either use it as is or if the list you are passing in now already has data in it you can use the VSAN.

              Dim MyStaticList As New Generic.List(Of VSAN)

              Private Sub DoSomething()

              Dim myType As String = "BT3Central Rocks"


              'Option 1
              MyStaticList = setVSANInfo(myType)

              'Option 2
              Dim tmpList As List(Of VSAN) = setVSANInfo(myType)
              MyStaticList.AddRange(tmpList)

              End Sub

              Public Function setVSANInfo(ByVal swType As String) As List(Of VSAN)
              Dim tmpVSANList As New List(Of VSAN)
              'Do something to Fill the list Here
              Return tmpVSANList
              End Function
              Last edited by Hoakie; 10-12-2007, 09:51 PM.
              John
              To invent, you need a good imagination and a pile of junk. ~ Edison

              Comment

              • Alex Franke
                Veteran Member
                • Feb 2007
                • 2641
                • Chapel Hill, NC
                • Ryobi BT3100

                #8
                BTW, Hoakie is right on target about using "Imports System.Collections.Generic" -- it makes things a *lot* easier to read!!!
                online at http://www.theFrankes.com
                while ( !( succeed = try() ) ) ;
                "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

                Comment

                • Hoakie
                  Established Member
                  • Feb 2007
                  • 382
                  • Iowa
                  • Craftsman 21829

                  #9
                  and +1 to Alex
                  John
                  To invent, you need a good imagination and a pile of junk. ~ Edison

                  Comment

                  • Hoakie
                    Established Member
                    • Feb 2007
                    • 382
                    • Iowa
                    • Craftsman 21829

                    #10
                    I can't wait until we get to the part about Non-deterministic finalization
                    John
                    To invent, you need a good imagination and a pile of junk. ~ Edison

                    Comment

                    • Alex Franke
                      Veteran Member
                      • Feb 2007
                      • 2641
                      • Chapel Hill, NC
                      • Ryobi BT3100

                      #11
                      Originally posted by Hoakie
                      I can't wait until we get to the part about Non-deterministic finalization
                      Let's bring him in easy, Hoakie -- we don't want him to swear off programming forever.

                      Hey, not to get too far off topic, but did you do that right brain / left brain test? I don't think my brain thinks I'm very logical about the way I think.
                      online at http://www.theFrankes.com
                      while ( !( succeed = try() ) ) ;
                      "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

                      Comment

                      • crokett
                        The Full Monte
                        • Jan 2003
                        • 10627
                        • Mebane, NC, USA.
                        • Ryobi BT3000

                        #12
                        I have a Fabric Object that has as an attribute a list of VSANs. I parse multiple switch files and they may or may not all have the same VSANs, but the fabric knows about all of them.

                        So Hoakie what you are saying is instead of passing the master list as it were, just implement the function to always return a new list for every switch, then simply add that to the master.

                        Dayum. That is so simple. Why the heck didn't I think of that? I have to do some checking to make sure I don't add the same VSAN to the master more than once but that is a lot easier than the garbled logic I have now - which still doesn't work quite right.
                        David

                        The chief cause of failure in this life is giving up what you want most for what you want at the moment.

                        Comment

                        • Alex Franke
                          Veteran Member
                          • Feb 2007
                          • 2641
                          • Chapel Hill, NC
                          • Ryobi BT3100

                          #13
                          Originally posted by crokett
                          So Hoakie what you are saying is instead of passing the master list as it were, just implement the function to always return a new list for every switch, then simply add that to the master.
                          I know I'm not Hoakie, but here's some food for thought:

                          If a switch owns the VSANs (a VSAN can belong to no more than one switch) then the switch should create and manage its list of VSANs. If VSANs can be shared among switches -- e.g. a many-many relationship between switches and VSANs -- then you probably do want to master list of VSANs, and a master list of switches in the Fabric and let them hold references to each other. You could essentially filter the VSANs by the switches that are associated with them.

                          Also, remember that if you're using references, you can use List.Contains( [item] ) to see if something's in the list.

                          Add something to the list only if it's not already there:
                          if ( !myList.Contains( myItem ) ) myList.Add( myItem );
                          online at http://www.theFrankes.com
                          while ( !( succeed = try() ) ) ;
                          "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

                          Comment

                          • crokett
                            The Full Monte
                            • Jan 2003
                            • 10627
                            • Mebane, NC, USA.
                            • Ryobi BT3000

                            #14
                            Originally posted by Alex Franke
                            Also, remember that if you're using references, you can use List.Contains( [item] ) to see if something's in the list.

                            Add something to the list only if it's not already there:
                            if ( !myList.Contains( myItem ) ) myList.Add( myItem );
                            The VSAN is a fabric construct, it just happens to reside on the switches. Once I get some basic VSAN stuff figured out I will start adding a list of switches to a VSAN.

                            Contains() might be the answer I need to a problem I am having. If this doesn't get it you and Hoakie are gonna get a PM later.
                            David

                            The chief cause of failure in this life is giving up what you want most for what you want at the moment.

                            Comment

                            • Hoakie
                              Established Member
                              • Feb 2007
                              • 382
                              • Iowa
                              • Craftsman 21829

                              #15
                              Just be aware that if you create a new list in the function and return it, you can not use the .Contains() method because you will have two Different objects in memory that have identical values. In this case, you would have compare the values that make the objects identical. If you have or can make up a unique key for VSAN you could use a Dictionary object instead of List object. Then you can easily compare the Key value instead of the reference to an object.
                              John
                              To invent, you need a good imagination and a pile of junk. ~ Edison

                              Comment

                              Working...