The information in this article applies to:
- Standard, Professional, and Enterprise Editions of Microsoft Visual
Basic, version 4.0, for Windows
SUMMARY
When converting a string with a numeric representation to a double with a
"%" percent symbol, a type mismatch is generated. In Visual Basic 3.0, the
application implicitly understood the "%" and converted the output to its
decimal equivalent
Step-by-Step Example
Use the steps below to re-create the error:
- Start a new Visual Basic project in either the Visual Basic 16-bit or 32-
bit Edition.
- Add a label to Form1. In the caption property of Label1, put in the
number "12.00%". Add a command button to the form, and in the click
event place the following:
Private Sub Command1_Click()
Dim D as Double
D = CDbl(Label1.Caption)
Msgbox Str$(D)
End sub
The following code will generate a type mismatch error in both Visual Basic
16-bit and 32-bit versions. In Visual Basic 3.0, the string will be
implicitly converted to ".12".
- For this code to work, the Left function should be implemented to strip
off the "%" character. In the case of the previous example, the output
needs to be divided by 100.
Private Sub Command1_Click()
Dim D as Double
D = CDbl(Left$(Label1.Caption,Len(Label1.Caption) - 1)/100)
Msgbox Str$(D)
End sub
The code above with the use of the Left function will output ".12".
|