Can anyone tell me the how i can do the following:-

I want to write a general piece of code that will be using UDT's.
Something like this -


Type udtTestA
   strA1dummy as String * 5
   strA2dummy as String * 6
end type

Type udtTestB
   strB1dummy as String * 3
   strB2dummy as String * 9
end type

Separate cls or bas file  

call GetMessage(udtTestA)

public function GetMessage(UDT as "any UDT I like")

end function

I know that in C using structures that this would be straight forward as you would use a buffer of type string and pass the length of that string.

This is driving me mad!

Who is Participating?
' I forgot this declaration in a class
Private mudtProps As ClientProps
TimCotteeHead of Software ServicesCommented:
public function GetMessage(UDT as Variant)

The variant datatype can contain any defined datatype including UDTs.
Either that, or define a simple class instead of a UDT.

Create class "udtTestA"

Private strAiDummy as string

Public Property Let AIDummy(AIDummy as string)
  strAiDummy = AIDummy
End Property

Public Property Get AIDummy() as string
  AIDummy = strAiDummy
End Property

Then, you can make the class public not creatable, (or multiuse) and can share it & pass the structure between projects.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

You may want to try using the lset statement to transfer the data in the UDT to a string.

dim udtA as udtTestA
dim aString as string

'I think you must make sure the string is the right
'length before you do the assignment
aString = Space$(Len(udtA))

lset aString = udtA

Yikes!  I'd never come across the "Lset" command before... Is it similar to using the CopyMemory API?
AA69Author Commented:
A bit more info might help.

I am receiving a data structure from an MQSeries queue which in essence is a C structure (ie  a fixed length string) this is then pulled from the queue using a
getmessagefromQ function. As above. However I want this function to be a general purpose function that will take any UDT and stick the data from the message in it.
Hence the need for

public function GetMessage(UDT as "any UDT defined in the .bas file")

When I call this function I will know which UDT I have eg UDT1 I then call my function with the parameter UDT1.

call function GetMessage(UDT1)

If "Public function GetMessage(UDT as variant)" is used VB errors with "You cannot coerce a UDT into a variant".

"Any UDT I like" will be one of any number of Publicly defined UDT's in a .bas module.

AA69Author Commented:
I forgot also that these DLLs will be running under COM+
Hi, maybe you can check what Rockford Lhotka uses.

' in his class he receives a string and converts it to UDT
Private Function GetState() As String
  Dim udtData As ClientData
  LSet udtData = mudtProps
  GetState = udtData.Buffer
End Function

Private Sub SetState(Buffer As String)
  Dim udtData As ClientData
  udtData.Buffer = Buffer
  LSet mudtProps = udtData
End Sub

' in a bas module
Public Type ClientProps
  IsNew As Boolean
  IsDirty As Boolean
  IsDeleted As Boolean
  ID As Long
  Name As String * 50
  ContactName As String * 50
  Phone As String * 25
End Type

Public Type ClientData
  Buffer As String * 131
End Type
AA69Author Commented:
Thanks Ameba I will try this out and report back.

agriggs I have tried that cdoe and it errors with a type mismatch error.
AA69Author Commented:
Hi ameba,

Thanks for the answer it was just what we needed. With lots of detail thanks very much. I really should learn to post to EE more quickly it would have saved me a lot of time and probably stopped me from looking stupid with my peers.

Hi Agriggs you were sort of there as well if you want some points i'll post up a question for some points post back and let me know.

Thanks for the points. You can pass those 'packed' strings between components in VB6.

But that technique won't work in VB.NET. From vb.oop newsgroup on devx:
"LSet is being replaced by LPad function ....which only works with strings and not with UDTs"
And VB6 UDT's are contiguous blocks of memory - while new Structures are not (they are objects)

Note that in VB.NET Mr. Lhotka is using XML to pass data between components/machines.
AA69Author Commented:
Hello Again I have a problem.

If we have a public UDT that contains an array of UDT's then lset gives us a Type mismatch error at compile time.

I assume this is because it cannot guarantee the data types will match across the arrays?


Public type Test2
   str1 as string *10
   str2 as string *20
End Type

Public Type TEST1
   str1 as string *  10
   udtTest2(10) as Test2
End Type

Please Can someone help.
Hi, if you can redefine UDTs to NOT use fixed length srings, you can convert (coerce) UDTs to Variants and pass Variants between your objects.
Public type Test2
  str1 as string * 10
  str2 as string * 20
End Type
Public type Test2
  str1 as string
  str2 as string
End Type

I can post ActiveX code for this.
Note that I didn't receive notification for your last post - it's ee bug.
AA69Author Commented:
Hi I can't redefine the UDT's not to use fixed length strings as they represent C structures on another machine.

I have posted another question under the Title
"UDT Type Again!!!" so if you can give an answer to us you can have another 2000 points.
I hadn't heard of Lset either, until I read Rockford Lhotka's book.  I knew he had a pretty good trick for that, I just didn't have a copy handy to get the exact code out of.
OK, I'll check CopyMemory to copy to string your UDTs in that question.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.