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 strongm 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 1

Status
Not open for further replies.

Motor11

Technical User
Jul 30, 2002
60
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 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?
 
Well, VB doesn't seem to like that. It seems that functions can't return classes."

As far as I know, they can. My reference book states that functions can return almost anything, including objects ( classes ), but that they cannot return UDT's.

Perhaps your problem is that you have a UDT in the class you are trying to have the function return. Why have the UDT in the class at all? Just have a private variable in the class for each of the properties. You've already got a Let/Get statement for each item, so it's easy to change.

Try that and see if it works.

Robert
 
Thanks for the tip. I will try it.

Just for reference, functions can return UDT's. I do that all the time and never had a problem with it.
 
The day someone writes a "non-OOPS" application, let me know! that wasn't a mistype... OOPS is correct!

I alwasys have lot's of OOPS in my code it seems!

Couldn't resist a little humor!
 
I am still having no luck creating a function that returns (or is) a class object.

I call the function here:

Code:
Private Sub mnuAddpoint_Click()
    Dim message As String
    Dim title As String
    Dim UserString As String
    Dim mypoint As New clsPoint2d
    
    'just get point from user with input box (for testing purposes)
    message = "Enter point:"   ' Set prompt.
    message = message + vbCrLf + "        format:   xxxxxxxx,yyyyyy,name"
    title = "Enter Point"   ' Set title.
    UserString = InputBox(message, title, default)
    mypoint = ascii2pt(UserString)
    

End Sub


The function consists of:

Code:
Private Function ascii2pt(instring As String) As clsPoint2d
    Dim holder() As String     'temp variable to hold split string
    Dim i As Integer
    
    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

And my class object now looks like:

Code:
Private m_X As Double
Private m_Y As Double
Private m_name As String

Public Property Let X(X As Double)
    m_X = X
End Property

Public Property Get X() As Double
    X = m_X
    
End Property

'same for Y and ptname
    
End Property

I get the message "object variable or with variable block not set" upon my first reference to the object within my function:
Code:
ascii2pt.X = Val(holder(0))

Any ideas?
 
The thing that jumps out at me is
Code:
Public Property Let [COLOR=red][b]X[/b][/color]([COLOR=red][b]X[/b][/color] As Double)
    m_X = X
End Property
You have a property name and a calling argument with the same name. If VB is interpreting m_X = X as an attempt to set m_X to the value of the property X rather than the calling argument of the same name then indeed, property X has not yet been set.
 
Thanks for the suggestion. I made the change but still no success. The class now reads:

Code:
Public Property Let X(inval As Double)
    m_X = inval
End Property

Public Property Get X() As Double
    X = m_X
    
End Property
 
Private 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 the message "object variable or with variable block not set" upon my first reference to the object within my function:"

Well, yeah. You haven't declared or created the 'ascii2pt' object - that's just the name of your function. Try this instead:

Private Function ascii2pt(instring As String) As clsPoint2d
Dim holder() As String 'temp variable to hold split string
Dim i As Integer
Dim retPt as New clsPoint2d

holder = Split(instring, ",")
For i = 0 To UBound(holder)
holder(i) = Trim(holder(i))
Next i
clsPoint2d.X = Val(holder(0))
clsPoint2d.Y = Val(holder(1))
If UBound(holder()) > 1 Then clsPoint2d.ptname = holder(2)

Return clsPoint2d
End Function
 
jcfiala, you ref'd your variable by type instead of by name. see the bold below.

Private Function ascii2pt(instring As String) As clsPoint2d
Dim holder() As String 'temp variable to hold split string
Dim i As Integer
Dim retPt as New clsPoint2d

holder = Split(instring, ",")
For i = 0 To UBound(holder)
holder(i) = Trim(holder(i))
Next i
clsPoint2d.X = Val(holder(0))
clsPoint2d.Y = Val(holder(1))
If UBound(holder()) > 1 Then retPt.ptname = holder(2)

Return retPt
End Function
 
jcfiala, Sheco ... which version of VB do you think you are using?
 
Thanks for the suggestions guys. We're getting closer, however I'm still not quite there.

My function now looks like:
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
But I get an error in the calling line:
Code:
mypoint = ascii2pt(UserString)

the error is: "object doesn't support this property or method"

As for the 'return' suggestion, the IDE chokes at it. Can you explicitly return something in a function in VB6? It wants an end of statement after 'return'. I think "return variable" might be C syntax. Doesn't a function always return the contents of the variable that shares the function name?
 
Wouldn't that be:

Set mypoint = ascii2pt(UserString)

instead ?

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top