Solved

UDT TYPES!!!

Posted on 2001-06-07
17
380 Views
Last Modified: 2012-06-27
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 -

projectA.bas

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!


0
Comment
Question by:AA69
  • 6
  • 6
  • 2
  • +2
17 Comments
 
LVL 43

Expert Comment

by:TimCottee
Comment Utility
public function GetMessage(UDT as Variant)

The variant datatype can contain any defined datatype including UDTs.
0
 
LVL 6

Expert Comment

by:anthony_glenwright
Comment Utility
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.
0
 
LVL 2

Expert Comment

by:agriggs
Comment Utility
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


0
 
LVL 6

Expert Comment

by:anthony_glenwright
Comment Utility
Yikes!  I'd never come across the "Lset" command before... Is it similar to using the CopyMemory API?
0
 

Author Comment

by:AA69
Comment Utility
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.

0
 

Author Comment

by:AA69
Comment Utility
I forgot also that these DLLs will be running under COM+
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
0
 
LVL 15

Accepted Solution

by:
ameba earned 200 total points
Comment Utility
' I forgot this declaration in a class
Private mudtProps As ClientProps
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:AA69
Comment Utility
Thanks Ameba I will try this out and report back.

agriggs I have tried that cdoe and it errors with a type mismatch error.
0
 

Author Comment

by:AA69
Comment Utility
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.
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
AA69,

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.
0
 

Author Comment

by:AA69
Comment Utility
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?

EG

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.
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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.
redefine:
Public type Test2
  str1 as string * 10
  str2 as string * 20
End Type
to:
Public type Test2
  str1 as string
  str2 as string
End Type

I can post ActiveX code for this.
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
Note that I didn't receive notification for your last post - it's ee bug.
0
 

Author Comment

by:AA69
Comment Utility
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.
0
 
LVL 2

Expert Comment

by:agriggs
Comment Utility
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.
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
OK, I'll check CopyMemory to copy to string your UDTs in that question.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now