开发者

Does textbox save an all number text as a long or string?

开发者 https://www.devze.com 2023-03-08 11:19 出处:网络
I have a brief discussion with my teammate regarding this. He says, if I enter a number in textbox, and try to use the value later on using textbox.text or val(textbox.text), I will not need to parse

I have a brief discussion with my teammate regarding this. He says, if I enter a number in textbox, and try to use the value later on using textbox.text or val(textbox.text), I will not need to parse the value to int开发者_Python百科eger. According to him, if the text attribute value is all number, you can directly get the value as integer, instead of string.

So, if I have textBox1.Text = "12345", then next time, if I use, intABC = textBox1.Text, it will not throw an error. Is it right? Does C# or other .Net language does this implicit conversion? Also, will the code store "12345" as string or integer? And how much memory will this value take, 5bytes for 5 characters or 2bytes for an integer?


TextBox.Text keeps the text as a simple string, it doesn't care about the real "meaning" of the string.

Then, if you want to have your number back, you need to parse the string, hence neither implicit nor explicit cast to int is allowed (or better, it will throw an exception if you do it...).

About the size, that text is stored as an UNICODE (UTF-16) string, hence from 2 to 4 bytes per character (depending on the character).

You can easily measure the size (just the size of the string, without the overhead due to reference size etc.) using the following code:

int numBytes = Encoding.Unicode.GetByteCount(stringToMeasure);

To find more info about strings, unicode and encodings have a look here, here or here .


your friend is wrong, it will make the compiler unhappy, the compiler won't even convert it automatically for you. Text property of a TextBox is of type string. check this

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.text.aspx


As to your question of other languages; if 'option strict' is not enabled, VB.NET will allow this. It will also allow this assignment if the input is not entirely numeric however, resulting in a runtime exception.


If you know you will only use numerical values, try using a NumericUpDown control.
You could then get/set the numerical value (decimal) by using the Value property.

A NumericUpDown control contains a single numeric value that can be incremented or decremented by clicking the up or down buttons of the control. The user can also enter in a value, unless the ReadOnly property is set to true.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号