将参数传递给方法 [英] Passing parameter to a method

查看:88
本文介绍了将参数传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,还在这里学习所以请耐心等待!



我试图将参数传递给我的方法,但不断得到编译错误



错误1 - 当前背景下不存在'vehicleMake'这个名称



以下代码,任何帮助都很多感谢,请简化您的答案,因为我还是新手,谢谢。



Hi folks, still learning here so please bear with me!

I am trying to pass a parameter to my method, but keep getting an complier error

Error1 - The name 'vehicleMake' does not exist in the current context

Code below, any help much appreciated, please simplify your answers as I’m still new to this, thanks.

public void RegReturn()
{

    string regNo;
    regNo = txtReg.Text;

    GetVehicleData RegSearch = new GetVehicleData();

    {
        RegSearch.GetTyreSize(regNo);
        TextBox1.Text = vehicleMake;

    }
}


public string GetTyreSize(string regNo)
{
    var service = new TyrescannerWebApp.com.carwebuk.www1.CarweBVRRWebService();
    var vehicleData = service.strB2BGetVehicleByVRM("regNo, "0.31.test");
    var vehicleMake = vehicleData.SelectSingleNode("//Vehicle/Combined_Make").InnerText;

    return vehicleMake;

}

推荐答案

读取范围 [ ^ ]变量以解决为什么你不能从调用方法访问方法中的变量。因为你有返回值,你可以使用如下所示。

read Scope [^] of variable to undestand why you can't access varible inside your method from calling method. since you have return value, you can use that as below.
string vehicleMake = RegSearch.GetTyreSize(regNo);
//use the return value from your method 
TextBox1.Text = vehicleMake;


当你使用它超出其范围时,很明显会出现此错误。你只能在GetTyreSize方法中使用它。



简单地使它成为全球(并不总是一个好方法)或返回它(好方法),如:



Its obvious to get this error as you are using it going beyond its scope. You can use it inside GetTyreSize Method only.

Simply make it Global(not always a good approach) or return it (good approach) like:

TextBox1.Text = RegSearch.GetTyreSize(regNo);





希望有所帮助:)



Hope it helps :)


这篇关于将参数传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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