Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 29th, 2004, 02:14 PM
Authorized User
 
Join Date: Aug 2004
Location: OKC, OK, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert string from text box to integer

How do you convert the contents inputed into a text box into an integer. All the inputs entered into the text box will be numeric.

I've declared a varible: Dim StoreNum as string (if I delcare at as integer I get the error: Cast from string "100" to type 'Integer" is not valid).

I've set the variable equal to the text box.
StoreNum = txtFirstNum.Text

When I watch the variable while I step through the code, the contents show "100". How do I change this to be an integer rather than a string?

Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.
__________________
Jeff Armstrong
Sr. Systems Mgr
SBC Services, Inc.
  #2 (permalink)  
Old October 29th, 2004, 02:34 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

StoreNum = CType(txtFirstNum.Text, Integer)
  #3 (permalink)  
Old October 31st, 2004, 12:15 PM
Friend of Wrox
 
Join Date: Feb 2004
Location: Bangalore, Karnataka, India.
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can also use like below

Convert.ToInt32(str) or System.Int32.Parse(str)


It is not how much we do,
but how much love we put in the doing.

-Mother Theresa
  #4 (permalink)  
Old November 4th, 2004, 09:09 PM
Registered User
 
Join Date: Nov 2004
Location: , FL, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Question,

Would it be appropriate, instead, to use

Dim StoreNum as Integer
StoreNum = Val(txtFirstNum.Text)

I am new to VB.Net, I recognize Val() as a holdover from VB 6.0 but am uncertain if using it (in this situation) is reliable.

Thanks!
  #5 (permalink)  
Old November 5th, 2004, 05:19 AM
Friend of Wrox
 
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Val() would work, but would be a little inefficient because it would convert the string txtFirstNum.Text to a double, then the double would be converted to an Integer so that StoreNum could hold it.

That said, it will probably make around one nanosecond of difference. Just don't put it in a big loop!

Also, you may get an overflow error if the number held in txtFirstNum.Text is too big for an integer (8,388,608 max). If you set txtFirstNum.MaxLength to 6 or less, you won't get any problems (being that the highest number you could then put in would be 999,999)

Quote:
quote:Originally posted by john.fl
 Question,

Would it be appropriate, instead, to use

Dim StoreNum as Integer
StoreNum = Val(txtFirstNum.Text)

I am new to VB.Net, I recognize Val() as a holdover from VB 6.0 but am uncertain if using it (in this situation) is reliable.

Thanks!
  #6 (permalink)  
Old November 6th, 2004, 10:04 PM
Registered User
 
Join Date: Nov 2004
Location: , FL, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jaucourt, that makes sense to me. Thank you and have a nice day! :)


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Resolved] Convert Time to Integer snufse ASP.NET 2.0 Basics 1 October 7th, 2008 11:44 AM
Passing string to a text box in search engine rylemer Classic ASP Basics 0 October 26th, 2005 03:23 PM
Passing string to a text box in search engine rylemer ASP.NET 1.0 and 1.1 Professional 2 October 25th, 2005 06:11 PM
convert string to integer kcheung Classic ASP Databases 1 July 28th, 2003 04:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.