Newbie to OOP needs basic help

I

Ivan Weiss

I am trying to write my program following OOP the way it is meant to be
and am creating a class to represent an object representing a project
that the users will work on. Here is my code:

Public Class clsProject

Structure myProject
Dim myProjectID As Integer
Dim myCustomerID As Integer
Dim myProjectStatus As String
End Structure

Property ProjectID()
Get
myProjectID
End Get
Set(ByVal Value)
myProjectID = Value
End Set
End Property

End Class

Am I starting this right, should I be using a structure and should I use
Property's like I did there? Also, I am getting an error saying
myProjectID is not defined in the property function yet it is defined as
part of the structure. Am I accessing it wrong? This is very new to
me.

Thanks!

-Ivan
 
R

Rob Teixeira [MVP]

Remove the Structure, and replace the Dim keywords with Private. The fields
of the Structure are not part of the class that is enclosing them the way
you have it written.
Also, I suggest using Properties with explicit types. Add an "As <some
datatype>" clause to your property block.

-Rob Teixeira [MVP]
 
I

Ivan Weiss

So the correct format would be:
In the get statement do I use the return keyword?

Public Class clsProject

Private myProjectID As Integer
Private myCustomerID As Integer
Private myProjectStatus As String

Property ProjectID() As Integer
Get
Return (myProjectID)
End Get
Set(ByVal Value As Integer)
myProjectID = Value
End Set
End Property

End Class

I always thought it was best to use a structure to organize the data
into one item but I guess that essentially is what the class is, a
larger structure.

-Ivan
 
R

Rob Teixeira [MVP]

inline:

Ivan Weiss said:
So the correct format would be:
In the get statement do I use the return keyword?

Public Class clsProject

Private myProjectID As Integer
Private myCustomerID As Integer
Private myProjectStatus As String

Property ProjectID() As Integer
Get
Return (myProjectID)
End Get
Set(ByVal Value As Integer)
myProjectID = Value
End Set
End Property

End Class

That is correct. The return statement is fine.
Remember two things:
1) under the covers a Property is basically like two functions (one to get
and one to set) that behaves like a field in terms of syntax
2) all return values from a function (or Property in this case) can be set
by assigning a value to the function (or property) name, or by using the
return statement. The only difference is that using the Return statement
exits the function immediately (no further code in the function will
execute).
I always thought it was best to use a structure to organize the data
into one item but I guess that essentially is what the class is, a
larger structure.

Both Structures and Classes are containers for encapsulating data, and both
have a lot of similarities. In fact, in C++, classes are just special-case
structures and the difference is more fuzzy in nature. Each has its own
benefits and limitations in the .NET world, so look in the help for the
differences Value Types and Reference types if you are interested.

-Rob Teixeira [MVP]
 
T

Tom Leylan

Ivan: Since you are starting out you'll want to avoid learning bad habits
so you don't have to unlearn them later. :) To answer your question first,
yes you use the return statement. There is no overwhelmingly good reason to
use the parentheses in Return (myProjectID) however. Absolutely a good idea
(thought not necessary) if you had an expression like Return (myProjectID *
10) but otherwise it is pointless.

Next... don't name your class clsProject. Hungarian notation is discouraged
in .Net.

And... don't name your member variables "my" people seem to fall into two
camps, the "m" as in mProjectId and the underscore as in _ProjectId and I
guess some people use "m_ProjectId"

And finally... explicitly scope your properties. This would be: Public
Property ProjectId() as Integer

Also... you might want to initialize your member variables but that's up to
you. VB.Net will initialize them for you but it can help reduce surprises.

Now on with your project,
Tom
 
I

Ivan Weiss

Next... don't name your class clsProject. Hungarian notation is
discouraged
in .Net

So you would recommend just naming my class Project? I thought you
should always put the abbreviation previous to every form/class etc. to
identify it. I am also calling all of my forms frmMain, frmProjects,
etc...

And... don't name your member variables "my" people seem to fall into
two
camps, the "m" as in mProjectId and the underscore as in _ProjectId and
I
guess some people use "m_ProjectId"

If I am not going with myProjectID you are suggesting going with
m_ProjectID instead? Is this really important or just a general
standard people follow.

Thank you for the advice. As far as the initialization I will
eventually be adding a constructor to take care of that, I just started
coding it and wanted to get this far first, lol.

Thanks again!

-Ivan
 
T

Tom Leylan

Ivan Weiss said:
So you would recommend just naming my class Project? I thought you
should always put the abbreviation previous to every form/class etc. to
identify it. I am also calling all of my forms frmMain, frmProjects,
etc...

Nothing is a hard and fast rule. I don't think you will find too many class
libraries (nothing in .Net for instance) that starts with "cls" however.
Naming it Project seems clear enough to me. The lower-case letters "frm"
for instance (Hungarian Notation) typically were applied to an identier.
You'll notice that the Class Button isn't named btnButton but a button
object you create might be named btnOk. If you are going to instantiate
your forms you'll end up with things like:

Private frmMyMain As frmMain = New frmMain

The other argument about HN in OOP is that they aren't buttons or forms...
if you follow the convention you need to begin all your identifiers with "o"
because they are all objects. And then what happens when you subclass a
button to create btnOkButton?

Private okbtnOk As btnOkButton = New btnOkButton
If I am not going with myProjectID you are suggesting going with
m_ProjectID instead? Is this really important or just a general
standard people follow.

I've settled on the underscore but most people probably use the "m" so that
would yield mId or mProjectId. My tendency in a Project class would be to
not preface it's properties with the word "Project" so you get mId and
mStatus. Really important? Heck no... but you have to choose something so
why not go with the system used in every example on Earth :) If you think
"my" makes things clearer for you consider doing it but be prepared for
other people to say "gee I don't do it that way." Of course you will
eventually meet the three other people in the world who use "my" and you can
chat about how intuitive it is :) I'm just kidding you... try not doing it
and if it gets too confusing you can always go back.

Take care,
Tom
 
I

Ivan Weiss

lol that does make sense. Might as well follow good habits, I will try
to follow that more although it is funny because school and re-world
don't mix all that well. In classes that notation as I used it was
drilled into my head so that it became habit and I never even thought
other of it.

-Ivan
 

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