Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Old May 17th 18, 02:01 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Nov 2014
Posts: 15
Default Problem getting a pointer to a User Defined Type (UDT)

I have a data structure consisting of an array of User Defined Types (UDF) - called Udta say. Each Udta contains 4 members that are also UDT's - called Udtb say. I wrote a function to return a pointer to a specific Udtb. But to my surprise, the pointer creates a new instance of Udtb although it inherits from the Udtb I was trying to point to.

I have confirmed this behaviour by monitoring the variables in Local Window.. I also display the pointer values using VarPtr.

My question. Is it possible to get a pointer to a specific Udtb as I am trying to do. It seems to me that I want 2 pointers pointing at the same UDF. I would prefer to stay with UDT's and not use classes.

The code is vba in Excel 2010.

Thank you

  #2   Report Post  
Old May 17th 18, 03:02 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Jun 2017
Posts: 7
Default Problem getting a pointer to a User Defined Type (UDT)

Am 17.05.2018 um 15:01 schrieb Desmond Walsh:
I have a data structure consisting of an array of User Defined Types (UDF) - called Udta say. Each Udta contains 4 members that are also UDT's - called Udtb say. I wrote a function to return a pointer to a specific Udtb. But to my surprise, the pointer creates a new instance of Udtb although it inherits from the Udtb I was trying to point to.

I have confirmed this behaviour by monitoring the variables in Local Window. I also display the pointer values using VarPtr.

My question. Is it possible to get a pointer to a specific Udtb as I am trying to do. It seems to me that I want 2 pointers pointing at the same UDF. I would prefer to stay with UDT's and not use classes.

The code is vba in Excel 2010.


Here you will find some detailed information about vba pointers:
https://bytecomb.com/vba-internals-a...ters-in-depth/
https://communities.bentley.com/prod...roperty-values

Therefore i would prefer objects instead udts and* and pass them by
reference.

Ulrich

  #3   Report Post  
Old May 18th 18, 12:28 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Nov 2014
Posts: 15
Default Problem getting a pointer to a User Defined Type (UDT)

On Thursday, May 17, 2018 at 10:03:03 AM UTC-4, Ulrich Möller wrote:
Am 17.05.2018 um 15:01 schrieb Desmond Walsh:
I have a data structure consisting of an array of User Defined Types (UDF) - called Udta say. Each Udta contains 4 members that are also UDT's - called Udtb say. I wrote a function to return a pointer to a specific Udtb. But to my surprise, the pointer creates a new instance of Udtb although it inherits from the Udtb I was trying to point to.

I have confirmed this behaviour by monitoring the variables in Local Window. I also display the pointer values using VarPtr.

My question. Is it possible to get a pointer to a specific Udtb as I am trying to do. It seems to me that I want 2 pointers pointing at the same UDF. I would prefer to stay with UDT's and not use classes.

The code is vba in Excel 2010.


Here you will find some detailed information about vba pointers:
https://bytecomb.com/vba-internals-a...ters-in-depth/
https://communities.bentley.com/prod...roperty-values

Therefore i would prefer objects instead udts and* and pass them by
reference.

Ulrich


Thank you for the links. I am now convinced that a UDT design cannot easily support what I was trying to do.

I converted my Utdb to a class. My GetPointer function returns a Variant which I change to a class pointer in the calling procedure. Everything now works as I intended.

I will post some sample code illustrating the UTD problem and the simple conversion to a class based solution

Desmond

  #4   Report Post  
Old May 18th 18, 12:59 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Jun 2017
Posts: 7
Default Problem getting a pointer to a User Defined Type (UDT)

Am 18.05.2018 um 13:28 schrieb Desmond Walsh:
On Thursday, May 17, 2018 at 10:03:03 AM UTC-4, Ulrich Möller wrote:
Am 17.05.2018 um 15:01 schrieb Desmond Walsh:
I have a data structure consisting of an array of User Defined Types (UDF) - called Udta say. Each Udta contains 4 members that are also UDT's - called Udtb say. I wrote a function to return a pointer to a specific Udtb. But to my surprise, the pointer creates a new instance of Udtb although it inherits from the Udtb I was trying to point to.

I have confirmed this behaviour by monitoring the variables in Local Window. I also display the pointer values using VarPtr.

My question. Is it possible to get a pointer to a specific Udtb as I am trying to do. It seems to me that I want 2 pointers pointing at the same UDF. I would prefer to stay with UDT's and not use classes.

The code is vba in Excel 2010.

Here you will find some detailed information about vba pointers:
https://bytecomb.com/vba-internals-a...ters-in-depth/
https://communities.bentley.com/prod...roperty-values

Therefore i would prefer objects instead udts and* and pass them by
reference.

