string to integer conversion

skaryChinezeGuie

Well-known member
Joined
Apr 23, 2006
Messages
94
Programming Experience
Beginner
the problem is that option strict won't allow string to integer conversion. How can i work around this or fix it? thanks in advance.:D

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] BtnDeposit_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myMoney [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Money
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myBank [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Bank
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DepositAmount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [B][I][U][COLOR=red]TxtDeposit.Text[/COLOR][/U][/I][/B]
[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] DepositAmount <= myMoney [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Bank = myBank + DepositAmount
Money = myMoney - DepositAmount
[/SIZE][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] FrmBank_Load([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.EventArgs)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("Try Again")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] BtnWithdraw_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myMoney2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Money
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myBank2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Bank
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WithdrawAmount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = [B][I][U][COLOR=red]TxtWithdraw.Text[/COLOR][/U][/I][/B]
[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] WithdrawAmount <= myBank2 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Bank = myBank2 - WithdrawAmount
Money = myMoney2 + WithdrawAmount
[/SIZE][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] FrmBank_Load([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.EventArgs)
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]MessageBox.Show("Try Again")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Last edited by a moderator:
skaryChinezeGuie said:
john is not funny. I "went there" for one because it was recommended to me, and two because after turning it on i actually realized the logic mistakes i made.
I wasn't trying to be funny. I seriously think OptionStrict=Off is the best feature of the Visual Basic language for the beginners. Sure you make mistakes and misassumptions, but you usually manage to to get anywhere for starters without getting stuck on implicit conversion of an integer in a textbox to a number variable, and you get there fast! At some point you need to learn more advanced programming and you can't get passed that with silly beginner errors, so you learn more and get more correct in coding after a while. There is written bookshelves on the validation topic, but it is not from that shelf the beginner grab her first book. So for a programmer like skaryChinezeGuie that doesn't know the difference between a String and a Integer, I really wouldn't recommend strict=on. Not yet.
 
sorry john. i have been getting snide comments like that because i'm new and when i started working on my project again and read the forum i was already half drunk. I really do like the death metal though. I took your advice and turnedit off and true it does work but i was more intested in doing it correctly, but i actually didn't really understand more than half the advice i was given. it was the doubt it heh heh that got me. sorry again.
 
JohnH said:
I wasn't trying to be funny. I seriously think OptionStrict=Off is the best feature of the Visual Basic language for the beginners. Sure you make mistakes and misassumptions, but you usually manage to to get anywhere for starters without getting stuck on implicit conversion of an integer in a textbox to a number variable, and you get there fast! At some point you need to learn more advanced programming and you can't get passed that with silly beginner errors, so you learn more and get more correct in coding after a while. There is written bookshelves on the validation topic, but it is not from that shelf the beginner grab her first book. So for a programmer like skaryChinezeGuie that doesn't know the difference between a String and a Integer, I really wouldn't recommend strict=on. Not yet.
I dissagree, mainly because of that ease factor.... people get used to it being off, and never go back and turn it on..... it's two years later and I STILL have yet to remember to go back and turn it on.... despite that I know I should. Programming shouldn't be easy.... it should be hard by default. If you want it easy, then you should have that option to make it so.... but we can sit here all day and argue this and there are merits to both sides, whether you agree with them or not. Maybe MS should have made Option Strict off in Express and Standard version, and turned it on by default in the other more powerfull versions. *shrug*

-tg
 
You could argue that, from a career and personal/professional development perspective, Option Strict=Off severely limits your understanding and employablilty.. As someone noted before, it's shocking the number of people I work with at my place that are allowed to write reports to run against the live system and they dont know the difference between a string containing numbers, and an integer. Using a function that returns a date as an argument to another function that accepts a date is something similarly beyond these guys. As another example, they dont quite understand the different between an SQL that is parameterized (with the parameter declared to accept a date) and an SQL that contains a string of #6/12/2005#.. they really think this is a date, not a string formatted in such a way that a database driver can treat it as a date.

I'm quite glad I was educated about OO through use of java at university - its typing is very strong and really reinforced my awareness of what data was stored for given variables. Being taught the complex route from the start is an advantage in getting it right in an ongoing sense.
On the flipside arguemnt to this, I can totally appreciate that programming is a hard subject, thinking like a computer is a difficult thing and that any help can be an advantage.. When I was much younger, like 10 years old, i tried to learn to program in C but I just didnt get it - my logic wasnt sufficiently advanced enough at the time to understand the concepts. Remembering this gives me an appreciation that these concepts are hard for others - in this case I think the beginner here has logic sufficiently advanced enough to appreciate the strong typing arguments, and in some strange kind of way I look at Option Strict as a necessary mental upgrade path for all VB programmers who relied upon it to make things easy; they should look to further themselves in a pro/pers developmental sense and develop more rigorous and precise ways of thinking.
As noted before, i feel the decision is mainly commercial; VB is regarded by some as a bit of a noddy language and the developers less technically capable than C style brethren. .NET can/is certainly closing this gap to nothing on a technical level but it will take more than that to shake the stigma. If Option Strict is a step towards that, then VBN programmers that care about how they are to be perceived in a professional sense would enable it by default



Techgnome - if you go to Tools/Options, click Show All Settings, navigate to Projects and Solutions/VB Defaults then you will find that you can enable it On by default. This may not have been the question you were asking (i assumed it was when you say 'still have to remember to go turn it on') and it doesnt help with the point you made about MS should have done this out of the box, but incase you didnt know.. it's in there :)
 
cjard - no that isn't what I meant.... I was talking about out of the box settings.... since Express and Standard are for the hobyist types, they are less likely to care if Option Strict is off.... While the Pro and Enterprise (And Team Server) are for the more ... um... enterprise type developers, and in those versions out of the box, Option Strict should be on....

On a happy note, I finaly did get around to turning on Option Strict, and I'm proud to report that my app was just fine.

-tg
 
TechGnome said:
cjard - no that isn't what I meant.... I was talking about out of the box settings.... since Express and Standard are for the hobyist types, they are less likely to care if Option Strict is off.... While the Pro and Enterprise (And Team Server) are for the more ... um... enterprise type developers, and in those versions out of the box, Option Strict should be on....

I just remembered, Microsoft DID turn Option Strict on, right out of the box.. It was called C# or something.. :D

*ducks*
 
Back
Top