Skip to main content
gamedev.net
🔒 Locked

Pointer in VB ?!?!?

Started by Daivuk 11 replies 700+ views
Original Post
Daivuk
Daivuk
Hi there. I did a incredible Engine that load 3D max map (all in c++ ). But now I want to do my own Map editor. I use VB for this. There exist pointer in vb ?? Exemple : in c++ :
  
int *ptrInt;
  
in VB6 ???!?
  
Private *ptrInt As Integer
// ?????

  
Just know if exist pointer in VB, and if, how can I use them!! Thankx DUK engine Troa Technologies. (3d game development)
owl
owl
There are no explicit pointers on vb. There are only ''object'' types that can be pointing to ''nothing'' or to an instantiated object

Dim MyObject as Object

Set Object = new Object

Set Object = nothing

That''s the closest thing to a pointer in vb.
[size="2"]I like the Walrus best.
Daivuk
Daivuk
humm.. can I do dynamic list with this ?
And an Object is a TYPE ??

Is this can be an object ? :
Public Type typPlane
a As Double
b As Double
c As Double
d As Double
End Type

DUK engine

Troa Technologies.
(3d game development)
owl
owl
Yes, you could define a Class Module, and use it in your linked list.
[size="2"]I like the Walrus best.
Daivuk
Daivuk
hum.. thanks ))

I have seen all this in class, but dont remember. lol


DUK engine

Troa Technologies.
(3d game development)
Tri
Tri
Make sure when you call functions you use the ByRef keyword in your parameter declaration. Passing a parameter by reference is similar to using a pointer. ByVal (the default in VB.NET, maybe in VB6 too) just makes a copy of the parameter and works with that. So...

Public Sub SetPlane (ByRef thePlane As typPlane, A as Double, B as Double, C as Double, D As Double)
  thePlane.A = A
  thePlane.B = B
  thePlane.C = C
  thePlane.D = D
End Sub

Would do the exact same thing as if thePlane was a pointer to your plane object in C++.


Current Projects:
- Ariene GE (72%): Adding material library effects..


[edited by - Tri on November 29, 2002 10:54:07 PM]

[edited by - Tri on November 29, 2002 10:54:39 PM]
liquiddark
liquiddark
ByRef is default in VB 6.0

ld
No Excuses
alnite
alnite
WinAPI''s CopyMemory and friends have been widely used among VB programmers to emulate pointers. I don''t know exactly how to use them.


My compiler generates one error message: "does not compile."
Daivuk
Daivuk
But.....
If I get a dynamic list in c++, on to do the same in vb ?

My typPlane (exemple) :
in c++ :

struct typPlane
{
double a,b,c,d;
typPlane *Next;
typPlane *Previous;
};


in vb ???!!??!?
Thankx for replies.

DUK engine

Troa Technologies.
(3d game development)
alexmoura
alexmoura
You should be able to add a new class to your project named typPlane add the following members to it and use it as nodes in your linked list:

a,b,c,d as double
Next as typPlane
Previous as typPlane


Class variables in vb allways work as if they are pointers (i.e., assignement means getting the variable to point to whatever it''s being assigned to, instead of copying the contents).

You know, I never wanted to be a programmer...

Alexandre Moura
owl
owl
You can't point to a Type (struct) in VB.
This is a little example on how to do it with classes:

1. Start a new Standar EXE Project.
2. Go to the Project menu, click 'Add Class Module'. Click Open.
3. On the properties window, change the name field From Class1 to CPlane.

4. Add the following code to the CPlane (code) window

      
Option Explicit

Public a As Double, b As Double, c As Double, d As Double
Public pNext As CPlane
Public pPrev As CPlane


5. Add the folowing code to the Form (code) window:

  
Option Explicit

' Pointers to first and last plane in the list
Dim pFirst As CPlane
Dim pLast As CPlane
' The planes list
Dim PlaneList As CPlane

Private Sub Form_Load()
Dim i As Long ' silly counter
Dim tmpPlane As CPlane ' Temporary pointer to store the new plane while creating

Set PlaneList = New CPlane ' Instantiate the new CPlane object
With PlaneList ' Fill up the members, you should use Properties instead of public members
.a = i * 1.234
.b = i * 5.678
.c = i * 9.012
.d = i * 3.456
End With
Set pFirst = PlaneList ' Set the pointer to the first plane in the list

For i = 1 To 10 ' Crate 10 new elements for the list
Set tmpPlane = New CPlane ' New plane

With tmpPlane ' Fill data. The With statement is used to keep the reference to the object for every time you call it's members. You must use it to do good coding
.a = i * 1.234
.b = i * 5.678
.c = i * 9.012
.d = i * 3.456
Set .pPrev = PlaneList
End With
Set PlaneList.pNext = tmpPlane ' You know
Set PlaneList = tmpPlane
Next

Set pLast = PlaneList ' Set the pointer to the last element

Set PlaneList = pFirst
For i = 1 To 10 ' Loop the list and show the a member value
MsgBox CStr(PlaneList.a)
Set PlaneList = PlaneList.pNext
Next
Set PlaneList = pFirst
End Sub


6. Run it, it should work.

7. Try to understand the code.

8. Learn.

Et Voilà, I hope this helps.

EDIT: OH! I forgot!! Set every pointer to Nothing after you are done, or your objects will fill your memory every time you run the program and they'll live into your memory until you reset the computer.

Set pPointer = Nothing ' Like delete o release in c++

[edited by - xaxa on November 30, 2002 7:11:37 PM]
[size="2"]I like the Walrus best.
Daivuk
Daivuk
oh thankx. That''s a help


DUK engine

Troa Technologies.
(3d game development)
ragonastick
ragonastick
For using pointers in VB, there are a couple of things you need:

1) VarPtr, ObjPtr, StrPtr: All these (undocumented) functions will return pointers to stuff. A pointer will fit inside a Long.

Eg:

Dim MyVariable As Whatever
Dim Pointer As Long

Pointer = VarPtr(MyVariable)

Usually, you only need VarPtr, so use it unless you know what you are doing.

This pointer you have is fairly useless unless you know how to get stuff out of it and in VB, there isn''t a native way of doing this. Enter CopyMemory. This is an API call (look it up yourself) which will copy a chunk of memory from one spot to another:

Function GetValueFromPointer(ByVal Pointer As Long) As Whatever
CopyMemory GetValueFromPointer, ByVal Pointer, LenB(GetValueFromPointer)
End Function

Will do the trick.

You can also mess around with the header of an array so you can use the array to access memory. For more info, look up the SAFEARRAY structure.

Before you do any of this stuff, make sure you understand all about ByVal and ByRef and where they are used in VB.

Trying is the first step towards failure.
Trying is the first step towards failure.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.