Ulrich

Thank you for the links. I am now convinced that a UDT design cannot easily support what I was trying to do.

I converted my Utdb to a class. My GetPointer function returns a Variant which I change to a class pointer in the calling procedure. Everything now works as I intended.

I will post some sample code illustrating the UTD problem and the simple conversion to a class based solution


Another helpful link may be
https://www.vitoshacademy.com/vba-at...gh-end-in-vba/
The interesting thing is how to create a static class in vba

Ulrich


  #5   Report Post  
Old May 19th 18, 09:31 PM posted to microsoft.public.excel.programming
external usenet poster
 
First recorded activity by ExcelBanter: Nov 2014
Posts: 15
Default Problem getting a pointer to a User Defined Type (UDT)

As I mentioned in my second post, switching the member UDT (UDTb) to a class (Classb) was very simple and gave me the pointer that I wanted
Here is test code that I wrote to illustrate the problem ;
--------------------------------------------------
================================================== ================================================== ====================
Name: Module1
================================================== ================================================== ====================
Option Explicit
' Module 1 TestUdtClass
'
' Test code to demonstrate a difference between a User Defined Type(UDT) and a Class
'
'-------------------------------------------------------------------------------------------
Public Type Udtb
bmember1 As Integer
bmember2 As Integer
End Type

Public Type Udta_v1
' Some members are references to other UDT's
index As Integer
amember1 As Udtb
amember2 As Udtb
End Type

Public Type Udta_v2
' Identical to Udta_v1 with UDT members replaced by Class members
index As Integer
amember1 As Classb
amember2 As Classb
End Type

Public Sub UsingUdt()
'
' Attempting to get a pointer to a UDT one layer down in the data structure
' The data structure is an array of UDT's with some members also UDT's (different UDT)

Dim marray() As Udta_v1
Dim udtb_pointer As Udtb

ReDim marray(1 To 2) As Udta_v1

Debug.Print vbNewLine & "USINGUDT Data structures use UDT's only"
marray(2).index = 2
marray(2).amember1.bmember1 = 111
marray(2).amember1.bmember2 = 122

udtb_pointer = GetUdtPointer(2, marray)
'
' Display contents of Udtb
Debug.Print "USINGUDT Using marray bmember1: " & marray(2).amember1.bmember1
Debug.Print "USINGUDT Using marray bmember2: " & marray(2).amember1.bmember2
Debug.Print vbNewLine

Debug.Print "USINGUDT Using GetUdtPointer bmember1: " & udtb_pointer.bmember1
Debug.Print "USINGUDT Using GetUdtPointer bmember2: " & udtb_pointer.bmember2

End Sub

Public Function GetUdtPointer(i As Integer, marray() As Udta_v1) As Udtb

' Intent was to get a pointer to a specific Udta. But the debug statements clearly show that
' a new instance of Udtb was created which inherited data from the targetted Utdb
' The function GetClassPointer is equivalent code for the Udta_v2 data structure
' NOTE: Use Local Window in debug mode to see the data structures being updated

GetUdtPointer = marray(2).amember1
'
' Now have two Udt pointers. Add some data
GetUdtPointer.bmember2 = 277
marray(2).amember1.bmember2 = 299

'
' Display contents of Udtb
Debug.Print "GETUDTPOINTER Using marray Varptr: " & VarPtr(marray(2).amember1)
Debug.Print "GETUDTPOINTER Using marray bmember2: " & marray(2).amember1.bmember1
Debug.Print "GETUDTPOINTER Using marray bmember2: " & marray(2).amember1.bmember2
Debug.Print vbNewLine

Debug.Print "GETUDTPOINTER Using GetUdtPointer Varptr: " & VarPtr(GetUdtPointer)
Debug.Print "GETUDTPOINTER Using GetUdtPointer bmember2: " & GetUdtPointer.bmember1
Debug.Print "GETUDTPOINTER Using GetUdtPointer bmember2: " & GetUdtPointer.bmember2
Debug.Print vbNewLine
End Function

Public Sub UsingClass()
'
' Attempting to get a pointer to second layer elements in the data structure
' The data structure is an array of UDT's with some members defined as instances of a class (Classb)


Dim marray() As Udta_v2
Dim classb_pointer As Classb

ReDim marray(1 To 2) As Udta_v2

Debug.Print vbNewLine & "USINGCLASS Data structures use UDT's and Classes"
marray(2).index = 2
'
' Because we are now using classes, must first create a class instance
Set marray(2).amember1 = New Classb
marray(2).amember1.bmember1 = 111

Set marray(2).amember2 = New Classb
marray(2).amember1.bmember2 = 122

