Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP and VB 2

Status
Not open for further replies.

Motor11

Technical User
Jul 30, 2002
60
0
0
US
Sorry that this turned into a long rant. Maybe someone will find it interesting enough to make it all the way through and comment.

I've been programming VB for quite a while but have managed for the mostpart to avoid using classes. On my latest project I decided to bite the bullet and go totally OOP. The result? Frustration.

My object is essentially a rectangle defined by the cartesian coordinates of its corners. The user will be able to place the rectangles in a real-world coordinate system using data from a survey (I'm a surveyor). Seems easy enough, right?

So the first thing I do is create a user defined type (UDT) for a cartesian point:

Code:
'IN A REGULAR MODULE

Type point2d
    X As Double
    Y As Double
    ptname as string
End Type

But it seems that I can't pass a UDT to my object. Why not? Okay, well I'll make a cartesian point object and pass that:


Code:
'IN MY NEW POINT2D CLASS MODULE

Private Type point2d
    X As Double
    Y As Double
    ptname as string
End Type

Private objPoint2d As point2d

Public Property Let X(X As Double)
    objPoint2d.X = X
End Property

'and so on for 'Y' and 'ptname'


Okay, now I can pass my point object to my rectangle class, which will contain an array of four points. Life is good, right? Not quite.

I need to parse an ascii file for points. So I'll just do what I always do - write a function:

Code:
'IN A FORM

private function Ascii2Pt (instrin as string) as point2d
 'do stuff here
end function

Well, VB doesn't seem to like that. It seems that functions can't return classes. So I'm left making a new method in my simple point2d class:

Code:
'BACK TO THE DANG CLASS MODULE

Public Property Let AsciiToPoint(mystring As String)
    'Do some more stuff
End Property

All this to try to mimic the functionality of a UDT. What a waste. All this and I haven't even gotten to create the class that I really care about (the rectangle).

Should I go back to writing my non-OOP code? I can't seem to get my head around VB's logic here. Do people really jump through all these hoops in order to work with classes? What am I missing?
 
I have very little use for the Type structure. I use Classes almost exclusively. It's how you get as much OOP as VB can do.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi Chiph,

I accidentally posted this question twice (it is being discussed on two threads now). With some help, I have narrowed my problem somewhat.

Right now I'm having trouble passing an object through a function. For example:

Code:
Dim mypoint As New clsPoint2d
mypoint = ascii2pt(UserString)

and elsewhere:
Code:
Public Function ascii2pt(instring As String) As clsPoint2d
    Dim holder() As String     'temp variable to hold split string
    Dim i As Integer
    Set ascii2pt = New clsPoint2d
    holder = Split(instring, ",")
    For i = 0 To UBound(holder)
        holder(i) = Trim(holder(i))
    Next i
    ascii2pt.X = Val(holder(0))
    ascii2pt.Y = Val(holder(1))
    If UBound(holder()) > 1 Then ascii2pt.ptname = holder(2)
    
End Function

I get an "object doesn't support this property or method"
error after the function excecutes and tries to pass the object to the mypoint variable (which is the clsPoint2d type).

Any ides would be very much appreciated.

Ryan
 
Motor11

If you want to pass UDT's to Classes (including Forms) look into Subs Functions Declared with the Friend keyword.

Regards Hugh
 
Gentlemen,

Thanks for the advice. It looks like I'm going to have to get a better handle on the 'set' and 'new' keywords. Strongm's suggestion was exactly what the doctor ordered. Thank you very much.

I looked up the friend keyword. The entry in MSDN under 'friend members' looks like something I will definitely use to pass UDTs to my class object. In fact, I am going to try a rewrite to see if I can use a point2d UDT and eliminate the extra class module. Is this typically considered less-correct?

Anyways, I am back on track. Thanks again.

Ryan
 
Glad to hear you got it going.

I second chiph's comment. I avoid using UDT's except when needed for API interface. Classes do the same thing and are much more flexible, such as being able to validate data assigned to a property in one handy location, raise errors when invalid data is entered, raise events in forms, etc, etc.

Sure, you might not need those things right now, but if sometime later in the project you find that you do, it's a lot easier to add in.

Robert
 
Based on the advice of TheVampire and Chiph, I'm going to forego the UDT and stick with creating a second class. What's the point of trying to go OOP if you only go half way, right?

One additional question: I know that one of the big selling points of creating objects is encapsulation. My object should stand on its own and be easily reused.

Right now, this isn't the case. My polygon object relies on my point object. This isn't a deal-breaker, but I'm wondering if there isn't a way to make the thing more self-contained?
 
When you are dealing with encapsulation, you are not really saying that your class should be able to stand on it's own. After all, a class could have code that requires a reference to a libray ( for example ADO ). Your class would not work if you forgot to include the reference in your project. This does not mean that the class is not encapsulated.

What encapsulation means is that the class code does not rely on any outside variables ( such as a global variable ) that can be changed elsewhere in the program without the class knowing about it. With a properly encapsulated class, the class owns it's data, and the only way the data can be changed is through the class properties.

Since your object point is a private class to your object and this point can only be manipulated by that particular object, you still have encapuslation. Yes, you do have to have the code for the point class in your program, but you are still doing good OOP and your class is self-contained.

Does that help any?

Robert
 
Robert,

Thanks, that makes sense.

I'm starting to feel better about creating objects. I'm sure I'd have my current project wrapped up with a pretty bow on it if I did it my usual way, but this exercise is informative.

I appreciate all the help from everyone. The resource of Tek-Tips is what made me bold enough to try and go this direction in the first place.

Ryan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top