views:

264

answers:

5

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
A is 65 ... So input char - 60 (>)
Andreas Niedermair
You shouldn't use hardcode 65 though. (char)'A' is the way to go.
Mehrdad Afshari
that's why i supplied the value in the brackets
Andreas Niedermair
(int)'A', i meant..
Mehrdad Afshari
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
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
he said - do not use any decision structures :) contains is decision - tryget/exception is internally a decision routine either!
Andreas Niedermair
I didn't say use contains, and you can do this perfectly well without it.
Pete OHanlon
please read my whole comment before replying ...
Andreas Niedermair
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
+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
A: 

Sounds like somebody doesnt want to do his homework himself.

Here we go:

You convert the Character insertered (A, B or C) to int, subtract 70, multiply it with -1 and Display the value in the messagebox.