Class and "Subproperty"

ObjectOriented
11-29-2004, 05:10 PM
Easy question easy awnser :
how to do this :

ClassName.position.x
ClassName.position.y

i know i can do it this way without class

private type object
position as position
end type

private type position
x as integer
y as integer
end type


but how can i do this with class and property ?

GavinO
11-29-2004, 05:28 PM
If the property get routine returns either an object reference or a UDT, then the syntax you showed will work (ie, the property get will return a 'position')

ObjectOriented
11-29-2004, 05:32 PM
how u put the type into the class then ?

i want the class to have subproperty

John
11-29-2004, 05:48 PM
Simple question yes. Simple answer, hardly.

I would do it with two classes as follows:
'Class clsPosition
Option Explicit

Private m_x As Integer
Private m_y As Integer

Private Sub Class_Initialize()
m_x = 0
m_y = 0
End Sub

Public Property Get X() As Integer
X = m_x
End Property

Public Property Let X(value As Integer)
m_x = value
End Property

Public Property Get Y() As Integer
Y = m_y
End Property

Public Property Let Y(value As Integer)
m_y = value
End Property

'Class MyClass
Option Explicit

Private m_pt As clsPosition

Private Sub Class_Initialize()
Set m_pt = New clsPosition

m_pt.X = 0
m_pt.Y = 0
End Sub

Public Property Get Position() As clsPosition
Set Position = m_pt
End Property

Public Property Let Position(value As clsPosition)
Set m_pt = value
End Property

Private Sub Class_Terminate()
Set m_pt = Nothing
End Sub

'Main form
Option Explicit

Private mc As MyClass

Private Sub Form_Load()
Set mc = New MyClass

mc.Position.X = 10
mc.Position.Y = 25
End Sub

Private Sub Form_DblClick()
MsgBox "X = " & mc.Position.X & vbNewLine & "Y = " & mc.Position.Y
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set mc = Nothing
End Sub
Judging by your username I'd say you are looking to learn about OOP. If any of this doesn't make sense feel free to ask.

ObjectOriented
11-29-2004, 05:57 PM
So you make 2 classes ? But theres a problem, the position is not really a separate class. Tell me if im worng a class is an object, so like if this thing is a game, u will have a character (thats our class) and he can do stuffs (subs in the class) and he have proprety like the position and the position got proprety too (x,y). So i dont uderstand why u do 2 classes :S

(thanks for the code i works but i dont understand why u use 2 separated class)

John
11-29-2004, 06:03 PM
The position can most certainly be a seperate class, I would argue that it should be. Many characters can be present in a game. Almost all of those, among other things (plants, trees, clouds) will also have positions. It would make better sense to create a class that all objects (plants, trees, characters, etc.) can use rather than each of them having to take care of the logic of what it means to be a valid position themselves.

2 Classes are used for better OOP. This way you can control what values X and Y get set to in all objects that use the class and subjectively disallow invalid values from within the position object itself rather than from the MyClass class or any other that may use it. One change to the clsPosition class will affect all objects that use it. Make the change there and you won't have to change it in possible 100's of other places in your application (game).

A class doesn't always have to have methods. Usually they do but not always.

ObjectOriented
11-29-2004, 06:12 PM
oh thanks man really nice, but know i good many other question.

Why u pass by reference the variable value ? (in the property Let)

Do i always have to do a nother class to make subproprety ? per exemple if i have 6 subproprety i really have to do 5 separate classes ?

(PS i love OPP, its so real and thats how i think know :)

John
11-29-2004, 06:19 PM
Yeah, I'm down with OPP (http://shop.fye.com/product.aspx?sku=60042378&loc=50244) too ;)

In the class clsPosition the two properties we have take byref arguments for no reason at all other than it is the default in VB6. ByRef or ByVal are the same thing in that place since they are integers and not objects. Passing a 5 (number) or a copy of a 5 (number) doesn't make much difference.

