Need help with List(of T) (newbie)

P

PMK

Hi,

I'm trying to translate this little tutorial from C# to VB but am
going wrong somewhere.

I create a simple custom class in customer.vb

Public Class Customer
Private m_age As Integer
Private m_name As String

Private Property Age() As Integer
Get
Return m_age
End Get
Set(ByVal value As Integer)
m_age = value
End Set
End Property

Private Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property

'NOT SURE I HAVE WRITTEN THE METHOD CORRECTLY OR NOT
'THE C# SAMPLE USES THE SAME NAME 'CUSTOMER' FOR THE METHOD SO THAT IS
WHAT I DID (ALSO TRIED OTHER NAMES)
Public Sub Customer(ByVal pName As String, ByVal pAge As Integer)
m_age = pAge
m_name = pName
End Sub
End Class

Then in another class, the C# code is:
public class SalesPersonCustomers
{
private List<Customer> _salesCustomers;

public SalesPersonCustomers()
{
_salesCustomers = new List<Customer>();
_salesCustomers.Add(new Customer("Alice", 10));
}

public List<Customer> GetCustomers(string _salesName)
{
return _salesCustomers;
}
}

What i tried (and variations) in VB is:
Dim _salesCustomers as List(of Customer)

Sub SalesPersonCustomers()
_salesCustomers = New List(Of Customer)
_salesCustomers.Add(Customer
End Sub

But I don't get the prompt on the _salesCustomers.Add(Customer line
for the parameters of name and age so the records are not added.

Thanks,

Peter
 
S

sloan

Sub SalesPersonCustomers()
_salesCustomers = New List(Of Customer)
_salesCustomers.Add(Customer
End Sub


Sub SalesPersonCustomers()
_salesCustomers = New List(Of Customer)
_salesCustomers.Add(new Customer("Alice", 10))

End Sub

When you call the .Add method, you need to pass in a (new instance) of the
Customer object.

It could also be:

Sub SalesPersonCustomers()
_salesCustomers = New List(Of Customer)

dim cust as Customer = new Customer ( "Bob" , 20 )

_salesCustomers.Add(cust)


End Sub
 
P

PMK

Hi Sloan,

Thanks for the reply, but it doesn't work. In both cases I get an
error at the parameters:
Too many arguments to public sub new'().

Doing a search, I do find a sub 'new' in the My namespace in
application.designer.vb. I assume that it is normal for that to be
there (but I could be wrong!)

Peter
 
W

wfairl

Hi Sloan,

Thanks for the reply, but it doesn't work. In both cases I get an
error at the parameters:
Too many arguments to public sub new'().

Doing a search, I do find a sub 'new' in the My namespace in
application.designer.vb. I assume that it is normal for that to be
there (but I could be wrong!)

Peter












- Show quoted text -

Public Sub Customer(ByVal pName As String, ByVal pAge As Integer)
m_age = pAge
m_name = pName
End Sub

is the constructor (or should be). In vb.net the keyword "new" is used
instead of the class name as it is in C# (and other OO languages).

So it should be:

Public Sub New(ByVal pName As String, ByVal pAge As Integer)
m_age = pAge
m_name = pName
End Sub
 
S

SAL

sfairl is correct. The reason you are getting that error is because VB is
putting a default, parameter less, constructor in there behind the scenes
and therefore the number of arguments is wrong...

S
 
P

PMK

Got it now - thanks.

Peter


Public Sub Customer(ByVal pName As String, ByVal pAge As Integer)
m_age = pAge
m_name = pName
End Sub

is the constructor (or should be). In vb.net the keyword "new" is used
instead of the class name as it is in C# (and other OO languages).

So it should be:

Public Sub New(ByVal pName As String, ByVal pAge As Integer)
m_age = pAge
m_name = pName
End Sub
 
S

sloan

Yeah, sorry. I didn't read it close enough to see the wrong (invisible)
constructor.

But with the others' help, you got it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top