Results 1 to 14 of 14

Thread: [RESOLVED] [2005] Array of structures

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved [RESOLVED] [2005] Array of structures

    VB Code:
    1. Private Structure tPlaylist
    2.         Dim Text As String
    3.         Dim Name As String
    4.     End Structure
    5.     Dim Playlists() As tPlaylist

    Later on in my code, when I get the amount of playlists I do the following:
    VB Code:
    1. Public Sub readPlaylist(ByVal lv As ListView, ByVal cm As ContextMenuStrip)
    2.         Dim Names() As String = IO.Directory.GetFiles(Sansa & "PLAYLISTS\", "*PLA")
    3.         Dim Playlists(Names.Length - 1) As tPlaylist
    4.         For i As Int32 = 0 To Playlists.Length - 1
    5.             Playlists(i).Text = (My.Computer.FileSystem.ReadAllText(Names(i)).Replace(Convert.ToChar(0), String.Empty))
    6.             Playlists(i).Name = IO.Path.GetFileNameWithoutExtension(Names(i))
    7.         Next
    8.  
    9.     End Sub
    However playlists is returning a nullreference error. If I set the definition up top to a constant number, then it works- but I cannot do it like that. The Playlists() needs to be public so I can access it througout my class. Any ideas?

    edit**

    I changed my code. Now it works inside the readPlaylist() sub, but the values of Playlists() are lost after the sub closes..How can I fix this??
    Last edited by |2eM!x; Dec 16th, 2006 at 04:28 PM.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Array of structures

    Dim Playlists(Names.Length - 1) As NEW tPlaylist

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

    Re: [2005] Array of structures

    Sorry Gig, not this time. That code won't compile.

    Remember that arrays themselves are reference type objects, whether they contain reference types or value types. They are declared a little differently to other reference types though.

    This code:
    VB Code:
    1. Dim Playlists() As tPlaylist
    declares a variable of type tPlaylist() but it creates no object. This code:
    VB Code:
    1. Dim Playlists(n) As tPlaylist
    declares a variable of type tPlaylist(), creates a tPlaylist array object with n+1 elements and assigns it to that variable. This code:
    VB Code:
    1. Dim Playlists() As tPlaylist = New tPlaylist(n) {}
    does the same and may make it more clear exactly what's happening.
    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
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2005] Array of structures

    jm,

    what's the difference between
    this that won't work:
    Dim Playlists(Names.Length - 1) As NEW tPlaylist

    and this that will work:
    Dim Arr as New ArrayList

    Besides the obvious difference between the type that is being declared. Why "New" with ArrayList and not with System Array (I think it's appropriately called a System Array).

  5. #5
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2005] Array of structures

    Ah,
    Nevermind.

    I think the answer is that the ArrayList initializes it's own capcity to a set number whereas the system array doesn't know what to do on it's own and therefore not specifying the number of elements is not the correct way of passing the required info to the constructor.

    Correct?

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

    Re: [2005] Array of structures

    An ArrayList is a class that maintains an array internally. It looks something like this:
    VB Code:
    1. Public Class ArrayList
    2.  
    3.     Private Const DEFAULT_CAPACITY
    4.  
    5.     Private items As Object()
    6.  
    7.     Public Sub New()
    8.         Me.New(DEFAULT_CAPACITY)
    9.     End Sub
    10.  
    11.     Public Sub New(ByVal capacity As Integer)
    12.         Me.items = New Object(capacity - 1) {}
    13.     End Sub
    14.  
    15. End Class
    Note that the language itself supports arrays, which is why you're able to create an array object without using the "New" key word. There is no specific support for the ArrayList or any other collection. They are just classes like any other.
    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

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [2005] Array of structures

    Quote Originally Posted by jmcilhinney
    Sorry Gig, not this time. That code won't compile.
    I noticed what I replied just now, and realized how silly I was... I wrote the post real quick before leaving to take care of some business...

  8. #8
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2005] Array of structures

    Note that the language itself supports arrays, which is why you're able to create an array object without using the "New" key word. There is no specific support for the ArrayList or any other collection. They are just classes like any other.

    Can you explain more about what you mean when you say "the language itself supports arrays whereas there is no specific support of the ArrayList?

    Thanks

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

    Re: [2005] Array of structures

    Arrays are a critical element of any decent, general-purpose programming language. As such, VB has been created in such a way as to include specific support for arrays. That means that the language itself knows what an array is and includes functionality to enable them to be used. When VB was created the authors said "this is how VB will handle arrays". The authors had no idea that collections in general, or the ArrayList specifically, would exist. The ArrayList is simply a class that makes use of the features of VB and the .NET Framework to provide something useful to us, just like any other class.
    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

  10. #10
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [2005] Array of structures

    Ok, good expanation but based on this part of your post:
    "Dim Playlists() As tPlaylist = New tPlaylist(n) {}"
    I'm a little confused. What role is the New keyword playing here other than perhaps giving us the option to call the constructor explicity when we don't have to.

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

    Re: [2005] Array of structures

    Quote Originally Posted by FourBlades
    Ok, good expanation but based on this part of your post:
    "Dim Playlists() As tPlaylist = New tPlaylist(n) {}"
    I'm a little confused. What role is the New keyword playing here other than perhaps giving us the option to call the constructor explicity when we don't have to.
    It's exactly as you say: calling the constructor explicitly when you don't need to. This goes to the difference between VB and C#. In VB you can use the "As New" syntax to declare a variable and create an object on the same line without an explicit assignment, while in C# you can't. In VB these are both valid:
    VB Code:
    1. Dim obj1 As New Object
    2. Dim obj2 As Object = New Object
    while in C# you must do this:
    Code:
    object obj = new object();
    Likewise with arrays, in VB you can declare a variable and create an object in one line without an assignment, while in C# you can't. In VB these are both valid:
    VB Code:
    1. Dim myArr1(length - 1) As Object
    2. Dim myArr2() As Object = New Object(length - 1) {}
    while in C# you must do this:
    Code:
    object[] myArr = new object[length];
    In VB you can also create an array object after declaring it without explicitly invoking a constructor, while in C# you can't. In VB these are both valid:
    VB Code:
    1. Dim myArr1() As Object
    2. Dim myArr2() As Object
    3.  
    4. ReDim myArr1(length - 1)
    5. myArr2 = New Object(length - 1) {}
    while in C# you must do this:
    Code:
    object[] myArr;
    
    myArr = new object[length];
    Basically, VB offers a VB-specific way to handle arrays, plus it offers a more standard .NET way to handle arrays. Which you choose to use is up to you, but either way you should be consistent.
    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

  12. #12

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Array of structures

    Thanks JM. Figured it out after a few tries.

    Okay so in the top of my code I have this:
    VB Code:
    1. Private Structure tPlaylist
    2.         Dim Text As String
    3.         Dim Name As String
    4.     End Structure
    5.     Dim Playlists() As tPlaylist

    In one sub I size the playlist array-
    VB Code:
    1. Playlists = New tPlaylist(Names.Length - 1) {}

    And it remembers its values, in another sub I access its values and they are still live.

    Thanks alot JM. However, is it possible for you to explain why I need these extra brackets {} at the end?

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

    Re: [2005] Array of structures

    Quote Originally Posted by |2eM!x
    is it possible for you to explain why I need these extra brackets {} at the end?
    That's what indicates to the compiler that you're creating an array. In C# that's not required because the square brackets do the job. If you were to leave the braces off you'd have this:
    VB Code:
    1. Playlists = New tPlaylist(Names.Length - 1)
    which would be interpreted as invoking a tPalylist constructor with an Integer argument and assigning the result to the Playlists variable. That's obviously not what you want and would fail due to mismatched types anyway.
    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

  14. #14

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Array of structures

    Thanks. Makes perfect sense

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