none
Variables not changing within a class RRS feed

  • Question

  • I'll paraphrase my coding to help.

            public static string CurrencyChoice = " ";
            public static string dGBP = " ";
            public static decimal dValue = 0M;
            public static decimal dCurrency = 0M;
            public static decimal Answer = 0M;

    dCurrency isn't changing

    ConsoleKey ckChoice = Console.ReadKey(true).Key;
                switch (ckChoice)
                {
                    case ConsoleKey.D1:
                        CurrencyChoice = "Chinese CNY";
                        dCurrency = 8.98M;
                        Console.Clear();
                        break;
                    case ConsoleKey.D2:
                        CurrencyChoice = "American USD";
                        dCurrency = 1.1M;
                        Console.Clear();
                        break;
                    case ConsoleKey.D3:
                        CurrencyChoice = "European EUR";
                        dCurrency = 1.15M;
                        Console.Clear();
                        break;
                    case ConsoleKey.D4:
                        CurrencyChoice = "Switzerland CHF";
                        dCurrency = 1.26M;
                        Console.Clear();
                        break;
                    case ConsoleKey.D5:
                        CurrencyChoice = "Japanese JPF";
                        dCurrency = 137.24M;
                        Console.Clear();
                        break;
                    case ConsoleKey.Escape:
                        System.Environment.Exit(1); //Work around from the normal break; to quit the console.
                        break;
                    default:
                        MenuIncorrectText();
                        break;
                        

                }
                CurrencyText();

    static void Maths()
            {
                if (decimal.TryParse(dGBP, out dValue) && dValue > 0) //saying if dGBP can become a decimal do the below code else output the input was incorrect and restart the currencys code and if dvalue is > 0 then carry on
                {
                    decimal Answer = dValue * dCurrency; //Works out currency by timesing the difference that was stated in the variable fCYN
                    AnswerText();

                }
                else
                {
                    Console.Clear();
                    IncorrectGBPText();
                }
            }

    Wednesday, October 16, 2019 4:06 PM

All replies

  • It outputs 0 every time instead of the correct number.
    Wednesday, October 16, 2019 4:18 PM
  • It will help us help you if you provide an explanation, not just code.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, October 16, 2019 5:01 PM
  • I have the variable dCurrency (I am doing a money converter) within the switch depending on the key they press they choose a currency by pressing 1 you change a the 'CurrencyChoice' variable which works great although the dCurrency variable doesn't seem to change and outputs 1 everytime.
    Wednesday, October 16, 2019 5:46 PM
  • Make sure that the static dCurrency variable is the only one with this name and is not redeclared as a local variable or function parameter.

    Wednesday, October 16, 2019 8:01 PM
  • I have the variable dCurrency (I am doing a money converter) within the switch depending on the key they press they choose a currency by pressing 1 you change a the 'CurrencyChoice' variable which works great although the dCurrency variable doesn't seem to change and outputs 1 everytime.

    When posting code please use the "Insert Code Block" feature of the forum editor.
    It should be the menu bar button that is second from the right. Hovering over
    it will show a tooltip that says" "Insert Code Block".

    For issues such as this you should be using the debugger to step through your
    code and check the logic flow, as well as the contents of variables as they are 
    changed.

    - Wayne

    Thursday, October 17, 2019 12:08 AM
  • Hi Ottoisking, 

    Thank you for posting here.

    I make a test based on your code, but I don’t have the problem that dCurrency isn't changing.

    Could you provide some code about ‘MenuIncorrectText’, ‘CurrencyText’, ‘AnswerText’, ‘IncorrectGBPText’ and where you use ‘Maths’ method?

    It will help us to make a test and reproduce your problem.  

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 17, 2019 5:28 AM
    Moderator
  • As a rule, "paraphrasing code" is almost always a bad idea.  You don't know where the problem lies, so you don't know that the lines you leave out might be important.  It's always better to give us a complete, runnable example.  Many of us will happily copy and paste your code on our systems and find the problem for you.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, October 17, 2019 9:15 PM