Pointers to Pointers?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i need to know if i can have a pointer to a pointer in VB.NET

given

Class CClass
'declarations
End Class

and then

Sub MySub()
Dim MyClass as CClass
End Sub

MyClass is a 'pointer' to a CClass object.
what i need is a pointer to a pointer...
my problem is that if i do this

Dim MyClassA as CClass
Dim MyClassB as CClass

MyClassA = Object1
MyClassB = MyClassA

it simply copies the address that MyClassA refers to, to MyClassB
i want MyClassB to point to the address of MyClassA if possible

this way whenever i repoint MyClassA at something new, MyClassB will point
at the same new object. the way it is above, if i reasign MyClassA to a new
object, MyClassB will still point at the first object. this is really easy to
do in C/C++...

heres something like what it could look like:

Dim MyClassA as CClass
Dim MyClassB as CClassPointer

MyClassA = Object1
MyClassB = MyClassA 'MyClassB points at MyClassA, instead of pointing at
what MyClassA points at

MyClassA = Object2 'now both MyClassA, and MyClassB point at the new object
 
Dave,

It makes me always curious, when I started with programming I "needed"
pointers to get values from memory. (It was really the hard places in
memory, not a virtual place given by the OS).

I can assure you that it is long ago, and I am happy that those days are
behind me, because it was as description what the program was doing nothing.

Why want people still make application programs in this style from far back
in the previous century?

Cor
 
i dont know what you mean, pointers are far from obsolete. what i describe is
easily doable in C/C++.

i am simply using the C/C++ word 'pointer'. in VB:

Dim ClassReference As MyClass

is a 'pointer' to a class.

IIRC in C/C++ it looks like

MyClass * ClassPointer;

and a Pointer to a Pointer would be easily done with

MyClass ** ClassPointerPointer;

just wondering if this is possible in VB too... or if theres a way to get a
similar effect.
 
Dave,

Back in 1830 people where riding on a horse from New York to San Francisco,
they had only one other choise and that was sailing around Cape Horn.

Probably is both still possible. However I have not the idea that it is the
fastest and the most comfortable way to do that.

It was for me not the question if it is possible, I was curious why you
would do it this way. (Going on a horse, instead of easier and more
comfortable way).

Cor
 
Dave Cousineau ( Sahuagin ) said:
i need to know if i can have a pointer to a pointer in VB.NET

given

Class CClass
End Class

Sub MySub()
Dim MyClass as CClass
End Sub

MyClass is a 'pointer' to a CClass object.

Bzzzzzzt! MyClass is a reference to an object not a 'pointer'.
There is a Pointer type in the System.Reflection namespace,
(for unmanaged code) but they are not for use in VB....

LFS
 
eh ok, i was using the term loosly...

what i am looking for then is instead of a reference to an object, a
reference to a reference

ill explain what im trying to do.

this is for a 2D game graphics engine.
basically i want a list of specific objects under settings that the game
uses, and that the user is free to reassign.

ie:

Shadow Sprite
Fire Sprite
Game Over Sound Effect

the user will choose a particular sprite or sound effect from the sprite and
sound effect lists for each item under this settings screen. then the main
part of the game can simply use 'Game Over Sound Effect' at the appropriate
time and

the problem is that i cant save any of these settings values in any other
object, because if i do it doesnt refer to the settings object, it will refer
to whatever sprite/sound object the user selected, and then if he changes it,
it wont have an effect... i want to be able to have a reference to these
object reference variables, if possible...
 
Dave Cousineau ( Sahuagin ) said:
this is for a 2D game graphics engine.
basically i want a list of specific objects under settings that the game
uses, and that the user is free to reassign.

ie:

Shadow Sprite
Fire Sprite
Game Over Sound Effect

the user will choose a particular sprite or sound effect from the sprite and
sound effect lists for each item under this settings screen. then the main
part of the game can simply use 'Game Over Sound Effect' at the appropriate
time and

the problem is that i cant save any of these settings values in any other
object, because if i do it doesnt refer to the settings object, it will refer
to whatever sprite/sound object the user selected, and then if he changes it,
it wont have an effect... i want to be able to have a reference to these
object reference variables, if possible...


