Search the web
Sign In
New User? Sign Up
helpwithvb · A group for those who need help with Visual Basic
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want to share photos of your group with the world? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Help with constructors and elementary OOP   Message List  
Reply | Forward Message #22039 of 22566 |
RE: [helpwithvb] Help with constructors and elementary OOP

Hi Bob:


MY FIRST PROBLEM

What I want to do is to use the class Lists_AddNumber to contain the code [that
currently operates in the form's code] which sets up a list to accept strings in
exactly the same way that the form's code does.

I would be grateful for an idiot-proof solution to this problem.


Idiot-proof is too far, if you are going to learn... but here's a few fist steps
and concepts:
- You want a class that implements some functionality, mimicking the
functionality that you coded into your form.

Great! This shows a fair understanding of OOP, conceptually. why? The class
can be applied to your form, or any form. Thus, the functionality is said to be
encapsulated in the class. That is, its locked away from what the form really
has to understand, except inasmuch as the form has to deal with an object of the
class -- in coding parlance, the interface between the form class and your
custom class.

Here are the questions you need to answer:
1) a) What goes in the class? b) What does the class code look like? and c)
where does it go?
2) What does the class need to expose to its 'client' (the form). This is the
interface between the classes.
3) How does the form use such a class?

Let's examine each in turn, briefly.

1) The main ingredients of a class are its members:
a) fields (variables or data elements, or objects it declares and
instantiates or references), This is the data or "things" it manipulates.
b) methods (subs or functions that define its behavior (both the fields
and methods can be private (internal to the class) or public (visible outside of
the class))
c) events (that it can raise... let's not worry about this type, here,
since your won't have any).

Here's the first conceptual hurdle... a field in a class doesn't exist until the
class does. This class will need a list.

b) What methods does the class need?
Well, it will certainly need a means to add a member. It will likely need a
means to display a member. Both of these would be subs (routines) in your
class. Both would be public -- see below.

c) No events needed.

2) What is the interface? Its the functions or fields (and sometimes events)
that things outside the class can know about. Well, the functions in b) above
are probably your class' interface. Not the list. That would be private.
Doing it that way is encapsulating the list within the class -- or hiding it, or
"data hiding" -- showing the outside world only what it needs to know.

So you would have a public AddElement routine and a public display routine.
Also a private list object. The former should take an argument of the type to
be added (number, or string, or whatever). Since the list exists only in the
class, you would also have to instantiate the list as part of the class. That
could be part of the constructor.

3) Now what? Well, in your form you declare an instance of myclass. That will
create your object, which, in turn will create a list object for itself to use.

Here's where constructors come in handy. See below.



MY SECOND PROBLEM

Can anyone please give me some absolutely elementary guidance as to

1. What a constructor is [in VB 2008] and what does it do?
2. Where do you use it - in the form file or in a separate class file?
3. What is the syntax of a constructor?


Sure.
1) A constructor, in any OOPS language, is a method of your class that runs
whenever an object of that type is instantiated (actually created).
In VB.NET, that occurs (pretty much, but there are exceptions) when you use the
New keyword. e.g.

Dim x as MyClass = New MyClass

OK. You can't see it, but there is a constructor running when you get to New
MyClass. If you don't write one, .NET does it for you -- but that 'default'
constructor is pretty boring. You may want to write your own, so that more
interesting things can happen. Constructors, like any function in .NET can be
'overloaded'. That is there can be many with different signatures (argument
lists). The compiler knows which one to run, based on how you call it in your
code. Keep reading.

2) Your constructor(s) are a part of your class, so the code for it goes into
your class. Period.

3) It's pretty simple. Your constructor uses the key word New

So

Dim mlist as List
Sub New()
mlist = New List
' other code here
End Sub

is a constructor with no arguments. If you write this into your class, it runs
when you instantiate an object of your class with

Dim x as MyClass = New MyClass

It also creates a new list object for you. Declaring it (above the sub) set one
up, but there's no actual list until you make one.

If you also write a sub like this

Dim mList as List
Sub New(MyList as List)
mList = MyList
End Sub

You would be passing in a reference to a list instead of creating one. You
might call that where you had a list already:

'in your form
dim MyList as New List
dim x as New MyClass(MyList)

Now .NET will run the SECOND constructor instead of the first one while creating
you object.

Lots of stuff to chew on here. Good fortune.

hth
-BDN

________________________________

From: helpwithvb@yahoogroups.com on behalf of Robert Dade
Sent: Mon 7/6/2009 10:45 AM
To: helpwithvb@yahoogroups.com
Subject: [helpwithvb] Help with constructors and elementary OOP


.





Mon Jul 6, 2009 8:50 pm

thezorch
Offline Offline
Send Email Send Email

Attachment
winmail.dat
Type:
application/ms-tnef
Forward
Message #22039 of 22566 |
Expand Messages Author Sort by Date

Hello I am a novice programmer who is doing an Open University [UK] degree level unit [for fun] on object oriented programming with VB Express Edition 2008. I...
Robert Dade
rwdade
Offline Send Email
Jul 6, 2009
3:45 pm

Bob, You may want to grab a copy of this book and check out chapter 5 for a coverage of constructors and more in designing encapsulated classes. ...
Timothy Rupp
timrupp804
Offline Send Email
Jul 6, 2009
5:46 pm

Hi Bob: MY FIRST PROBLEM What I want to do is to use the class Lists_AddNumber to contain the code [that currently operates in the form's code] which sets up a...
Neiger, Bruce D
thezorch
Offline Send Email
Jul 6, 2009
8:53 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help