|
-
Dec 16th, 2006, 03:46 PM
#1
Thread Starter
Admodistrator
[RESOLVED] [2005] Array of structures
VB Code:
Private Structure tPlaylist
Dim Text As String
Dim Name As String
End Structure
Dim Playlists() As tPlaylist
Later on in my code, when I get the amount of playlists I do the following:
VB Code:
Public Sub readPlaylist(ByVal lv As ListView, ByVal cm As ContextMenuStrip)
Dim Names() As String = IO.Directory.GetFiles(Sansa & "PLAYLISTS\", "*PLA")
Dim Playlists(Names.Length - 1) As tPlaylist
For i As Int32 = 0 To Playlists.Length - 1
Playlists(i).Text = (My.Computer.FileSystem.ReadAllText(Names(i)).Replace(Convert.ToChar(0), String.Empty))
Playlists(i).Name = IO.Path.GetFileNameWithoutExtension(Names(i))
Next
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.
-
Dec 16th, 2006, 07:42 PM
#2
Re: [2005] Array of structures
Dim Playlists(Names.Length - 1) As NEW tPlaylist
-
Dec 16th, 2006, 08:01 PM
#3
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:
Dim Playlists() As tPlaylist
declares a variable of type tPlaylist() but it creates no object. This code:
VB Code:
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:
Dim Playlists() As tPlaylist = New tPlaylist(n) {}
does the same and may make it more clear exactly what's happening.
-
Dec 16th, 2006, 10:15 PM
#4
Frenzied Member
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).
-
Dec 16th, 2006, 10:33 PM
#5
Frenzied Member
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?
-
Dec 17th, 2006, 06:02 AM
#6
Re: [2005] Array of structures
An ArrayList is a class that maintains an array internally. It looks something like this:
VB Code:
Public Class ArrayList
Private Const DEFAULT_CAPACITY
Private items As Object()
Public Sub New()
Me.New(DEFAULT_CAPACITY)
End Sub
Public Sub New(ByVal capacity As Integer)
Me.items = New Object(capacity - 1) {}
End Sub
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.
-
Dec 17th, 2006, 06:08 AM
#7
Re: [2005] Array of structures
 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...
-
Dec 17th, 2006, 06:10 AM
#8
Frenzied Member
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
-
Dec 17th, 2006, 06:35 AM
#9
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.
-
Dec 17th, 2006, 09:26 AM
#10
Frenzied Member
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.
-
Dec 17th, 2006, 05:52 PM
#11
Re: [2005] Array of structures
 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:
Dim obj1 As New Object
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:
Dim myArr1(length - 1) As Object
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:
Dim myArr1() As Object
Dim myArr2() As Object
ReDim myArr1(length - 1)
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.
-
Dec 17th, 2006, 07:01 PM
#12
Thread Starter
Admodistrator
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:
Private Structure tPlaylist
Dim Text As String
Dim Name As String
End Structure
Dim Playlists() As tPlaylist
In one sub I size the playlist array-
VB Code:
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?
-
Dec 17th, 2006, 07:19 PM
#13
Re: [2005] Array of structures
 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:
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.
-
Dec 17th, 2006, 07:53 PM
#14
Thread Starter
Admodistrator
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|