You lost me at the part about 'then if he changes it'.

He selected the effect in the first place, and you are using that, so you have
a means to use what the user selects. If he selects another effect he has to
be on the settings screen as before, does ne not? Where can he make a change
in what he selects other than on the settings screen (which you are already
using)?

If it were me, I'd create a settings class and let the user alter its property
values. Any code that needs to use one of the values gets it from the
settings class. If the user changes a value, it goes into effect on next use
(because the code calls the same property he just changed....)

I just don't see a need for such indirection for user options. As long as
you use the same property the users sets, how are you going to get out
of sync?

LFS
 
heres an example:

all physical game objects will have a 'shadow sprite' object reference.
instead of the user (the user of the game design software not the game
itself) going to each new game object he creates and selecting a sprite for
the shadow there, he just goes into a settings form, and theres a setting for
'shadow sprite'. all game objects would now have a 'shadow sprite' member
that is a reference to a reference, which refers to the reference he selected
under the settings form.

is this taboo or something? its a simple question that seems like it could
easily be answered by just saying 'no there isnt'.
 
Dave said:
heres an example:

all physical game objects will have a 'shadow sprite' object reference.
instead of the user (the user of the game design software not the game
itself) going to each new game object he creates and selecting a sprite for
the shadow there, he just goes into a settings form, and theres a setting for
'shadow sprite'. all game objects would now have a 'shadow sprite' member
that is a reference to a reference, which refers to the reference he selected
under the settings form.

is this taboo or something? its a simple question that seems like it could
easily be answered by just saying 'no there isnt'.

pseudo code:

class settings
public shadowsprite
end class


class gameobject
dim settings as settings
end class

Something like this?

Armin
 
Pointers aren't allowed in VB. There are ways to access memory locations
directly, however, using unsafe code and special methods. These aren't
needed very often, however. The only time I've ever had use for them in VB
is when the API calls are just way too slow (like when manipulating
graphics).

What you want sounds more like what Larry proposed - a class that defines
your property values. If you are really against this idea, you can store
the properties as objects in a HashTable or SortedList or something:

Class ShadowSprite
' define properties/methods, etc.
End Class

Class GameObject
Public Settings As New HashTable
End Class

Dim GO As New GameObject
Dim Sp As New ShadowSprite

GO.Settings.Add("ShadowSprite", Sp)

It sounds like you might want to serialize this, so you'll want to keep that
in mind when you create your classes.

"Dave Cousineau ( Sahuagin )"
 
Dave Cousineau ( Sahuagin ) said:
all physical game objects will have a 'shadow sprite' object reference.
instead of the user (the user of the game design software not the game
itself) going to each new game object he creates and selecting a sprite for
the shadow there, he just goes into a settings form, and theres a setting for
'shadow sprite'. all game objects would now have a 'shadow sprite' member
that is a reference to a reference, which refers to the reference he selected
under the settings form.

is this taboo or something? its a simple question that seems like it could
easily be answered by just saying 'no there isnt'.

I believe it was aready mentioned that VB has no pointers....

But it appears you are complicating the situation. If you have a settings class,
and want to provide a default property for all objects, then (depending on who
you want to give priority to) have your objects check the settings class before
making use of any value, such as:

Public Class GameObject

Private mShadowSprite As Object

Public Property ShadowSprite() As Object
Get
If mShadowSprite Is Nothing Then
Return Settings.ShadowSprite
Else
Return mShadowSprite
End If
End Get

Set(ByVal value As Object)
mShadowSprite = value
End Set

End Property

End Class


Now, anytime GameObject.ShadowSprite is used, a check is made
to see if it has its own sprite, or if not, it uses the one set by the user.

In that instance, each object's sprite would override the default value.
You could make it the other way, where the default value overrides the
local value, or, some combination (where a checkbox indicates who gets
priority, etc.)

In any case, a reference to a reference is not needed, and, don't you
agree, just looking at the explaination of it, sounds overly complicated?

LFS
 
Back
Top