Pergunta

I have a method passing a string to a class. For testing reasons I have used a button for now. I have searched in the forum for similar questions but they refer to php and other situation that I am unable to understand. The class strips out few characters from the string and assign the value to 3 different strings depending on the header. I need to return those 3 strings back to the caller and I have coded it as following.

Caller:

private void button4_Click(object sender, EventArgs e)
     {
     string a, b, c;
     string invia = textBox8.Text.ToString();
     Stripper strp = new Stripper();
     strp.Distri(invia, out a, out b, out c);
     textBox7.Text = a;
     textBox7.Text = b;
     textBox7.Text = c;}

Class:

class Stripper
{

 public  void  Distri (string inArrivo, out string param1, out string param2, out string param3)

    {
        string corrente="";
        string temperatura="";
        string numGiri="";
        string f = inArrivo;
        f = f.Replace("<", "");
        f = f.Replace(">", "");

        if (f[0] == 'I')
        {
       string _corrente = f;
            _corrente = _corrente.Replace("I", "");
            corrente = _corrente;
        }
      else if (f[0] == 'T')
        {
      string _temperatura = f;
             _temperatura = _temperatura.Replace("T", "");
              temperatura = _temperatura;
        }
        else if (f[0] == 'N')
        {
         string _numGiri = f;
            _numGiri = _numGiri.Replace("N", "");
             numGiri = _numGiri;
        }
        param1 = corrente;
        param2 = temperatura;
        param3 = numGiri;
        }
       }
      }

The code is working without problems but I am not sure if this is the correct way to achieve the returning of multiple values from a class. Is there a better way?

Foi útil?

Solução

I think create class is better in this case

   public class MyClass
    {
        public string Corrente { get; set; }
        public string Temperatura { get; set; }
        public string NumGiri { get; set; }
    }

then

public MyClass Distri(string inArrivo)
{
    // your code

    MyClass myclass = new MyClass() {
        Corrente = corrente, 
        NumGiri = numGiri, 
        Temperatura = temperatura 
    };
    return myclass;

}

this is how you can call

Stripper strp = new Stripper();
MyClass myclass = strp.Distri(invia);

// access values as below

textBox7.Text = myclass.NumGiri;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top