Set classb_pointer = GetClassPointer(2, marray)
'
' Display contents of Udtb
Debug.Print "USINGCLASS Using marray bmember1: " & marray(2).amember1.bmember1
Debug.Print "USINGCLASS Using marray bmember2: " & marray(2).amember1.bmember2
Debug.Print vbNewLine


Debug.Print "USINGCLASS Using GetClassPointer bmember1: " & classb_pointer.bmember1
Debug.Print "USINGCLASS Using GetClassPointer bmember2: " & classb_pointer.bmember2

End Sub

Public Function GetClassPointer(i As Integer, marray() As Udta_v2) As Variant
'
' This code is identical to GetUdtPointer except for
' 1 Function name is GetClassPointer instead of GetUdtPointer
' 2 Marray defined as Udta_v2 instead of Udta_v1
' 3 Use Set keyword to define new pointer
' 4 Function returns a Variant
' NOTE: Use Local Window in debug mode to see the data structures being updated

Set GetClassPointer = marray(2).amember1
'
' Now have two pointers. Add some data
GetClassPointer.bmember2 = 277
marray(2).amember1.bmember2 = 299

'
' Display contents of Udtb
Debug.Print "GETCLASSPOINTER Using marray Objptr: " & ObjPtr(marray(2).amember1)
Debug.Print "GETCLASSPOINTER Using marray Varptr: " & VarPtr(marray(2).amember1)
Debug.Print "GETCLASSPOINTER Using marray bmember2: " & marray(2).amember1.bmember1
Debug.Print "GETCLASSPOINTER Using marray bmember2: " & marray(2).amember1.bmember2
Debug.Print vbNewLine

Debug.Print "GETCLASSPOINTER Using GetClassPointer Objptr: " & ObjPtr(GetClassPointer)
Debug.Print "GETCLASSPOINTER Using GetClassPointer Varptr: " & VarPtr(GetClassPointer)
Debug.Print "GETCLASSPOINTER Using GetClassPointer bmember2: " & GetClassPointer.bmember1
Debug.Print "GETCLASSPOINTER Using GetClassPointer bmember2: " & GetClassPointer.bmember2
Debug.Print vbNewLine
End Function


================================================== ================================================== ====================
Name: Classb
================================================== ================================================== ====================
' Class Classb
'
' The members of this class are identical to the UDT Udtb
' No methods are defined for this class
'
'-------------------------------------------------------------------------------------------
Public bmember1 As Integer
Public bmember2 As Integer
-----------------------------------------------------------

Here is the output ;
USINGUDT Data structures use UDT's only
GETUDTPOINTER Using marray Varptr: 2152204
GETUDTPOINTER Using marray bmember2: 111
GETUDTPOINTER Using marray bmember2: 299


GETUDTPOINTER Using GetUdtPointer Varptr: 112225048
GETUDTPOINTER Using GetUdtPointer bmember2: 111
GETUDTPOINTER Using GetUdtPointer bmember2: 277


USINGUDT Using marray bmember1: 111
USINGUDT Using marray bmember2: 299


USINGUDT Using GetUdtPointer bmember1: 111
USINGUDT Using GetUdtPointer bmember2: 277

USINGCLASS Data structures use UDT's and Classes
GETCLASSPOINTER Using marray Objptr: 162841984
GETCLASSPOINTER Using marray Varptr: 138064736
GETCLASSPOINTER Using marray bmember2: 111
GETCLASSPOINTER Using marray bmember2: 299


GETCLASSPOINTER Using GetClassPointer Objptr: 162841984
GETCLASSPOINTER Using GetClassPointer Varptr: 112225000
GETCLASSPOINTER Using GetClassPointer bmember2: 111
GETCLASSPOINTER Using GetClassPointer bmember2: 299


USINGCLASS Using marray bmember1: 111
USINGCLASS Using marray bmember2: 299


USINGCLASS Using GetClassPointer bmember1: 111
USINGCLASS Using GetClassPointer bmember2: 299

------------------------------------------------------------------------
Meeting this kind of problem is intimidating. Excel cannot detect that this coding is not what the author intended. Only a close examination of the data will reveal unintended behaviour.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compile error: User-defined type not defined Ayo Excel Programming 3 April 23rd 09 07:42 PM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
Workspace faux user-defined type not defined Chris S[_2_] Excel Programming 3 November 11th 04 05:51 PM
User-defined data type; Error: Only User-defined types... tiger_PRM Excel Programming 1 July 18th 04 03:32 PM
User defined type problem R Avery Excel Programming 1 May 26th 04 02:41 PM


All times are GMT +1. The time now is 05:25 AM.

Powered by vBulletin® Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright 2004-2018 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"

 

Copyright © 2017