Resolved Dim As *?*

Dimuthu93

Member
Joined
Oct 18, 2009
Messages
13
Programming Experience
Beginner
:confused:
hey guys
im new to the proggramming world, in fact i only began last month when i joined our year ten CP class, well anyway, i have a question, our teacher gave us this assignment, we had to create a grade calculator, seems easy enough (like put in 20/40 come out 50%) . heres what i had done

Dim Mark As Decimal
Mark = txtMark.Text
lblMark.text = FormatPercent(Mark)

What have i done wrong?
How can i fix it?
Please help
 
Last edited:
I dunno what your FormatPercent() function does but the code you've posted would need to be changed to something like this for starters:
VB.NET:
Dim Mark As Decimal
If Decimal.TryParse(lblMark.Text.Trim, Mark) Then lblMark.Text = FormatPercent(Mark)
 
I tried what you had written the following error message occured:

Conversion from string "12/24" to type 'Decimal' is not valid.

Thanks for trying though, also how do you get the coding to appear like that?
 
First of all, put Option Strict On at the very top of your code. This will enforce explicit type conversion and avoid a lot of errors.

You cannot save text from a textbox to a number directly. You need to save it to a string variable. Then you can convert the string to a number with the TryParse method.

Why are you using the Decimal number type? That should only be used for very large currency values. Use the Double type instead for a fractional number like a percentage. Furthermore, the percent figure should be divided by 100.

Example:

Dim mark As Double
Dim smark As String
smark = txtMark.Text
Double.TryParse(smark, mark)
mark = mark / 100
lblMark.Text = mark.ToString("p")
 
Last edited:
I revised my post to include a division for the percent figure. When you format a number to a string using the .ToString() method, you can use an argument to display the number in different ways. The "p" displays it as a percentage. Try out the code for yourself:


Option Strict On 'put this line at the very top of your code window

Dim mark As Double
Dim smark As String
smark = txtMark.Text
Double.TryParse(smark, mark)
mark = mark / 100
lblMark.Text = mark.ToString("p")
 
Back
Top