Solved

UDT TYPES!!!

Posted on 2001-06-07
17
381 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
ID: 6163848
public function GetMessage(UDT as Variant)

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

Expert Comment

by:anthony_glenwright
ID: 6163888
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
ID: 6163945
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
ID: 6163957
Yikes!  I'd never come across the "Lset" command before... Is it similar to using the CopyMemory API?
0
 

Author Comment

by:AA69
ID: 6164548
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
ID: 6164552
I forgot also that these DLLs will be running under COM+
0
 
LVL 15

Expert Comment

by:ameba
ID: 6164787
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
ID: 6164798
' I forgot this declaration in a class
Private mudtProps As ClientProps
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:AA69
ID: 6167103
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
ID: 6185497
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
ID: 6185646
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
ID: 6202595
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
ID: 6206494
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
ID: 6206506
Note that I didn't receive notification for your last post - it's ee bug.
0
 

Author Comment

by:AA69
ID: 6206590
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
ID: 6206591
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
ID: 6206692
OK, I'll check CopyMemory to copy to string your UDTs in that question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now