473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

VarPtr() Fun

I have a couple of questions about the VarPtr() function of VBA. I
have created my own class, lets call it MyObject and in one of my
modules, I have declared an array of type MyObject. In my array, i
wish to store one or more references to the same instantiation of a
MyObject object. So, I have something like this:

Dim myArray(10) As MyObject
Dim newObject as MyObject

Set newObject = new MyObject

Set myArray(0) = newObject
Set myArray(1) = newObject

As a quick and dirty way of checking if different elements of the array
are referencing the same object, I thought I could use VarPtr like
such:

if(VarPtr(myArray(0)) = VarPtr(myArray(1))) then debug.print "Objects
equal"

However, in this instance, VarPtr seems to be returning the address of
the myArray variable and not the address of the MyObject that was
stored in the array. How do I retrieve the address of the MyObject
that was stored in the array? Or, is this a hopless endeavor better
suited to C programming? Thanks for your help.

-Vincent

Jul 6 '06 #1
5 6617
On 5 Jul 2006 18:36:13 -0700, "Vincent" <an**********@verizon.net>
wrote:

I would probably check the value of a unique property. Something that
in a table would get a unique index:
myArray(0).CustomerID = myArray(1).CustomerID
That way, I don't have to know and worry about VBA memory management.

-Tom.

>I have a couple of questions about the VarPtr() function of VBA. I
have created my own class, lets call it MyObject and in one of my
modules, I have declared an array of type MyObject. In my array, i
wish to store one or more references to the same instantiation of a
MyObject object. So, I have something like this:

Dim myArray(10) As MyObject
Dim newObject as MyObject

Set newObject = new MyObject

Set myArray(0) = newObject
Set myArray(1) = newObject

As a quick and dirty way of checking if different elements of the array
are referencing the same object, I thought I could use VarPtr like
such:

if(VarPtr(myArray(0)) = VarPtr(myArray(1))) then debug.print "Objects
equal"

However, in this instance, VarPtr seems to be returning the address of
the myArray variable and not the address of the MyObject that was
stored in the array. How do I retrieve the address of the MyObject
that was stored in the array? Or, is this a hopless endeavor better
suited to C programming? Thanks for your help.

-Vincent
Jul 6 '06 #2
That is what the IS operator is for:

If MyArray(0) IS MyArray(1) then debug.print "equal"

As you have seen, VarPtr returns a pointer to the
variable, not the value of the variable.

(david)
"Vincent" <an**********@verizon.netwrote in message
news:11**********************@v61g2000cwv.googlegr oups.com...
>I have a couple of questions about the VarPtr() function of VBA. I
have created my own class, lets call it MyObject and in one of my
modules, I have declared an array of type MyObject. In my array, i
wish to store one or more references to the same instantiation of a
MyObject object. So, I have something like this:

Dim myArray(10) As MyObject
Dim newObject as MyObject

Set newObject = new MyObject

Set myArray(0) = newObject
Set myArray(1) = newObject

As a quick and dirty way of checking if different elements of the array
are referencing the same object, I thought I could use VarPtr like
such:

if(VarPtr(myArray(0)) = VarPtr(myArray(1))) then debug.print "Objects
equal"

However, in this instance, VarPtr seems to be returning the address of
the myArray variable and not the address of the MyObject that was
stored in the array. How do I retrieve the address of the MyObject
that was stored in the array? Or, is this a hopless endeavor better
suited to C programming? Thanks for your help.

-Vincent

Jul 6 '06 #3
That makes sense. Thanks David, I will give that a try.

-Vincent

david epsom dot com dot au wrote:
That is what the IS operator is for:

If MyArray(0) IS MyArray(1) then debug.print "equal"

As you have seen, VarPtr returns a pointer to the
variable, not the value of the variable.

(david)
"Vincent" <an**********@verizon.netwrote in message
news:11**********************@v61g2000cwv.googlegr oups.com...
I have a couple of questions about the VarPtr() function of VBA. I
have created my own class, lets call it MyObject and in one of my
modules, I have declared an array of type MyObject. In my array, i
wish to store one or more references to the same instantiation of a
MyObject object. So, I have something like this:

