Hello, I am a newbie in programming and I want to write a program in Visual Studio with using C# language which uses a textbox and a button only. When the user writes string "A" in the textbox and presses the button, the program shows integer "5" in a messagebox. If the user writes string "B" in the textbox, the program shows integer "4" in a messagebox, and for string "C", it shows "3", and goes like that... The tricky (for me) part of this program is I am not able to use any decision structures like if,switch,etc. It is possible in some way that I don't know. Please help me to learn how to do it. Thanks for giving your time.
+3
A:
You should not use a decision structure. The idea is to find the difference of the entered character from "A".
Good luck!
Mehrdad Afshari
2009-02-27 12:28:23
A is 65 ... So input char - 60 (>)
Andreas Niedermair
2009-02-27 12:36:08
You shouldn't use hardcode 65 though. (char)'A' is the way to go.
Mehrdad Afshari
2009-02-27 12:39:03
that's why i supplied the value in the brackets
Andreas Niedermair
2009-02-27 12:41:08
(int)'A', i meant..
Mehrdad Afshari
2009-02-27 12:44:10
A:
you should just discount 60 (<) from the char :) use this as a reference
var input = (int)'A';
var mask = (int)'<';
var result = (char)(input - mask);
edit:
i like the downvote!
Andreas Niedermair
2009-02-27 12:28:43
A:
You could do this by using a generic Dictionary. There are a couple of little gotchas you'd need to take care of, but a simple bit of testing will show you what these are (they are pretty obvious if you think about the problem for a bit).
Pete OHanlon
2009-02-27 12:28:48
he said - do not use any decision structures :) contains is decision - tryget/exception is internally a decision routine either!
Andreas Niedermair
2009-02-27 12:31:51
I didn't say use contains, and you can do this perfectly well without it.
Pete OHanlon
2009-02-27 12:35:21
I did read your whole comment - I chose to ignore the fascetiousness of your other part. Just because a language construct uses an decision routine internally doesn't mean you should be banned from using it. Whole swathes of the framework use decisions somewhere inside them.
Pete OHanlon
2009-02-27 19:28:56
+1
A:
This works in Java, I think this will work in C# as well.
If you subtract 'A' from the charachter the user enters you'll get an Int, 0 for A, 1 for B, etc. Basic maths can do the rest :) Don't forget to convert to a Char rather than subtracting strings.
Benjamin Confino
2009-02-27 12:30:57