 |
 |

08-11-2003, 10:16 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
By Reference(Is it impossible?)
|
Hello
I'm programming some game in DirectX(i know this is the wrong forum, but my question isn't about Dx). DirectX has in its library a type called point which just exist out a x and y long. I use this to store the mouse position. I want to pass the variable i stored the position of the mouse in to a class so that class also knows where it is.
First VB doesn't like the directX type because it says it is a private type so you can't pass it to a function of another class. So i tried to just pass 2 long variants.
Now the second problem how can i pass them by reference and link them(just the reference, not the values) to 2 variables i declared in my class.
I couldn't get it done, probably i forgot a simple VB function or something that does it. Pls help me, because i only get it to store the current values and that is no use because then the class doens't know over a few seconds where the mouse really is only where it was.
I hope youse understand me.
ChoKamir
|
Last edited by ChoKamir; 08-11-2003 at 12:39 PM.
|

08-11-2003, 10:32 AM
|
 |
MetaCenturion
Retired Moderator * Guru *
|
|
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
|
|
Use Property Let and Property Get
Code:
Private mPoint As point
Public Property Let MousePoint(ByVal RHS As point)
mPoint = RHS
End Property
|
|

08-11-2003, 11:47 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
Quote: Originally Posted by Iceplug Use Property Let and Property Get
Code:
Private mPoint As point
Public Property Let MousePoint(ByVal RHS As point)
mPoint = RHS
End Property
This won't work, It doesn't allow RHS(or any point) variable to be a parameter of a function/sub/property or what ever that is public. And secondly it is byVal so i don't get the reference. What i try to do is:
that de mPoint changes when the variable given by RHS changes here is an example just simpler:
'In Class1
Option Explicit
Dim temp as long 'Just a variable
sub Init(m_temp as long)
set temp = m_temp (which doesn't work with longs but with normal objects it does)
end sub
function getTemp() as long
gettemp = temp
end function
'in a form
Dim x as new class1
Dim frmTemp as long
Private Sub Form_Load()
frmTemp = 3 'Stores a value in the variable
x.init frmTemp 'passes the variable to the class
frmTemp = 4 'Stores a new value in the variable
debug.print x.getTemp 'Gets the value of the variable in the class
end sub
And you will see the output is 3 while it should be 4. Of coars i can write a new class to store the long value in and pass that as a object to the class. It would work that way but isn't really effiecient with memory and then you need so many classes(who do all little things) in your project that it gets a whole mess.
Thx for you try any way
ChoKamir
|
|

08-11-2003, 12:36 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
|
Is your class a private class in a standard exe project, and if so, does
this point variable need to be passed to any thing other than the form?
|
|

08-11-2003, 12:39 PM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
Quote: Originally Posted by Thinker Is your class a private class in a standard exe project, and if so, does
this point variable need to be passed to any thing other than the form?
Actually it is a project running on DirectX the type is defined in the DirectX dll. I've a form from where i call on a class. that class calls on another class. The form isn't used for anything. In my example you just have to swap the form for a class to. So it is between 2 classes.
ChoKamir
I hope it helps, i think it is impossible what i try to do
|
|

08-11-2003, 12:45 PM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
|
I'm sorry, I still don't understand. It obviously isn't impossible to do if
it involves a DirectX object. If I understood exactly what you were
trying to do I know I could tell you what would work.
|
|

08-11-2003, 12:52 PM
|
|
Centurion
* Expert *
|
|
Join Date: Feb 2002
Location: Virginia, USA
Posts: 145
|
|
|
It sounds like he has the direct X object declared in his form and he wants to access it within his class. What I would do is Set a reference to it within his class. He's saying that it is complaining about something being private, I think he has something set up wrong.
What objects are you using? Let us see portions of the code, otherwise we are shooting in the dark.
-- Van^
|
__________________
Spedism is a disease and I am the cure. :)
|

08-12-2003, 05:26 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
Okay Forget everything that is said before. I just uploaded a simpled version of the problem. Try to get it work. The output is now:
Code:
The Class thinks the value is: 3
But the forms value is: 4
While it should be:
Code:
The Class thinks the value is: 4
But the forms value is: 4
I hope there is a solution for it, but i think it is impossible. Thanks for all you die hards who wanna have a try.
ChoKamir
PS here is a copy of the code, which i uploaded:
Code:
'Class1
Option Explicit
Dim theSameValue As Long 'The variable linked to the variable in the form
Sub Init(m_Value As Long)
theSameValue = m_Value 'Links the 2 variables
End Sub
Sub printMyValue()
Debug.Print "The Class thinks the value is: " & theSameValue 'prints the value of the variable in the class
End Sub
Code:
'Form1
Option Explicit
Dim aValue As Long 'Just a variable to store some information in
Dim x As Class1 'The reference to class1
Private Sub Form_Load()
Set x = New Class1 'Creates a new instance of the class
aValue = 3 'puts some information in the variable
x.Init aValue 'passes the variable to the class
aValue = 4 'changes the value
x.printMyValue 'prints the value of the variable the class thinks it has
Debug.Print "But the forms value is: " & aValue 'Prints the actual value of the variable
'As you see the 2 variables differ, how can i make sure that it doesn't just copy the value in the class but the reference
'to the memory of the variable. With objects it would be easy you can use set ... = ....
'This doesn't work for longs, integer, strings etc
End Sub
|
|

