UDT to Module Function

Started by Petrus Vorster, April 13, 2013, 05:20:22 PM

Previous topic - Next topic

Petrus Vorster

Hi all

I think I am overlooking something simple. I want to use a UDT via a function in a module.
for instance:
Type STUDENT
  surname as string * 50
  initials    as string * 4
End type

The module will have for instance :
Function FindStudent (myfile as string, student as dword) as string

This dim issue here is wrong. I cannot determine what "student" here must be.
The module should be able to take any of the several UDT's, pass it through this function and it should save/ search correctly.
However when I try this i get all kinds of errors. Obviously the "as dword" is incorrect, but what do one use for a UDT?

Thanks for all the patience!
-Regards
Peter

Eddy Van Esch

Function FindStudent (myfile as string, MyStudent as STUDENT) as string ??
Or you can pass a pointer to a udt ...
Eddy

Petrus Vorster

That would work, but it wont be universal for any UDT.
I want to create a Module function that can handle ANY UDT I pass it (within reasonable limits) for instance:

The function will be something like:
Function savedetails (myfile as string, MYUDT as ......) as long

so any UDT i send through must be save in the given file. The functioning is simple, its just how to pass any UDT to a module.
-Regards
Peter

Eddy Van Esch

#3
Something like this?

TYPE testUDT1
a AS LONG
b AS DWORD
END TYPE

TYPE testUDT2
a AS LONG
b AS DWORD
c AS STRING * 6
END TYPE


FUNCTION Test(BYVAL WhichUDT AS DWORD, a AS DWORD) AS LONG
    DIM V1 AS testUDT1 PTR 
    DIM V2 AS testUDT2 PTR
   
    SELECT CASE WhichUDT
   
      CASE 1: 
        v1 = a
        MSGBOX STR$(@v1.a) + STR$(@v1.b)
       
      CASE 2: 
        v2 = a
        MSGBOX STR$(@v2.a) + STR$(@v2.b) + @v2.c
       
    END SELECT
   
END FUNCTION



'-----------------------------------------------------------
FUNCTION PBMAIN () AS LONG
    DIM t1 AS testUDT1
    DIM t2 AS testUDT2
     
    t1.a = 1
    t1.b = 22
    t2.a = 3
    t2.b = 44
    t2.c = "abcdef"
   
    Test(1, VARPTR(t1))
    Test(2, VARPTR(t2))
END FUNCTION
   
Eddy

Petrus Vorster

Thanks a million Eddy!

It works like a charm! Just the BYVAL in the module and the VARPTR in the call and there you go!!!
This will save EONS of my time!!!

Appreciate it very much!
-Regards
Peter

Petrus Vorster

 :(
Nope. The call works, but the data in the UDT doesn't go through to the function in the module.
-Regards
Peter

Eddy Van Esch

#6
Petrus,
The below program outputs:

1 2
3 4abcdef

Wasn't that what you needed?
Or is there something I am misunderstanding?


#COMPILE EXE
#DIM ALL

DECLARE FUNCTION zTrace LIB "zTrace.DLL" ALIAS "zTrace" (zMessage AS ASCIIZ) AS LONG

TYPE testUDT1
a AS LONG
b AS DWORD
END TYPE

TYPE testUDT2
a AS LONG
b AS DWORD
c AS STRING * 6
END TYPE

FUNCTION Test(BYVAL WhichUDT AS DWORD, a AS DWORD) AS LONG
    DIM V1 AS testUDT1 PTR 
    DIM V2 AS testUDT2 PTR
   
    SELECT CASE WhichUDT 
      CASE 1
        v1 = a
        zTrace STR$(@v1.a) + STR$(@v1.b)    'You can replace zTrace by MsgBox or PRINT
       
      CASE 2: 
        v2 = a
        zTrace STR$(@v2.a) + STR$(@v2.b) + @v2.c
       
    END SELECT 
END FUNCTION

'-----------------------------------------------------------
FUNCTION PBMAIN () AS LONG
    DIM t1 AS testUDT1
    DIM t2 AS testUDT2
     
    t1.a = 1
    t1.b = 2
   
    t2.a = 3     
    t2.b = 4
    t2.c = "abcdef"
   
    Test(1, VARPTR(t1))
    Test(2, VARPTR(t2))
    SLEEP 9999
END FUNCTION
'-----------------------------------------------------------
Eddy

Petrus Vorster

Hi Eddy

Your example works with like small quantities of data like those you showed, but once the UDT becomes quite large, only the first few bits of data is passed to the module. Quite strange, but i think it has something to do with the Dword setting.
(My UDT has like several Long's, some strings, etc)

It appears as if the Dword setting allows for only a certain volume of info to be passed as I get only a small portion of the data in the module.

I will experiment more, I really appreciate the help. I have progressed a great deal on this thanks to your help!

Regards.
-Regards
Peter

Petrus Vorster

Hi Eddy

FINALLY!!! Thanks a million for the help.
Its is now working perfectly. Just had to ensure the PTR is correctly assigned and the receiving file was opened with the correct size!!!!
Had a real hard time trying to figure out just what the heck the issue was!!!

I am sure this will reduce lots of hours behind a PC!!!

:)
-Regards
Peter