The part where it makes a difference is in the MyClass class (I know bad name *shrug*). In this particular case it isn't a huge deal but still better to use ByRef. Why you ask? Because we want to learn good habits early and part of those good habits is good memory management. Passing something ByVal means that a complete copy of the object will be made in memory and then a reference to that copy will get passed to the callee. Passing it ByRef doesn't make that copy and therefore saves time and memory.

Yes, if you have many subproperties I would make many classes whenever possible. In some cases it may be a matter of preference but to me I would rather use classes because it just seems cleaner to me and makes things more maintainable for future changes.

For the record, this would all be much much simpler and far cleaner and more elegant in VB.NET because there we would have full support for OOP including inheritance. OOP in VB6 is really a joke.

ObjectOriented
11-29-2004, 06:32 PM
hehe thanks so i will use byref for the header of the proprety :)

but i still got a last question :) i plan to finish this year(school year i mean) and after go with C++, what u think of that? is it a good move?

im now in grade 11 and i plan to make high degrees (doctorate hell yeah) in computer science. i spooke to my teacher about that and he said that in university u arent learning a language itself but algoriths. So that **** me off, because i'm a realistic person an i need to work with concreat stuff to learn, so i will try to do my exercise in C++.

other things about class :

since my starting (restarting) an rpg i thinked to work with object, the character is an object an he can jump run as a sub in that class, am i going into the good direction ?

John
11-29-2004, 06:58 PM
Your teacher is a smart person. Language doesn't really matter, it is the concepts which are important. If OOP is what you like then I'd suggest learning C#. It is the latest and greatest language, is free of charge and very well supported in the community. Generally you do learn algorithms in college but of course you will need to know whatever language they use to impliment the algorithms. The school I went to used C++, others use Java, and I've even heard that some colleges are moving to VB.NET now that it is truly object oriented. My choice would be C#.

Yes, a character would be a great example of an object. The character would have some properties such as Height, Weight, Hair, Race, etc. and methods such as Walk, Run, Jump, Eat, etc. Hair would be a good example of another object though since it may itself have properties such as Length, Color, Type etc. Ideally you would be using a more object oriented language such as C#, VB.NET, Java, or C++ where you could then use true inheritance and things would be much easier to plan, code, and maintain.

ObjectOriented
12-01-2004, 04:40 PM
is it possible to have an array attribute ?

for exemple :

perso.inventory.bag.stash(1)

how can you do this, i tried a think but it didnt work :S thanks for help


and what option explicit mean ?

GavinO
12-01-2004, 04:59 PM
Sure, the property would look something like this:

Property Get PropName(Index As Type)
'Index is the one they asked for
End Property
Property Let PropName(Index As Type, NewValue As Type)
'Index is the one they are writing to, NewValue is what they're writing
End Property

You can have as many parameters for properties as you want, the only limitation is that the Let must have 1 more than the Get, and the last one will have the new value. The principle is similar in VB.Net/C#.Net. In C++, you don't have explicit 'property' routines, so you can work the parameters however you wish.

ObjectOriented
12-01-2004, 05:49 PM
thanks a lot, but i still have a problem, since i want a fixed size array, can i tell the property i want an array of range 1 to 6 ?


Private m_Stash(6) As String

Public Property Get Stash(Index As Integer) As String

If Index <= 6 And Index >= 0 Then
Stash(Index) = m_Stash(Index)
Else
MsgBox "Index between 0 and 6",,"The Stash Class"
end
End If

End Property

Public Property Let StashIndex As Integer, ByVal Value As String)
If Index <= 6 And Index >= 0 Then
m_Stash(Index) = Value
Else
MsgBox "Index between 0 and 6",,"The Stash Class"
end
End If
End Property


i mean how can i tell the intelli-sence feature that i want an index ranged from 0 to 6 ?

GavinO
12-02-2004, 10:31 AM
I don't know of a way to have the intellisense pop up the limits, but if you document the limits and throw an out-of-bound error when the property is called out of bounds, you should be safe.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum