Results 1 to 6 of 6

Thread: Needing some help with arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    14

    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

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

    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.
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    14

    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"}

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

    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?
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    14

    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

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

    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:
    Code:
    As String()
    rather than:
    Code:
    As Array
    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

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