|
-
Feb 14th, 2006, 05:19 PM
#1
Thread Starter
Lively Member
Cstr()
The Cstr() is not supported in C#.NET. So how would i go about converting a a string to an integer. I.e a number in a text box to an integer.
regards
Carl
-
Feb 14th, 2006, 05:20 PM
#2
Re: Cstr()
Convert.ToInt32(TextBox.Text.Trim);
Edit: C# guys and their crazy crazy case sensitivity.
-
Feb 14th, 2006, 06:17 PM
#3
Re: Cstr()
By the way, CStr, CInt, etc. should not really be used to "convert" anything in VB.NET either. CStr and it's ilk should be used for casting, not converting. "Casting" means that you are changing the type of the reference you are using to access the object, but not changing the object itself. "Converting" means that you are actully changing the object. Here's a VB.NET example of when it would be appropriate to use CStr:
VB Code:
Dim myArrayList As New ArrayList
myArrayList.Add("Some String")
Dim myString As String = CStr(myArrayList(0))
The object in the ArrayList IS a String but indexing the ArrayList retruns an Object reference, so you cast it as a String. At no time is the object itself anything other than a String.
On another note, if you're converting the contents of a TextBox to an Int32 then you'd better be sure that the String actually does represent a valid integer or else Convert.ToInt32 or Int32.Parse will both throw an exception. If you're converting user input you really should validate it, unless you've already done so by handling keyboard events, etc. It is generally a good idea to use a TryParse method when converting user inputted text to numbers. If you're using .NET 1.x you only have Double.TryParse, but you can use the arguments to specify that only integers are valid. If you're using .NET 2.0 you have Int32.TryParse as well.
-
Feb 14th, 2006, 06:19 PM
#4
Re: Cstr()
I had heard somewhere that TryParse throws an exception behind the curtains. Is that true?
-
Feb 14th, 2006, 06:30 PM
#5
Re: Cstr()
 Originally Posted by sevenhalo
I had heard somewhere that TryParse throws an exception behind the curtains. Is that true?
Absolutely not. In fact, TryParse was created specifically to avoid an exception being thrown. Here's the story I've read, although I can't guarantee that it's true.
Microsoft created the IsDate and IsNumeric Runtime functions to mimic the equivalent functions in VB6. IsDate merely tried to convert the specified object to a Date and caught the exception if it failed. That much is true. It is pretty terrible programming practice but it's true. IsNumeric was implemented the same way for numbers. The developers at Microsoft realised that this produced pretty terrible performance if the objects failed to convert, thus they created Double.TryParse. IsNumeric calls Double.TryParse internally to avoid an exception being thrown when the conversion failed. Now that Date.TryParse exists in .NET 2.0, I'd imagine that IsDate has had it's implementation changed so that it too will avoid throwing exceptions. I guess they figured that IsNumeric would get used more often than IsDate so it was more important to get working properly.
-
Feb 15th, 2006, 09:09 AM
#6
Thread Starter
Lively Member
Re: Cstr()
Thanks for all of your help. I'm trying to make a program that adds to numbers in the two fileds but the Val() function does not seem to work, not supported in C# either. i need a way for it to recognise the text bos string as an integer so i though i would try and convert it. Am i on the right line or am i way off?
-
Feb 15th, 2006, 09:18 AM
#7
Re: Cstr()
Absolutely, but no VB6-style functions are going to work in C#. A lot of functions were added to the VB.NET run-time library that mimic functions that existed in VB6. These are specifically so that VB6-style code won't break in VB.NET, but C# has no such concern. In actual fact, the VB.NET run-time library is just a .NET assembly, so you can actually reference it from a C# app, but I strongly recommend against doing so. Val works in a very dodgy way anyway, and exemplifies for me the sort of thing that is responsible fro VB having been looked down upon by other developers.
Anyway, what you should do in C#, and VB.NET for that matter, is use a TryParse method. If you are using 2005 you can use Integer.TryParse. If you're using 2003 you have to use Double.TryParse, but you can specify that only integers are valid. Look them up in the help/MSDN. The help topics will have code examples.
-
Feb 15th, 2006, 04:06 PM
#8
Thread Starter
Lively Member
-
Feb 15th, 2006, 05:59 PM
#9
Addicted Member
Re: Cstr()
textboxes fire validation events. you might want to think about using them.
Code:
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
Code:
public bool IsInteger(string strNumber)
{
Regex rexNotInt=new Regex("[^0-9-]");
Regex rexInt=new Regex("^-[0-9]+$|^[0-9]+$");
return !rexNotInt.IsMatch(strNumber) &&
rexInt.IsMatch(strNumber);
}
"And most of the evils of society can, in fact, be cured through information. We have a society that has been disinformed and based on the disinformation has made irrational choices. And that's what I mean by 'ignorance.' People, who ordinarily might be smart, are deprived of the data by which to make a rational decision, don't have the data to do it."
Frank Zappa
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|