 |

11-29-2004, 05:10 PM
|
 |
Freshman
|
|
Join Date: Nov 2004
Location: Montréal, Québec
Posts: 32
|
|
Class and "Subproperty"
|
Easy question easy awnser :
how to do this :
ClassName.position.x
ClassName.position.y
i know i can do it this way without class
Code:
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 ?
|
|

11-29-2004, 05:28 PM
|
 |
Coder of Fortune
Retired Leader * Expert *
|
|
Join Date: Dec 2002
Location: Troy, NY USA
Posts: 3,120
|
|
|
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')
|
__________________
-- The Gavster
Like to IRC? Try irc.randomirc.com
GavServer
|

11-29-2004, 05:32 PM
|
 |
Freshman
|
|
Join Date: Nov 2004
Location: Montréal, Québec
Posts: 32
|
|
|
how u put the type into the class then ?
i want the class to have subproperty
|
|

11-29-2004, 05:48 PM
|
 |
Bit Flipper
|
|
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
|
|
Simple question yes. Simple answer, hardly.
I would do it with two classes as follows:
Code:
'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
Code:
'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
Code:
'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.
|
|

11-29-2004, 05:57 PM
|
 |
Freshman
|
|
Join Date: Nov 2004
Location: Montréal, Québec
Posts: 32
|
|
|
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)
|
|

11-29-2004, 06:03 PM
|
 |
Bit Flipper
|
|
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
|
|
|
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.
|
|

11-29-2004, 06:12 PM
|
 |
Freshman
|
|
Join Date: Nov 2004
Location: Montréal, Québec
Posts: 32
|
|
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 
|
|

11-29-2004, 06:19 PM
|
 |
Bit Flipper
|
|
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
|
|
Yeah, I'm down with OPP 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.
|
Last edited by John; 11-29-2004 at 07:19 PM.
|

11-29-2004, 06:32 PM
|
 |
Freshman
|
|
Join Date: Nov 2004
Location: Montréal, Québec
Posts: 32
|
|
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 ?
|
|

11-29-2004, 06:58 PM
|
 |
Bit Flipper
|
|
Join Date: Feb 2002
Location: The Inner Loop
Posts: 5,550
|
|
|
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.
|
|

12-01-2004, 04:40 PM
|
 |
Freshman
|
|
Join Date: Nov 2004
Location: Montréal, Québec
Posts: 32
|
|
|
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 ?
|
|

12-01-2004, 04:59 PM
|
 |
Coder of Fortune
Retired Leader * Expert *
|
|
Join Date: Dec 2002
Location: Troy, NY USA
Posts: 3,120
|
|
Sure, the property would look something like this:
Code:
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.
|
__________________
-- The Gavster
Like to IRC? Try irc.randomirc.com
GavServer
|

12-01-2004, 05:49 PM
|
 |
Freshman
|
|
Join Date: Nov 2004
Location: Montréal, Québec
Posts: 32
|
|
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 ?
Code:
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 ?
|
Last edited by ObjectOriented; 12-01-2004 at 05:57 PM.
|

12-02-2004, 10:31 AM
|
 |
Coder of Fortune
Retired Leader * Expert *
|
|
Join Date: Dec 2002
Location: Troy, NY USA
Posts: 3,120
|
|
|
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.
|
__________________
-- The Gavster
Like to IRC? Try irc.randomirc.com
GavServer
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|