Dim myArray(10) As MyObject
Dim newObject as MyObject

Set newObject = new MyObject

Set myArray(0) = newObject
Set myArray(1) = newObject

As a quick and dirty way of checking if different elements of the array
are referencing the same object, I thought I could use VarPtr like
such:

if(VarPtr(myArray(0)) = VarPtr(myArray(1))) then debug.print "Objects
equal"

However, in this instance, VarPtr seems to be returning the address of
the myArray variable and not the address of the MyObject that was
stored in the array. How do I retrieve the address of the MyObject
that was stored in the array? Or, is this a hopless endeavor better
suited to C programming? Thanks for your help.

-Vincent
Jul 6 '06 #4
As david says you should use the Is operator but if you do want the pointer
to an object you should be using objptr, not varptr, and BTW the pointer to
a string variable is strptr.

so
Dim myArray(10) As MyObject
Dim newObject As MyObject

Set newObject = New MyObject

Set myArray(0) = newObject
Set myArray(1) = newObject

If ObjPtr(myArray(0)) = ObjPtr(myArray(1)) Then
Debug.Print "yep"
End If

If myArray(0) Is myArray(1) Then
Debug.Print "yep2"
End If

Set myArray(0) = Nothing
Set myArray(1) = Nothing
Set newObject = Nothing
Prints
yep
yep2

From MSDN (Q199824)
VarPtr - Returns the address of a variable.
VarPtrArray - Returns the address of an array.
StrPtr - Returns the address of the UNICODE string buffer.
VarPtrStringArray - Returns the address of an array of strings.
ObjPtr - Returns the pointer to the interface referenced by an object
variable.

It might be an idea to look at http://vb.mvps.org/hardcore/ or try and get
hold of a hard copy.
--

Terry Kreft
"Vincent" <an**********@verizon.netwrote in message
news:11**********************@v61g2000cwv.googlegr oups.com...
That makes sense. Thanks David, I will give that a try.

-Vincent

david epsom dot com dot au wrote:
That is what the IS operator is for:

If MyArray(0) IS MyArray(1) then debug.print "equal"

As you have seen, VarPtr returns a pointer to the
variable, not the value of the variable.

(david)
"Vincent" <an**********@verizon.netwrote in message
news:11**********************@v61g2000cwv.googlegr oups.com...
>I have a couple of questions about the VarPtr() function of VBA. I
have created my own class, lets call it MyObject and in one of my
modules, I have declared an array of type MyObject. In my array, i
wish to store one or more references to the same instantiation of a
MyObject object. So, I have something like this:
>
Dim myArray(10) As MyObject
Dim newObject as MyObject
>
Set newObject = new MyObject
>
Set myArray(0) = newObject
Set myArray(1) = newObject
>
As a quick and dirty way of checking if different elements of the
array
are referencing the same object, I thought I could use VarPtr like
such:
>
if(VarPtr(myArray(0)) = VarPtr(myArray(1))) then debug.print "Objects
equal"
>
However, in this instance, VarPtr seems to be returning the address of
the myArray variable and not the address of the MyObject that was
stored in the array. How do I retrieve the address of the MyObject
that was stored in the array? Or, is this a hopless endeavor better
suited to C programming? Thanks for your help.
>
-Vincent
>

Jul 6 '06 #5
Thanks Terry, I did not realize there was a separate function for what
I was trying to do.

-Vincent

Terry Kreft wrote:
As david says you should use the Is operator but if you do want the pointer
to an object you should be using objptr, not varptr, and BTW the pointer to
a string variable is strptr.

so
Dim myArray(10) As MyObject
Dim newObject As MyObject

Set newObject = New MyObject

Set myArray(0) = newObject
Set myArray(1) = newObject

If ObjPtr(myArray(0)) = ObjPtr(myArray(1)) Then
Debug.Print "yep"
End If

