none
Why my method isnt working? RRS feed

  • Question

  • public void GetVal(string msg, double value)
            {
                if (comPort.IsOpen == true)
                {
                    comPort.WriteLine(msg);
                }
                Thread.Sleep(200);
                string buff = comPort.ReadLine();
                Thread.Sleep(200);
                string numberPart = buff.Substring(buff.LastIndexOf(' ')).Trim();
                value = double.Parse(numberPart);
                
            }

    this is my Method(), but the thing is when i call it example: GetVal("VA VV_IN", vv_in)

    the value i get in my variable vv_in is 0, when i make the method without "double value" and instead i write "vv_in = double.Parse(numberPart) the value i get in the vv_in is the wanted value...

    Wednesday, December 20, 2017 11:22 AM

All replies

  • It looks like you actually want to return the value from the method?

    In which case you could just...return it by declaring the method as returning a double and assigning the result into your vv_in variable when you call it:

    public double GetVal(string msg)
            {
                if (comPort.IsOpen == true)
                {
                    comPort.WriteLine(msg);
                }
                Thread.Sleep(200);
                string buff = comPort.ReadLine();
                Thread.Sleep(200);
                string numberPart = buff.Substring(buff.LastIndexOf(' ')).Trim();
                return double.Parse(numberPart);
                
            }

    Then call it with:

    vv_in = GetVal("VA VV_IN")

    Alternately you could declare the value argument as a 'ref' or 'out' parameter, but this just seems unnecessary when you can simply return the value as the method result.


    Wednesday, December 20, 2017 11:36 AM
  • Hello xBehemoth,

    Your issues is related to parameter pass, when you pass value type the parameter("value") will copy the value("vv_in") you want to pass. In other words, The "value" instance can't affect the "vv_in" content because it is copy part for value type. As for a workaround, you could use ''ref" keywords.

    void GetVal(string msg,ref double value)
            {
            ...
            ...
            }

    For how to use

    GetVal("VA VV_IN", ref vv_in)

    Best regards,

    Neil Hu


    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, December 21, 2017 6:24 AM
    Moderator
  • Hello xBehemoth,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and patience!

    Best regards,

    Neil Hu


    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.

    Wednesday, December 27, 2017 8:27 AM
    Moderator