Thumbs upString to number to string

Posts 14 of 4 · Page 1 of 1
String to number to string
Well I was bored and I am playing around with c# because how much farther it can go compared to vb.net

And I have found a very simple way to type a number in a textbox, grab the number and run it through a subroutine which will out put that number spelled look at the screen shot.





So were going to need a textbox and a button and 2 labels

label1 will be the display, label2 will be the instructions to the user. Change button1 text to Change, and change label1 text to Zero.

Now were are going to need to write our SubRoutine



Now we are going to write a Select case statement or in c# a switch




Now for the tricky part I got stuck on this part and after a bit of playing I figured it out.

We are going to Call our sub by



the int.phase it phase's the string looking for the int

and lets add error handling




All code was writen by myself Immortal- / cosconub and I wrote this tutorial

@Jason - What would be a better way to write this other then a switch?
You can do exactly the same thing with VB.NET

I'd use an Enum simply because I'm lazy. I doubt it performs as well but oh well:

Code:
private enum NumberWords
{
    Zero = 0, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen, Seventeen, Eighteen, Nineteen
}
Note: You only need to go up to 19, then specify the tens (twenty, thirty...etc) and then the suffixes ( hundred, thousand...etc ) before you can write a recursive algorithm to fill in the rest of the values.

To use the enum:

Code:
int i = 10;
string word = Enum.Parse(typeof(NumberWords), i).ToString();
Stop calling them subroutines.
Quote Originally Posted by CyberStriker View Post
Stop calling them subroutines.
Technically there is a difference between the two - subroutines don't return data to the caller where as functions do. I personally think it's beyond retarded that VB differentiates between the two by using the two separate key words; in C++, C#, java and virtually all other commercial languages you just set the return to void to define a subroutine. I also think its retarded how programmers differentiate with them as well, so I agree with you in regards to calling subroutines anything but functions (even though technically they aren't.)

Truth, there aren't too many ways to do this. Even a lot of game engines have very large switch statements to translate KeyCodes to their character representation (i.e when you type into a textbox) Performance - wise there isn't a very big impact as switch statements can be well optimized by the compilers + they're virtually translated into a large array of conditional jumps - which isn't intensive to begin with.
Posts 14 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?