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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange typematch 1

Status
Not open for further replies.

RPrinceton

Programmer
Jan 8, 2003
86
US
Hi Everyone,
Please enlighten me.
Environment: VB6 SP3 Windows 2000 Pro.

I have a user defined Type named NFLIO.Week defined as String * 2.
With field WkIdx defined as Integer I receive a type mismatch error in the following scenarios:
WkIdx = CVar(NFLIO.Week)
WkIdx = CInt(NFLIO.Week)
WkIdx = CDbl(NFLIO.Week)

I changed field WkIdx to Variant.
With field WkIdx defined as Variant I receive a type mismatch error in the following scenarios:
WkIdx = CVar(NFLIO.Week)
WkIdx = CDbl(NFLIO.Week)

The following works:
Field WkIdx is defined as Variant
WkIdx = NFLIO.Week

What is even more strange is that when in development I didn't have to play any games with the data type...the type mismatches began occurring after I created an .EXE. What am I not understanding?
Please advise. Thx in advance.
Regards,
RPrinceton




 
RPrinceton,
You should introduce variable of type NFLIO. Do not operate with your udt directly

vladk
 
Sorry vladk, I'm not following. What do you mean by introduce?
Can you give me an example?

I have it defined as follows:
Type NFLDsect
RecdCode As String * 1
Week As String * 2
End Type
Dim NFLIO As NFLDsect
WkIdx = NFLIO.Week

Is this not correct?

Please advise. Thx in advance.
Regards,
RPrinceton
 
RPrinceton,

This is correct. I would not use *1 and *2 though. In my life, it usually brings more problems than benefits. I would also make upgrade with SP5 or SP6.

By the way, before your statements like WkIdx = CVar(NFLIO.Week), did you initialize your NFLIO.Week with any value, like this:

Dim NFLIO As NFLDsect
Dim WkIdx As Integer

NFLIO.Week = "1 "

WkIdx = CVar(NFLIO.Week)
WkIdx = CInt(NFLIO.Week)
WkIdx = CDbl(NFLIO.Week)

I think, you should.

vladk
 
RPrinceton,

To finalize my thoughts: you should avoid applying cdbl and cint to the strings that cannot be converted to numeric values.

vladk

 
Hi vladk,
I have isolated the type match problem. I have included the code snippet. Please bear in mind that this code displays the correct week number when running in the IDE mode, however when compiled the week number displays as blank. It is as though the CopyMemory function does not work once the module is compiled. Since dialoging last I have upgraded to SP6.
Regards,
Randall Princeton

Code snippet:
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

Type NFLDsect
RecdCode As String * 1
Week As String * 2
F1 As String * 1
VisitingTeam As String * 3
F2 As String * 1
VisitingScore As String * 3
F3 As String * 1
HomeTeam As String * 3
F4 As String * 1
HomeScore As String * 3
F5 As String * 1
DOW As String * 3
F6 As String * 1
EOR As String * 2
End Type

Dim NFLIO As NFLDsect
Open "c:\MyFile" For Input As #1
Line Input #1, IO
CopyMemory ByVal NFLIO, ByVal StrPtr(IO), LenB(NFLIO) 'required to get contents of IO mapped to record user type

MsgBox (NFLIO.Week)
 
>I have isolated the type match problem

Firstly, I think vladk was correct; if NFLIO.Week does not contain a string representing a numeric value (and in your example code it is empty) you will get all the Type Mismatch errors you describe.

Secondly, it might also be worth pointing out that VB will do string casting on the fly, so you don't (necessarily) have to be explicit about it.

Given the above two comments, the following should work correctly:
Code:
Dim NFLIO As NFLDsect
    Dim WkIdx As Long
    NFLIO.Week = "12"
    WkIdx = NFLIO.Week
but the next example would fail with atype mismatch:
Code:
Dim NFLIO As NFLDsect
    Dim WkIdx As Long
    WkIdx = NFLIO.Week

Thirdly, you can directly read your UDTs in from your file by opening in Random mode and using Get:
Code:
Dim hFile As Long
Dim NFLIO As NFLDsect
hFile = FreeFile

Open "c:\MyFile" For Random As hFile Len = Len(NFLIO) + 2 ' ensure we deal with CRLF; just use + 1 if only an LF seperator
Get #hFile, , NFLIO
Close hFile
MsgBox NFLIO.Week
This means we don't have to use the CopyMemory function
 
Hi Everyone,
I have implemented strongm's solution and it works properly.
Thank you again.
Regards,
RPrinceton
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top