[RESOLVED] Passing ArrayLists between subroutines
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Passing ArrayLists between subroutines

  1. #1
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    [RESOLVED] Passing ArrayLists between subroutines

    I am new to the use of ArrayLists and am trying to convert an algorithm to use them in order to save memory. However, I am struggling to achieve error-free code while using "Strict On".

    Here is a basic version of the code I am attempting to use.

    Code:
    Option Explicit On
    Option Strict On
    
    Module Program
    	Sub Main()
    		
    		Dim P(1) As Structs.Point
    		P(0).X = 0
    		P(0).Y = 0
    		P(1).X = Convert.ToDecimal(1.2345)
    		P(1).Y = Convert.ToDecimal(1.9876)
    		
    		Dim AL As New ArrayList()
    		AL.Add(100)
    		
    		Dim AL1 As New ArrayList()
    		AL1.Add(P(0))
    		AL1.Add(P(1))
    		
    		Dim AL2 As New ArrayList()
    		AL2.Add("A")
    		AL2.Add("B")
    		AL2.Add("C")
    		AL2.Add("D")
    		
    		Dim AL3 As New ArrayList()
    		AL3.Add(True)
    		AL3.Add(False)
    		
    		AL.Add(AL1)
    		AL.Add(AL2)
    		AL.Add(AL3)
    		
    		Call Tester(AL)
    		
    		Console.Write("Press any key to continue . . . ")
    		Console.ReadKey(True)
    	End Sub
    
    	Sub Tester(ByRef AL As ArrayList)
    
    		Dim a As New ArrayList
    		a = AL(2)
    		Console.WriteLine(a(2))
    		If (AL(1)(0)).X < (AL(1)(1)).X Then
    			Console.WriteLine(Convert.ToString((AL(1)(1)).X))
    		End If
    
    	End Sub
    
    End Module
    
    Class Structs
    	Public Structure Point
    		Public X As Decimal
    		Public Y As Decimal
    	End Structure
    End Class
    It works when I do not use the Strict option. How do I improve the code to compile error-free?
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  2. #2
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: Passing ArrayLists between subroutines

    I have been able to set a new ArrayList equal to one of the ArrayLists that form part of another ArrayList.

    Code:
    Dim a As New ArrayList
    a = DirectCast(AL(2), ArrayList)
    However, I still get a late binding issue. Even if I set the new ArrayList equal to the ArrayList in AL that consists of the Points structure, I seem unable to escape the late binding issue.

    Code:
    	Sub Tester(ByRef AL As ArrayList)
    
    		Dim a As New ArrayList
    		a = DirectCast(AL(1), ArrayList)
    		Console.WriteLine(a(1))
    		If a(0).X < a(1).X Then
    			Console.WriteLine(Convert.ToString(a(1).X))
    		End If
    
    	End Sub
    Any ideas?
    Last edited by FrankZA; June 3rd, 2009 at 04:39 AM.
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  3. #3
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: Passing ArrayLists between subroutines

    I managed to get it to work. All it takes is the liberal application of DirectCast.

    Code:
    	Sub Tester(ByRef AL As ArrayList)
    
    		Dim a As New ArrayList
    		a = DirectCast(AL(1), ArrayList)
    		Console.WriteLine(Convert.ToString(DirectCast(a.Item(1), Structs.Point).X))
    		If DirectCast(a.Item(0), Structs.Point).X < DirectCast(a.Item(1), Structs.Point).X Then
    			Console.WriteLine(Convert.ToString(DirectCast(a.Item(1), Structs.Point).X))
    		End If
    		
    	End Sub
    Is there any way of making this neater?
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: [RESOLVED] Passing ArrayLists between subroutines

    Does it work?
    Code:
    a = DirectCast(AL(1), ArrayList)
    looks like you're getting only the SECOND element of AL()
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Mar 2008
    Location
    Stellenbosch, South Africa
    Posts
    18

    Re: [RESOLVED] Passing ArrayLists between subroutines

    In this example it was my aim to get only the second element in the ArrayList AL: the ArrayList of Structs.Point objects. I cannot think of a neater way of extracting an ArrayList out of another ArrayList.

    The bad thing is that creating the new ArrayList increases my memory use, but it is probably still better than making use of static 2D arrays.
    Last edited by FrankZA; June 4th, 2009 at 01:58 AM.
    SharpDevelop 3.1
    .NET Framework 3.5
    Windows XP (SP3)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center