Results 1 to 10 of 10

Thread: ArrayList and structures

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    ArrayList and structures

    Hi all,

    ArrayList is a generic structure.
    Let's say I have my own structure (contained of Boolean+string+int). I want to set a dynamic array based on my own structure. How....?

    Thanks!

  2. #2
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    Re: ArrayList and structures

    Quote Originally Posted by emc2 View Post
    Hi all,

    ArrayList is a generic structure.
    Let's say I have my own structure (contained of Boolean+string+int). I want to set a dynamic array based on my own structure. How....?

    Thanks!
    This is a example about dynamic arrray :
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim scores() As Integer
    
            ReDim scores(1)
            scores(0) = 100
            scores(1) = 200
    
            For i = 0 To scores.Length - 1
                MsgBox(scores(i))
            Next
    
            ReDim Preserve scores(2)
    
            scores(2) = 300
    
            For i = 0 To scores.Length - 1
                MsgBox(scores(i))
            Next
        End Sub
    End Class
    If you want to keep the existing items in the Array , you can use the keyword Preserve .

    ReDim Preserve scores(2) :

    In this case the Array dynamically allocate one more String value and keep the existing values.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: ArrayList and structures

    When you say "generic" don't mean it in the .NET sense, because an ArrayList is not a generic class, which is the problem. From .NET 2.0 onwards you should not use an ArrayList at all but rather use the List(Of T), which is a generic class. If you have your own type named SomeType, you would create a List(Of SomeType). The collection will then only accept SomeType objects and it will return its items as SomeType values rather than Object references.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: ArrayList and structures

    Thanks. That's what I meant.
    Can you write the releavnt syntax?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: ArrayList and structures

    Thanks, but I woudln't prefer the "redim" statement. It's a VB6 .

    Quote Originally Posted by manhit45 View Post
    This is a example about dynamic arrray :
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            Dim scores() As Integer
    
            ReDim scores(1)
            scores(0) = 100
            scores(1) = 200
    
            For i = 0 To scores.Length - 1
                MsgBox(scores(i))
            Next
    
            ReDim Preserve scores(2)
    
            scores(2) = 300
    
            For i = 0 To scores.Length - 1
                MsgBox(scores(i))
            Next
        End Sub
    End Class
    If you want to keep the existing items in the Array , you can use the keyword Preserve .

    ReDim Preserve scores(2) :

    In this case the Array dynamically allocate one more String value and keep the existing values.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ArrayList and structures

    You dont need to do all that though, as JMC has explained you can just use the generic List(Of T) like so:
    vb Code:
    1. Dim MyCustomTypeList As New List(Of MyCustomStructure)
    2.  
    3. Dim structureinstance As New MyCustomStructure
    4. structureinstance.SomeStringProperty = "a string"
    5. 'etc etc
    6.  
    7. MyCustomTypeList.Add(structureinstance)

    Get the idea?

    EDIT: When you say "its a VB6"... do you mean your application is written in VB6? If so, you should post it in the VB6 forum and not the VB.NET forum
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: ArrayList and structures

    VB6 wouldn't have the ArrayList, so the interpretation of Redim being VB6 is that it is legacy.
    My usual boring signature: Nothing

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ArrayList and structures

    Ah, thats what I was hoping thanks for the clarification
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    128

    Re: ArrayList and structures

    That's exactly what I meant :-)
    Thanks you all!

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: ArrayList and structures

    Dont forget to mark your thread as Resolved if you've got everything you need (go to the Thread Tools menu at the top of your first post in this thread)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

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



Click Here to Expand Forum to Full Width