NewBe Need Help With Const and Val

WackoWolf

Active member
Joined
Jan 6, 2006
Messages
39
Location
Rhode Island
Programming Experience
Beginner
I am new to Vb. The question I have might seem stupid to most but I am lost and need some help.

This is what I have for the code.

VB.NET:
[SIZE=2][COLOR=#0000ff]Const[/COLOR][/SIZE][SIZE=2] Adult = 30
 
Adult = (Val(TextAdult.Text) * Adult)

It is saying that for Adult in the beginning of "Adult = (Val(TextAdult.Text) * Adult)"
This is what it says "Constant cannot be target of an Assigment"

How can I write this so that I don't get this error?
Before I try to you Const I had no problems running the code, but now that I want to use "Const" I have errors. What am I doing wrong?

Any and all help would be cool.

Thank You
WackoWolf

[/SIZE]
 
first off you should be declaring variables as a type, such as:

Adult As Integer = 30
or
Adult As Decimal = 30

while declaring a variable you should give declare what scope it is by adding: Dim, Private, Friend, Public in front of it all

if it's being declared in a sub routine then the Dim keyword should be used, if it's at the top of a module or class (form) then the Private, Public, Friend should be used such as:

Private Adult As Integer = 30

if you have a variable that you want to set a value and that value can't be changed then you add the word "Const" in the declaration of the variable:

Private Const Adult As Integer = 30

in your case you're using the Const keyword, meaning the value can't be changed which is why this line:
Adult = (Val(TextAdult.Text) * Adult)
is giving you and error, if you change this line:
Const Adult = 30
to:
Dim Adult As Integer = 30 it'll work

also when changing a variable type to another (like a string to an integer, which is what you're question is based on) you should use the Convert methods, i'm assuming that all you need is an integer which means you'd use Convert.ToInt32 instead of Val

VB.NET:
Dim Adult As Integer = 30
Adult = Convert.Int32(TextAdult.Text * Adult)
 
Thank You, Hopefully I understand what you are saying.
This is what I now have for the code and it works, let me know what you think and if I should change it. I feel I have it right, but I am new and do wwant to learn to do it the right way and not half ass.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Adult [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 30
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Children [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = 10
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Night [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] NightlyRate [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Total [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Decimal
[/COLOR][/SIZE][SIZE=2]Adult = (TextAdult.Text * Adult)
Children = (TextChildren.Text * Children)
Night = (Val(TextNight.Text))
NightlyRate = (Adult + Children) * Night
Total = (NightlyRate * 0.05) + NightlyRate
TextTotal.Text = FormatCurrency(Total)
 
[/SIZE]


Thank You for your time and help.
I will be back again and again etc...

WackoWolf
 
from the looks of it, this is a hotel/motel rate calculator or something

i would declare Children and Nights as Integer because you're never going to need a decimal and i would have NightlyRate be a decimal as well

other than that without seeing the whole program, i'd say it looks good
 
JuggaloBrotha said:
f you have a variable that you want to set a value and that value can't be changed
Interesting turn of phrase. ;)

WackoWolf, if you're new to VB then I suggest that you generally avoid Runtime functions. Get used to using the VB.NET language and the .NET Framework without Runtime functions as much as possible. I really suggest avoiding Val. It uses a very lazy method of creating a numerical value and can lead to your app using values that the user never intended. Val will simply ignore invalid characters and make assumptions about the string value. You should validate it properly and reject it outright if it contains any invalid characters. I would suggest using the Double.TryParse method, which returns a Boolean value that indicates whether the string was successfully converted to a Double. In .NET 2.0 the other in-built types have a TryParse method too. As for the last line, I'd suggest this instead:
VB.NET:
TextTotal.Text = Total.ToString("c")
The "c" indicates that the standard currency format should be used.
 
Back
Top