Solved

UDT TYPES AGAIN!!!!!!!

Posted on 2001-06-19
7
337 Views
Last Modified: 2008-02-01
Hi,

I have a problem with UDT's. The problem is mainly trying to pass them
around functions as parameters, when the functions receiving them as
parameters need to be generic. The UDTs are required because they represent
C Structures on another UNIX type platform.

Some Code........

In a public .bas file

Public Type TEST1
    str1 as String *10
    str2 as String *20
End Type

Public Type TEST2
    str1 as String *10
    str2 as TEST1
End Type

Public Type TEST4
    str1 as String *10
    str2(21) as TEST2
End Type

In a class module that needs to pass the UDTs around as parameters to other
functions.

Private Type TEST3
    strBuffer as string * len(TEST2) ' I know you can't do this in code but
its here for illustration purposes
End Type

Private Type TEST5
    strBuffer as string * len(TEST4) ' I know you can't do this in code but
its here for illustration purposes
End Type

I can use Lset to do the following
TEST3.strBuffer = "A constant string that represents data in a passed C
structure the length and format of UDT"

Lset TEST4=TEST2
This stuffs TEST2 into TEST4 thereby populating TEST4 with the data in TEST2
in the required format.

However I can't use Lset to do this......

TEST5.strBuffer = "A constant string that represents data in a passed C
structure the length and format of UDT"

Lset TEST4=TEST5

This is supposed to stuff TEST5 into TEST4 thereby populating TEST4 with the
data in TEST2 in the required format.
The problem is that because TEST5 contains an array of UDT's this causes VB
to poke up a type mismatch error.

I can pass around TEST3.strBuffer but not a specific UDT because of the
following

Public Function Test6(strIN as string)

    ' This allows me to pass in any string of any length

End Function

Public Function Test7(udtIN as TEST4)

    ' This allows me to pass in a udt of a predefined specific type
   ' eg TEST4 and doesn't allow me to pass any UDT into this function

End Function

Is there any other way I can do this???

A very fustrated,

Andy
0
Comment
Question by:AA69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:RodStephens
ID: 6206317
If your goal is to pass various objects to a function written in C/C++, you may be able to get there using the Declare statement.

> Lset TEST4=TEST5

If you just want to pass the data in TEST5 into a function that expects a character pointer to the data, declare the function to take a TEST5 object as a parameter. When you call the routine, the address of the TEST5 is passed into the function. It only knows it has received a pointer, not what kind of thing it points to.

> ' eg TEST4 and doesn't allow me to pass any UDT into
> this function

If you want to be able to pass any UDT into a function, either:

1. Try declaring the argument as Any.
2. Make multiple declarations for the function.

Public Declare Function AbortDoc Lib "gdi32" Alias "AbortDoc" (ByVal hdc As Long) As Long

Public Declare Function Whatever1 Lib "C:\test.dll" _
    Alias "Whatever" (ByVal value As TEST1) As Long
Public Declare Function Whatever2 Lib "C:\test.dll" _
    Alias "Whatever" (ByVal value As TEST2) As Long

Then use the right version of the function depending on the argument you want to pass it.
0
 
LVL 15

Accepted Solution

by:
ameba earned 200 total points
ID: 6206857
Instead of LSet you can use CopyMemory to convert UDT to string
Ref.: http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=325
http://www.vb2themax.com/Item.asp?PageID=TipBank&ID=350

' bas module -----------------------------------------------------
Option Explicit
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Type TEST1
   str1 As String * 10
   str2 As String * 20
End Type

Public Type TEST2
   str1 As String * 10
   udtTest1 As TEST1
End Type

Public Type TEST4
   str1 As String * 10
   udtTest2(0 To 10) As TEST2
End Type


' Form1 -----------------------------------------------------
Option Explicit
Private m_complex As TEST4

Private Sub Form_Click()
    Dim s As String ' will hold contents of m_complex
    Dim i As Long
   
    Cls
    Print "COMPLEX CASE ------------------------------------------------"
   
    ' fill udt with some data
    m_complex.str1 = "Mike"
    For i = 0 To 10
        m_complex.udtTest2(i).str1 = "u2_str1_" & i
        m_complex.udtTest2(i).udtTest1.str1 = "u1_str1_" & i
        m_complex.udtTest2(i).udtTest1.str2 = "u1_str2_" & i
    Next

    ' convert udt to string
    s = GetStringFromUDT()
   
    ' convert Variant to udt
    ' empty udt (this step is not needed)
    Dim empty_udt As TEST4
    m_complex = empty_udt
   
    Call ReadStringAndFillUDT(s)  ' convert string to udt
   
    ' check result
    Print m_complex.str1
    For i = 0 To 10
        Print "  " & m_complex.udtTest2(i).str1
        Print "     " & m_complex.udtTest2(i).udtTest1.str1
        Print "     " & m_complex.udtTest2(i).udtTest1.str2
    Next
End Sub

' convert m_complex to string
Public Function GetStringFromUDT() As String
    Dim bytes As Long ' the number of bytes needed to hold udt contents
    bytes = LenB(m_complex)
   
    GetStringFromUDT = Space$((bytes + 1) \ 2) ' make a buffer long enough to hold the UDT
    ' copy the UDTs into the string
    CopyMemory ByVal StrPtr(GetStringFromUDT), ByVal VarPtr(m_complex), bytes
End Function

' convert string to m_complex
Public Sub ReadStringAndFillUDT(ByVal buffer As String)
    Dim bytes As Long ' the number of bytes needed to hold udt contents
    bytes = LenB(m_complex)
   
    ' copy the string into the UDTs
    CopyMemory ByVal VarPtr(m_complex), ByVal StrPtr(buffer), bytes
End Sub
0
 
LVL 15

Expert Comment

by:ameba
ID: 6206946
>"UDT TYPES AGAIN!!!!!!!"
Here is the link to previous question "UDT TYPES!!!" (20 pts)
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20131345
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:AA69
ID: 6206960
Good point I should have put that link in I'll remember next time. I'll try that code and report back again.
0
 

Author Comment

by:AA69
ID: 6207314
Good point I should have put that link in I'll remember next time. I'll try that code and report back again.
0
 

Author Comment

by:AA69
ID: 6449349
Hi Sorry for the delay in giving out the points( holidays and all that). Anyway we actually still used lset and didn't need to use the memcopy function we simply built up the UDT using simple for next loops and concatenating the strings as we went it all works just dandy. But your answer is still valid and I might well use that somewhere else so you get the points. Just one thing could you please point me to Rockford Lhotka's book.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6449365
Thanks
http://www.lhotka.net/rocky/Publications.aspx
Just click on:  "Professional Visual Basic 6 Distributed Objects" book
Note that book is not quite easy to read / understand.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

628 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