If myArray(0) Is myArray(1) Then
Debug.Print "yep2"
End If

Set myArray(0) = Nothing
Set myArray(1) = Nothing
Set newObject = Nothing
Prints
yep
yep2

From MSDN (Q199824)
VarPtr - Returns the address of a variable.
VarPtrArray - Returns the address of an array.
StrPtr - Returns the address of the UNICODE string buffer.
VarPtrStringArray - Returns the address of an array of strings.
ObjPtr - Returns the pointer to the interface referenced by an object
variable.

It might be an idea to look at http://vb.mvps.org/hardcore/ or try and get
hold of a hard copy.
--

Terry Kreft
"Vincent" <an**********@verizon.netwrote in message
news:11**********************@v61g2000cwv.googlegr oups.com...
That makes sense. Thanks David, I will give that a try.

-Vincent

david epsom dot com dot au wrote:
That is what the IS operator is for:
>
If MyArray(0) IS MyArray(1) then debug.print "equal"
>
As you have seen, VarPtr returns a pointer to the
variable, not the value of the variable.
>
(david)
>
>
"Vincent" <an**********@verizon.netwrote in message
news:11**********************@v61g2000cwv.googlegr oups.com...
I have a couple of questions about the VarPtr() function of VBA. I
have created my own class, lets call it MyObject and in one of my
modules, I have declared an array of type MyObject. In my array, i
wish to store one or more references to the same instantiation of a
MyObject object. So, I have something like this:

Dim myArray(10) As MyObject
Dim newObject as MyObject

Set newObject = new MyObject

Set myArray(0) = newObject
Set myArray(1) = newObject

As a quick and dirty way of checking if different elements of the
array
are referencing the same object, I thought I could use VarPtr like
such:

if(VarPtr(myArray(0)) = VarPtr(myArray(1))) then debug.print "Objects
equal"

However, in this instance, VarPtr seems to be returning the address of
the myArray variable and not the address of the MyObject that was
stored in the array. How do I retrieve the address of the MyObject
that was stored in the array? Or, is this a hopless endeavor better
suited to C programming? Thanks for your help.

-Vincent
Jul 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Harry J. Smith | last post by:
How do you tell if two variables past as arguments to a subroutine are actually the same variable? In some languages you can compare their addresses. Visual Basic doesn't have pointers so what is...
1
by: john | last post by:
Hello, I am trying to convert a project from vb6 to vb.net. The code is a sample from an sdk for Canon digital camera's. I have gotten most of the issues resolved, but there are some that I am...
1
by: Al | last post by:
Hi I am converting VB6 to VB. Net it is refereeing to DLL as follow. I am not sure how I can convert it to VB.Net statement. Declare Function Chain_DES Lib "chaindes.dll" (ByRef Data As Any,...
0
by: Adrian Game | last post by:
I have been trying to update/upgrade the VB6 wave render project that can be found at
28
by: MLH | last post by:
The largest integer A97 can deal with is 2,147,483,647, as I understand it from HELP. I would be content to represent larger integers as strings. For example, "2147483648" would suit me fine. I...
43
by: ZillionDollarSadist | last post by:
Hello, I'm working at a simple Access '97 + VB4 application, and I ran into a terrible problem: something I never modified now gives me a totally unwanted "Invalid use of null" error. It happens...
1
by: sathyp | last post by:
Public Function SetPrinterDefaultsW(ByVal sPrinterName As String, _ ByVal nPaperSize As Long, ByVal nOrientation As Long) As Boolean Dim Prn As Printer Dim hPrinter As Long Dim pd As...
1
by: DaveS | last post by:
Hi, Is there an easy way to convert the following piece of VB6 code to VS2005? myBuffer = VarPtr(lana) TIA, Dave
2
by: Jerry West | last post by:
I have the following code and was wondering what the .NET equivalent is for VarPtr & ByVal....it seems .NET also dislikes ByVal in this code. Perhaps it is now redundant? Any help appreciated! ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.