Basic Class

tyremt

Member
Joined
Apr 15, 2010
Messages
10
Programming Experience
Beginner
Hi all,

I wondered if anyone could help me with the best way to extract data from a custom class I created which inherits the use of ArrayList.

I have a users class as follows:

VB.NET:
Public Class aUser

Public Sub New()
End Sub

Public Sub New(ByVal aName, ByVal aGen)
cName = aName
cGen = aGen
End Sub

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

    Public Property Gen() As String
        Get
            Gen= cGen
        End Get
        Set(ByVal value As String)
            cGen= value
        End Set
    End Property

End Class

And a user Collections class as follows:

VB.NET:
Imports System
Imports System.Collections

Public Class users_Col

    Inherits ArrayList

    Public Sub New()
    End Sub

End Class

And I initialise via a my winform on load:

VB.NET:
'Form Load...

Dim objUsers as New users_Col

users_Col.Add(New aUser("Joe", "Male"))
users_Col.Add(New aUser("Mary", "Female"))

All the above works fine, but I want to extract the user details via something like:

VB.NET:
Dim userOne as string = users_Col(0).Name & " " & users_Col(0).Age
Dim userTwo as string = users_Col(1).Name & " " & users_Col(1).Age

Which doesn't work.

Does anyone have any ideas or tips? :D
 
Right off the bat I see a number of things wrong with your aUser class, you're not storing the Name and Age in a private variable and in the property getter's you're recursively setting the variable instead of returning it. You're not specifying variable types at all.
VB.NET:
Option Explicit On
Option Strict On

Public Class aUser

Private cName, cGen As String

Public Sub New()
End Sub

Public Sub New(ByVal aName As String, ByVal aGen As String)
cName = aName
cGen = aGen
End Sub

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

    Public Property Gen() As String
        Get
            Return cGen
        End Get
        Set(ByVal value As String)
            cGen= value
        End Set
    End Property

End Class
Now later in your code you're using the .Name & .Age properties, yet your class doesn't have an Age property, it has a Gen property though.
 
Opps thats me & typos :eek:

I was using age before but I thought I'd deleted them :)

I was using the above as an example of what I would like to achieve.

You pointed me in the right direction & its working now :) cheers!

Out of curiosity is that an acceptable way to structure a custom class or is there a better or simpler way to achieve the same outcome?

:)
 
That's basically how it's done, though I would reset those string properties in the empty constructor:
VB.NET:
Option Explicit On
Option Strict On

Public Class aUser

    Private cName, cGen As String

    Public Sub New()
        cName = String.Empty
        cGen = String.Empty
    End Sub

    Public Sub New(ByVal aName As String, ByVal aGen As String)
        cName = aName
        cGen = aGen
    End Sub

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

    Public Property Gen() As String
        Get
            Return cGen
        End Get
        Set(ByVal value As String)
            cGen= value
        End Set
    End Property

End Class


Though in VS 2010 you can simplify things a little and do this:
VB.NET:
Option Explicit On
Option Strict On

Public Class aUser

    Public Sub New()
        Me.Name = String.Empty
        Me.Gen = String.Empty
    End Sub

    Public Sub New(ByVal aName As String, ByVal aGen As String)
        Me.Name = aName
        Me.Gen = aGen
    End Sub

    Public Property Name() As String = String.Empty 'Initial value
    Public Property Gen() As String = String.Empty 'Initial value

End Class
 
I'd use a strongly typed list of User and not ArrayList.
VB.NET:
Dim users as New List(Of User)
 
That makes sense :)

Also for instance say I wanted to create a View function in my Collections class users_Col, and I wanted to pass the index of an item i.e.

VB.NET:
aName = users_Col.View(0) ' return the Name for index 0 of the collection

what would be the best way to do this, I understand I'd need a View function in the users_Col class but how do I refer to the underlying aUser class to wire it all up?

I'm just getting picky here out of curiosity, not played with classes much ;)
 
Don't know what you mean by "view", but you can index the collection directly; users(0) is the first item.
 
You can also access the item's properties directly too:
VB.NET:
aName = users_Col(0).Name ' return the Name for index 0 of the collection
Assuming users_Col is a List(Of aUser)
 
Don't know what you mean by "view", but you can index the collection directly; users(0) is the first item.

Ya, that works, but what I was looking for was a way to access the base class (aUser) from within the collections class (users_Col), i.e.

VB.NET:
Imports System
Imports System.Collections

Public Class users_Col

    Inherits ArrayList

    Public Sub New()
    End Sub

    Public Sub View(byVal aIndex as integer)

       'Access the items from aUser for index aIndex

    End Sub

End Class

I'm just trying to get an idea for the inheritance etc. I've googled lots but it yeilded not wha I was trying to understand.

:confused:
 
If you need a custom collection class you should derive from Collection(Of T) from namespace Collections.ObjectModel.

Me keyword provides reference access to current instance from within the class and has same functionality as a variable reference from outside the class. So similarly Me(0) is the first item accessed from the class.
 
Well a Collection is simply that, a collection of objects. The (Of T) means it's a strongly typed collection of whatever type you specify in the 'T' part, which means that collection can only hold object of that type.

Just like there's List(Of T), which means if you have a List(Of String) then only Strings can be stored in it, likewise with List(Of Integer) or List(Of YourClass).
 
hi john, do you have any online references to this or books you recommend :)
I recommend you use the help system included with Visual Studio, it is part or whole of MSDN library and depending on configuration partly local/online. The whole MSDN library is available online at MSDN Library
Try searching for "Collection(Of T)".

Apart from that reading VB.Net books to increase the general knowledge is probably a good recommandation, though personally I've only ever read one VB book (think it was 'learn VB in 24 hours' or something like that) and have learnt most from MSDN and online resources, though again I had previous programming knowledge. Of course you need to apply knowledge to practical usage, 'learn by doing' works for many. Even things you never heard of before and not quite understand right away may seem trivial once you give it a try.
 
Back
Top