简体   繁体   中英

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). 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!). 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. 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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