Classes in VB.Net

  • Thread starter Thread starter ats@jbex
  • Start date Start date
A

ats@jbex

I have a class in VB.Net named PartyDetails. The class has a number of
Properties, 1 Sub and 2 Functions.

The Sub is Public Sub New() and the functions are:

Public Function GetParty(ByVal pno as Long) As PartyDetails
Private Function GetPartyFromReader(ByVal reader as SqlDataReader) As
PartyDetails

These are both styled on the MembershipUser class.

On my form I have the following:

Dim p as PartyDetails = Nothing
p = Party.GetParty(pno)

This gives the following error at the above line:

Reference to a non-shared member requires an object reference.

If I change it to the following:

Dim p as PartyDetails = New PartyDetails(some properties)
p.GetParty(pno)

then everything works OK until I try to access any of the PartyDetails
properties. I then get an error telling me to use the New call or to check
for Null values first. There are no null values in the PartyDetails class
and I cannot get this to work.

Any help would be much appreciated.

TIA

--
ats@jbex

The world is my expense
The cost of my desire
Jesus blessed me with its future
And I protect it with fire

Rage Against The Machine - Sleep Now In The Fire
 
ats@jbex said:
I have a class in VB.Net named PartyDetails. The class has a number
of Properties, 1 Sub and 2 Functions.

The Sub is Public Sub New() and the functions are:

Public Function GetParty(ByVal pno as Long) As PartyDetails
Private Function GetPartyFromReader(ByVal reader as SqlDataReader)
As PartyDetails

These are both styled on the MembershipUser class.

What does "styled on" mean?
On my form I have the following:

Dim p as PartyDetails = Nothing
p = Party.GetParty(pno)

This gives the following error at the above line:

Reference to a non-shared member requires an object reference.

Party is a class name. If you use a class name to call a member, it must be
a Shared member (public shared function...).
If I change it to the following:

Dim p as PartyDetails = New PartyDetails(some properties)
p.GetParty(pno)

then everything works OK until I try to access any of the
PartyDetails properties. I then get an error telling me to use the
New call or to check for Null values first. There are no null values
in the PartyDetails class and I cannot get this to work.

Any help would be much appreciated.

TIA


Can you post the line in wich the error occurs?


Armin
 
Hi

Ok, what's happening is you are returning a PartyDetails object with the
p.GetParty(pno), which you are not doing anything with. Really what you
should do is make the GetParty method Shared and use it to create the insance
of the party object:

Public Shared Function GetPart(ByVal pno as Long) as PartyDetails
....
End Function

Then to get the instance, in your form use the code:

Dim p as PartyDetails = PartyDetiails.GetParty(pno)

I think you are currently expecting the code p.GetParty(pno) to populate the
instance of PartyDetails p. If that's what you are really after then you
should populate the current instance of PartyDetails in the GetParty method
rather than retun a PartyDetails object. Personally I think the first
approach is better.

Hope this helps

Tom
 
Back
Top