Results 1 to 6 of 6

Thread: One array instead of 3

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    75

    One array instead of 3

    The question is simple, but I want it to ask.
    It's usually happend that I have some arrays with same length.
    For example name(), surname(), age()
    Is it the best way to leave all info in 3 arrays or it possible ( better ) to concatenate somehow?

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: One array instead of 3

    The most appropriate way is to use either an array (or a collection of class instances) or dataset.

    vb Code:
    1. Class Person
    2.    Public Name As String
    3.    Public Surname As String
    4.    Public Age As Integer
    5. End Class
    6.  
    7. Public a_persons() As Person ' Array
    8. Public l_persons As New List(Of Person) ' List
    9. Public d_persons As New Dictionary(Of String, Person) ' Keyed dictionary

    Or use System.Data.Dataset or Datatable classes.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    75

    Re: One array instead of 3

    Ok.Thanks! Spasibo!
    Public a_persons() As Person ' Array
    That's I like.
    But can't get idea why such a code throws error:
    Dim persons(1) As person ' Array

    Try
    persons(0).Name = "Vasya"
    persons(0).Name = "Vasin"
    persons(0).Age = 21

    MsgBox(persons(0).Name)
    Catch
    End Try

    Will try to find some info to read.

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: One array instead of 3

    When you define an array in this way, each entry is a class.

    Code:
    Dim persons(1) As person ' Array
    
    persons(0) = New person With {.Name = "Vasya", .Age = 21}
    
    or 
    
    persons(1) = New person 
    persons(1).Name = "Vasin"
    persons(1).Age = 22

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    75

    Re: One array instead of 3

    Thank you.
    So, it's not very convinient in my case.. May be better to leave arrays.

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: One array instead of 3

    If you were to change the class to a structure, you do not have to instance it. However, classes are what OOP is all about, every button, every form, every string, every array are all classes.

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