Announcement

Collapse
No announcement yet.

Pass different UDT's to the same function?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pass different UDT's to the same function?

    How could WriteUDT be coded to receive other UDT's?
    TheRecord AS RecordType is hard-code to a single UDT.
    I want to pass records from any file to this function.
    Code:
    TYPE RecordType
      Counter    AS DWORD
      UserName   AS STRING * 30
      Password   AS STRING * 30
    END TYPE
    FUNCTION PBMAIN AS LONG
     
      LOCAL sFileName    AS STRING
      LOCAL record       AS RecordType
      LOCAL result       AS LONG
      LOCAL RecordNumber AS DWORD
     
      sFileName = "C:\SOMETHING.TXT"
      RecordNumber = 1
      record.counter = 1
      record.UserName = "HEIDI KLUME"
      record.Password = "RUNWAY"
      result = WriteUDT(sFileName, RecordNumber, Record)
      ? "Result of write" + STR$(result)
    END FUNCTION
    FUNCTION WriteUDT(sFileName AS STRING, RecordNumber AS DWORD, TheRecord AS RecordType) AS LONG
      'How would this be coded to receive data from any UDT?
      LOCAL Retries      AS LONG
      LOCAL hFile        AS LONG
     
      FOR Retries = 1 TO 100
        ERRCLEAR
        OPEN sFileName FOR RANDOM AS #hFile LEN = SIZEOF(TheRecord)
        IF ERR = 0 THEN EXIT FOR
        SLEEP 100
      NEXT
      IF ERR THEN FUNCTION = ERRCLEAR: EXIT FUNCTION
     
      PUT #hFile, RecordNumber, TheRecord
      IF ERR THEN FUNCTION = ERRCLEAR: EXIT FUNCTION
     
      CLOSE #hFile
      IF ERR THEN FUNCTION = ERRCLEAR
     
    END FUNCTION
    How long is an idea? Write it down.

  • #2
    Place UDT into sBuffer string and pass as a string
    Change SIZEOF(Record) to LEN(sBuffer)
    Works, but not sure the best way

    Code:
    TYPE RecordType
      Counter    AS DWORD
      UserName   AS STRING * 30
      Password   AS STRING * 30
    END TYPE
    FUNCTION PBMAIN AS LONG
      LOCAL sFileName    AS STRING
      LOCAL record       AS RecordType
      LOCAL result       AS LONG
      LOCAL RecordNumber AS DWORD
      LOCAL s            AS STRING
      sFileName = "C:\SOMETHING.TXT"
      RecordNumber = 1
      record.counter = 1
      record.UserName = "HEIDI KLUME"
      record.Password = "RUNWAY"
     
      s = record:  result = WriteUDT(sFileName, RecordNumber, s)
     
      ? "Result of write" + STR$(result)
      SLEEP 2000
    END FUNCTION
    FUNCTION WriteUDT(sFileName AS STRING, RecordNumber AS DWORD, sBuffer AS STRING) AS LONG
      'How would this be coded to receive data from any UDT?
     
      LOCAL Retries      AS LONG
      LOCAL hFile        AS LONG
      FOR Retries = 1 TO 100
        ERRCLEAR
        OPEN sFileName FOR RANDOM AS #hFile LEN = LEN(sBuffer)
        IF ERR = 0 THEN EXIT FOR
        SLEEP 100
      NEXT
      IF ERR THEN FUNCTION = ERRCLEAR: EXIT FUNCTION
      PUT #hFile, RecordNumber, sBuffer
      IF ERR THEN FUNCTION = ERRCLEAR: EXIT FUNCTION
      CLOSE #hFile
      IF ERR THEN FUNCTION = ERRCLEAR
    END FUNCTION
    Last edited by Mike Doty; 30 Dec 2008, 05:28 AM.
    How long is an idea? Write it down.

    Comment


    • #3
      >Works, but not sure the best way

      It's the ONLY way with COMPILED code (which could be placed into, say, a DLL) unless you add a 'size of record' parameter to the function.

      However, a MACRO FUNCTION would not be sensitive to the 'parameters' so this should work..

      Code:
      MACRO FUNCTION WriteUdt(szFile,udtvar) 
      
       MACROTEMP hFile, S
       DIM hFile AS LONG, S AS STRING
         hFIle = FREEFILE 
         OPEN szFile FOR APPEND AS hFile 
         S      = UdtVar 
         PRINT #hFile, S
         CLOSE hFile
      END MACRO
      Something like that, anyway.
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment

      Working...
      X