为什么我的方法不起作用? [英] Why my method isnt working?

查看:98
本文介绍了为什么我的方法不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
            
        }

这是我的方法(),但事情是我称之为例子:GetVal(" VA VV_IN",vv_in)

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

我在变量vv_in中得到的值是0,当我使方法没有"double value"时而我写"vv_in = double.Parse(numberPart)我在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...

推荐答案

看起来你真的想要从方法中返回值?

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

在这种情况下你可以......通过将方法声明为返回double并分配结果来返回它在调用它时进入你的vv_in变量:

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);
            
        }

然后调用它:

vv_in = GetVal("VA VV_IN")

或者你可以将value参数声明为'ref'或'out'参数,但是当你只是将值作为方法结果返回时,这似乎是不必要的。

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.


这篇关于为什么我的方法不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