08-12-2003, 05:46 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
|
Here is the solution i found. but i hope there is a better way. Because my program has a lot of those problems and else i would have to create a whole bunch of those classes(valueClass). If youse need more information on it, pls ask me, I can give you enough reasons why i do it this way and not just make the variable in the class or .... But i'll save that talking to another day.
I hope this helps. Most people i talked to on MSN are stunned by the problem. everyone know the solution in C or Java(they are object oriented languages, so they are made for those problems) my only hope is that VB also has some way of doin this.
ChoKamir
PS I found maybe a better way of describing what i want and why:
i want it to use on piece of memory and link all the names(of the variables) to that piece of memory. Not creating a whole bunch of pieces of memory and then updating it every time.
|
|

08-12-2003, 08:49 AM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
|
I have looked at the examples you attached and they don't seem to
have anything to do with your original question. I don't have a clue
what you are trying to do and if I did I could help you.
|
|

08-12-2003, 09:10 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
Quote: Originally Posted by Thinker I have looked at the examples you attached and they don't seem to
have anything to do with your original question. I don't have a clue
what you are trying to do and if I did I could help you.
Okey sorry for all the confusiness(does this word exist??). But what i try to do here and what i explained at the beginning is nearly the same only at the beginning i was doing it with a type defined in the DirectX library and now i'm doing it with a long variant. It is a bit easier with the long variant otherwise you have all the problems with type etc. So if i get it working with the long I will get it to work with the type. But if you prefer to start the hard way you can do that. Thx anyway for looking at my post.
ChoKamir
|
|

08-12-2003, 09:27 AM
|
 |
Disillusioned Code Poet
Retired Moderator * Guru *
|
|
Join Date: Apr 2002
Location: Tennessee, USA
Posts: 12,808
|
|
If I'm understanding the situation at all, it seems you are describing two different problems. First, the point type you mention in your first post -- the easiest way to handle this is simply to reproduce the type definition in your own project, rather than trying to access the private one in the DX library.
And as to your latter problem:
Code:
Set x = New Class1 'Creates a new instance of the class
aValue = 3 'puts some information in the variable
x.Init aValue 'passes the variable to the class
aValue = 4 'changes the value
x.printMyValue 'prints the value of the variable the class thinks it has
Debug.Print "But the forms value is: " & aValue 'Prints the actual value of the variable
^^ In your code above, as you discovered, aValue is simply a copy of the value in x; once set, it does not maintain any link to the object. If you need to access the property of the class itself, use Iceplug's suggestion by adding a Property Let and Get for the variable in Class1:
Code:
Public Property Get Value() As Long
Value = theSameValue
End Property
Public Property Let Value(ByVal NewVal As Long)
theSameValue = NewVal
End Property
Now you can set that property from your form by using x.Value = aValue, and retrieve it as Debug.Print x.Value
Hope this helps.
|
__________________
Laura
Ita erat quando hic adveni.
|

08-12-2003, 09:52 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
|
Alright this is not exactly what i want. it works tho but what if you have a thousant instances of the class. That means you all have to update the value in the classes. Not really a good way, ey??. As you can see in my solution for it(the second class1.zip in my posts). That no matter how many class instances you create and as long as you init them with the sama parameter. It keeps track of the changes in the variable of the form.
I'll describe my problem here without the use of DirectX because this is no directX forum.
I have a forum and a custom made button(which is a class, don't ask me why i made it as a class and not as a usercontrol because that has to do with DirectX). The forum keeps track of the mouse but the button has to change it's background when the mouse goes over the button. First there was a type called Point with 2 variables x and y as longs in it. Because that sounded to complicated i replaced the type with just 2 long variables, which would look a bit messier but is easier to start with.
Now i put on that form for instance 100 buttons. If i use a property or a public declared variable in the buttonClass, then everytime the mouse moves i need to update the 100variables in all the classes. A better way to do it is to locate a piece of memory to store a long( to mouse x position and y position) then to pass to all the classes the pointer of that memory so that they all use the same piece of memory. When something changes it, a class or the forum or who ever, they all have the new value. This methode spares system memory, because you only need one time the length of a long in your memory instead of x(amount of buttons) times the space for a long. Secondly it spares calculating time because if you change it all the other buttons immediatly know the new value without telling them.
ChoKamir
As you see in my first post i talked about 2 problems. To solve the first one i used the 2 longs but still the second problem stays unsolved. The best way until now is with my wrapper class. The second class1.zip in this tread. The first one is the sample program.
|
|

08-12-2003, 10:06 AM
|
 |
Disillusioned Code Poet
Retired Moderator * Guru *
|
|
Join Date: Apr 2002
Location: Tennessee, USA
Posts: 12,808
|
|
|
In that case, I am confused why you are making it a property of the class at all. If the form and all the class instances need to see/use this variable, why not just make it public in a standard module?
|
__________________
Laura
Ita erat quando hic adveni.
|

08-12-2003, 10:15 AM
|
 |
Junior Contributor
|
|
Join Date: Aug 2003
Location: The Netherlands
Posts: 358
|
|
Quote: Originally Posted by lebb In that case, I am confused why you are making it a property of the class at all. If the form and all the class instances need to see/use this variable, why not just make it public in a standard module?
Uhmm... Yeah i could do that just bind the 2 in one DLL. It makes it all a lot easier really. First was my idea to have a seperact DLL for the button and another one for the initaliasing of DirectX(here replaced with the forum) but uhmm... stupid a bit i'll just put them together.
Thx m8 i think i found my solution
ChoKamir
|
|
|
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
|
|
|
|
|
|
|
|
 |
|