Structure

VBnetster

Active member
Joined
Feb 15, 2008
Messages
32
Programming Experience
Beginner
hi, couldnt really think of a subject for this post...

I best show my code it kinda explains itself better than i can explain.

VB.NET:
    Structure customers
        Dim name As String
        Dim email As String
        Dim phone As Double
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim people(4) As customers
        people(0).name = "sean"
        people(0).email = "sean@xx.com"
        people(0).phone = 5467892299

        people(1).name = "julia"
        people(1).email = "julia@xx.com"
        people(1).phone = 5467892299

        people(2).name = "anna"
        people(2).email = "anna@xx.com"
        people(2).phone = 5467892299

        people(3).name = "melanie"
        people(3).email = "melanie@xx.com"
        people(3).phone = 5467892299

    End Sub

So, what i want to do is loop through the contents of this array.. i used structure while i "could" have used a multidimensional array, but i just wanted to try this out.

It isnt the same as working with the avarage array, it seems i cant simply use a for each loop. is there some other way i can loop through this to get "name, email, phone" ?

thx
 
I would suggest you look through the Class Question and Custom classes (again) threads.

I dont profess to be an expert on classes (yet :D) but I think you'll find they'll be a lot more useful to you.

VB.NET:
Option Strict On

Public Class Customer

    Private _Name As String
    Private _Email As String
    Private _Phone As Double

    Public Sub New(ByVal name As String, ByVal email As String, ByVal phone As Double)
        _Name = name
        _Email = email
        _Phone = phone
    End Sub

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Public Property Email() As String
        Get
            Return _Email
        End Get
        Set(ByVal value As String)
            _Email = value
        End Set
    End Property

    Public Property Phone() As Double
        Get
            Return _Phone
        End Get
        Set(ByVal value As Double)
            _Phone = value
        End Set
    End Property

End Class

and

VB.NET:
Option Strict On

Public Class Form1
    Private CustomerList As List(Of Customer)


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CustomerList = New List(Of Customer)
        CustomerList.Add(New Customer("sean", "sean@xx.com", 73))
        CustomerList.Add(New Customer("julia", "julia@xx.com", 743))
        CustomerList.Add(New Customer("anna", "anna@xx.com", 7532))
        CustomerList.Add(New Customer("melanie", "melanie@xx.com", 654))

        For Each _Customer As Customer In CustomerList
            MessageBox.Show(_Customer.Name)
        Next
    End Sub
End Class

Hope that helps.
 
I would have to agree with InertiaM, a class would be more fitting for this.

Also, it is kinda rare to need to maintain a primitive array anymore, you can use arraylist or Lists (like InertiaM demonstrated) which is generic and much easier to use (you could use that for each loop you like). In your example of using a regular array, you could have simply used a regular for loop =-p
 
i fell in love with the for each loop :p do use the normal for sometimes when it's a must. And for some reason I can't stand classes! I have a book that is teaching me VB.net i've read through using classes but daaamn, for some reason I just cant adapt to it! I manage to live without but I know if i want to work as a VB.Net coder I will need them!

I have no problem learning anything else, I can adapt to just about everything. I used to code in MASM a couple of years ago I've used pascal before that and server side scripting languages like PHP & Perl. But I've always managed to avoid using classes :p

Well I dropped everything for VB.Net, I saw it as being the future. It seems job wise almost everything is PHP but erm not sure why but I gave that up for VB.Net.
 
Back
Top