ส่งกลับค่าหลายค่าจากชั้นเรียนหนึ่งไปยังอีกวิธีหนึ่ง

ฉันมีวิธีการส่งสตริงไปยังชั้นเรียน เพื่อเหตุผลในการทดสอบ ตอนนี้ฉันได้ใช้ปุ่มแล้ว ฉันค้นหาคำถามที่คล้ายกันในฟอรัมแล้ว แต่คำถามเหล่านั้นอ้างถึง php และสถานการณ์อื่น ๆ ที่ฉันไม่สามารถเข้าใจได้ คลาสจะตัดอักขระสองสามตัวออกจากสตริงและกำหนดค่าให้กับสตริงที่แตกต่างกัน 3 ชุด ขึ้นอยู่กับส่วนหัว ฉันต้องส่งคืน 3 สตริงนั้นกลับไปยังผู้โทรและฉันได้เขียนโค้ดไว้ดังต่อไปนี้

ผู้โทร:

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

รหัสทำงานได้โดยไม่มีปัญหา แต่ฉันไม่แน่ใจว่านี่เป็นวิธีที่ถูกต้องในการส่งคืนค่าหลายค่าจากคลาสหรือไม่ มีวิธีที่ดีกว่า?


person FeliceM    schedule 07.05.2013    source แหล่งที่มา


คำตอบ (1)


ฉันคิดว่า create class ดีกว่าในกรณีนี้

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

แล้ว

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

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

}

นี่คือวิธีที่คุณสามารถโทรได้

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

// เข้าถึงค่าต่างๆ ตามด้านล่างนี้

textBox7.Text = myclass.NumGiri;
person Damith    schedule 07.05.2013
comment
ขอบคุณ ฉันจะเปลี่ยนผู้โทรเพื่อรับค่าเหล่านั้นได้อย่างไร - person FeliceM; 07.05.2013
comment
ขอบคุณมาก. เข้าใจอย่างถ่องแท้ - person FeliceM; 07.05.2013
comment
+1 ต้องการใช้ตัวเลือกนี้มากกว่า Tuple เนื่องจากใช้งานได้จาก .Net Framework 4.0 เท่านั้น stackoverflow.com/questions/7120845/ - person DaniKR; 02.05.2016