Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Defined Types - assigning at group level 1

Status
Not open for further replies.

JesOakley

Programmer
Jan 21, 2008
42
0
0
GB
Hi. If I have read in a 40 char fixed length record into a string, can I move this value in one go into a field defined as a 40 char UDT, where the UDT is made up 3 fields; 30, 6 and 4 chars respectively? It seems madness that you have to do every element individually. I have tried the following and it fails with Type Mismatch:
Code:
Type InfileInfo
    field1 As String * 30
    field2 As String * 6
    field3 As String * 4
End Type

Public Sub ProcessFLFile()
    Dim InRecord As InfileInfo, strInRec as string, intInFileNo as integer

    strInfile = "C:\filename.txt"
    intInFileNo = FreeFile
    Open strInfile For Input As #intInFileNo

    Do While Not EOF(intInFileNo)
        Line Input #intInFileNo, strInRec
        InRecord = strInRec ' << type mismatch here
Any help gratefully appreciated.
 
You find this behavior strange? I don't know of any language that behaves in any other fashion. There is no implied order in a Type definition. So there is no way to make the engine understand that Field1 is the first 30 characters. Or is Field2 the first 6? Maybe Field3 is 4 characters starting after the 6th?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Perhaps:

Code:
Type RecordX    ' Define user-defined type.
    ID As String * 2
    NameX As String * 20
End Type

Sub GetRecord()

Dim MyRecord As RecordX    ' Declare variable.
Open "C:\docs\TESTFILE.txt" For Random As #1 Len = Len(MyRecord)

    Position = 1    ' Define record number.
    Get #1, Position, MyRecord    ' Read record.
    
    Debug.Print MyRecord.ID
    Debug.Print MyRecord.NameX

Close #1

End Sub

 
EBGreen:- Cobol?

Remou:- That's what I thought too, but it gave me the same error, as if record had to be read into a string.
 
Try running the code exactly as I did, because it worked for me. The TESTFILE.txt consisted of two lines:

[tt]10abcdefghijklmnopqqqq
11abcdefghijklmnopqqqq[/tt]

I also tried with a record type:

Code:
Type RecordX    ' Define user-defined type.
    ID As String * 2
    NameX As String * 16
    NameY As String * 4
End Type

and that worked as well.

 
Cheers Remou. It must have been something to do with me using Open As Input with Line Input versus your Open as Random with Get that made the difference, because mine didn't work and yours did!
However, although the first line in my my input file (created in a text editor) read in okay, the 2nd record was offset. I was able to fix this by adjusting the record length on my Open statement to say
Code:
Open strInfile For Random As #intInFileNo Len = Len(InRecord) [COLOR=red]+ 2[/color]
I presume it needed to take the CR and the LF into account (presumably could have declared them at the end of my UDT too).
 
Just going back to EBGreen's point for a minute, isn't there an implied order in a UDT? And if not, then is there another easy way to achieve the same effect i.e. mapping a group level field and then being able to reference the lower level fields?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top