|
-
Apr 14th, 2010, 11:58 PM
#1
Thread Starter
New Member
Needing some help with arrays
I'm still learning VB.net and I struggle with it a lot
I have done my assignment that is due Wednesday, but I there is a bit of code I have added which is an array and I want to add a list of items to the array. If that makes sense.
I have 6 classes two have arrays.
Here is the code from one of the classes
Public Function getItems() As Array
Return _LevelItems
End Function
How would I add the item list to this?
Where do I put it? Like underneath it or in the items class? That code is in the Level class.
Thanks
-
Apr 15th, 2010, 12:06 AM
#2
Re: Needing some help with arrays
You should not be declaring the method return type as Array for a start. You only ever use the Array type if you want to be able to refer to different types of arrays. If that's not the case then you should use the specific type of the array, e.g. String() or Integer().
As for the question, if you have a function that returns the array then you need to call that function from outside the object, e.g. in a form, to get the array object. You can then get and/or set the elements of the array.
If you want to be able to change the size of the array then you have a problem. The way you have things set up, you won't be able to. You would have to add extra/different functionality to your class for that. This is one of the reasons that collections are used in preference to arrays in many scenarios, especially where the number of items is variable.
-
Apr 15th, 2010, 12:15 AM
#3
Thread Starter
New Member
Re: Needing some help with arrays
OK....
Thanks for the reply but it's like i'm reading another language lol.
What I want to do is add items to it.
I put this in but I don't think it's right
Dim Items() As String = {"Item", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10"}
-
Apr 15th, 2010, 12:24 AM
#4
Re: Needing some help with arrays
What do you mean by "add items"? Your array there has 10 elements. Do you mean that you want to replace the existing values with new values, so there will always be 10 elements, or do you mean that you want to add an 11th, etc, element?
-
Apr 15th, 2010, 12:28 AM
#5
Thread Starter
New Member
Re: Needing some help with arrays
Sorry I'm not making myself clear enough... I seriously am such a noob at this. First year learning...
What I want is to know if that code is right?
I want to be able to add the items to the array. Like item, item2, item 3 etc
I don't need to add this part in to my assignment but I wanted to anyway. If I can understand it and make sure it's right.
Public Function getItems() As Array
Return _LevelItems
End Function
Dim Items() As String = {"Item", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10"}
End Class
-
Apr 15th, 2010, 12:33 AM
#6
Re: Needing some help with arrays
I'm afraid that it's still not clear to me what you're trying to do. That code:
Code:
Dim Items() As String = {"Item", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10"}
is valid for initialising an array, if that's the question. I don't really see how that's related to the function though. Your array is named Items and the function returns _LevelItems. Are they supposed to be the same thing? Also, as I said, if that function returns a String array then that's how it should be declared:rather than:
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
|