简体   繁体   English

在C#中返回变量

[英]Returning variables in C#

This is my method that's in my class 这是我班上的方法

public void tostringmeth(string concat)
    {
      // string concat;
        string markstring;
        string matricstring;

        markstring = "";
        matricstring = "";

        Mark.ToString(markstring);
        Matric.ToString(matricstring);


       concat= FirstName + " " + SecondName + " " + DoB+ " " + Course+ " " + markstring + " " + matricstring ;

        return;

and essentially I need the "concat" variable to be returned to the main form so I can use it (it's only displaying it in a lable). 实际上,我需要将“ concat”变量返回到主表单,以便我可以使用它(它仅在标签中显示)。 How would I go about getting it accessible? 我将如何获取它? Decalaring it a public string throws an error for some reason. 将其声明为公共字符串会出于某种原因引发错误。 EDIT: I've got it now, it was a very obvious and stupid mistake (public STRING not void!). 编辑:我现在知道了,这是一个非常明显和愚蠢的错误(公共STRING不无效!)。 Thanks everyone! 感谢大家!

public string tostringmeth(string concat)
{
    // do your thing

    return concat;
}

And in your form: 并以您的形式:

MyLabel.Text = tostringmeth(myConcatString);

You are passing the concat field to your Method but doing nothing the value that it contains. 您正在将concat字段传递给您的Method,但不执行其包含的值。 I see no reason to pass it in to the Method. 我认为没有理由将其传递给方法。 I would do something like this: 我会做这样的事情:

public string tostringmeth() 
{ 
    string markstring; 
    string matricstring; 

    markstring = ""; 
    matricstring = ""; 

    Mark.ToString(markstring); 
    Matric.ToString(matricstring); 


   return FirstName + " " + SecondName + " " + DoB+ " " + Course+ " " + markstring + " " + matricstring ; 

}

Two options: 两种选择:
First, the better option, return the desired string value. 首先,更好的选择是返回所需的字符串值。 (I've simplified the method logic a bit) (我已经简化了方法逻辑)

  public string tostringmeth()    
  {    
       return FirstName + " " + SecondName + " " + 
             DoB + " " + Course + " " +
             Mark.ToString(string.empty) + " " + 
             Matric.ToString(string.empty);
  }

Second, modify the method input parameter (must declare method parameter as ref) 其次,修改方法输入参数(必须将方法参数声明为ref)

  public void tostringmeth(ref string concat)    
  {    
       concat = FirstName + " " + SecondName + " " + 
             DoB + " " + Course + " " +
             Mark.ToString(string.empty) + " " + 
             Matric.ToString(string.empty);
  }

Their are two simple ways. 它们是两种简单的方法。 first I think is the simplest... 2nd already told is simple as well :) 首先,我认为这是最简单的...第二个已经告诉过的也很简单:)

public string stVar; // It will not give error. because it is not in any function 
                   //it is in the scope of class. In this way no need to return

modifiers (like public/private etc) can not be used with variables of a method(function). 修饰符(例如public / private等)不能与method(function)的变量一起使用。 They can be used easily with class members(class variables, declared out of any function brackets) 它们可以轻松地与类成员一起使用(类变量,在任何功能括号之外声明)

public void tostringmeth(string concat)
{
  // string concat;
    string markstring;
    string matricstring;

    markstring = "";
    matricstring = "";

    Mark.ToString(markstring);
    Matric.ToString(matricstring);


   stVar= FirstName + " " + SecondName + " " + DoB+ " " + Course+ " " + markstring + " " + matricstring ;

}

2nd way already told 第二种方式已经告诉

public string tostringmeth(string concat)
{
  // string concat;
    string markstring;
    string matricstring;

    markstring = "";
    matricstring = "";

    Mark.ToString(markstring);
    Matric.ToString(matricstring);


   concat= FirstName + " " + SecondName + " " + DoB+ " " + Course+ " " + markstring + " " + matricstring ;
  return concat;
}

Function that returns something must be non-void type. 返回内容的函数必须是非空类型。

// void function cant return anything
public void tostringmeth(string inputParameter)
{
    return; // returns nothing 
}


// string function can return string
public string tostringmeth(string inputParameter)
{
     // string function can return string

     string outputParameter = something;
     return outputParameter; // returns variable value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM