Peter Luong
Peter Luong

Reputation: 3

Return for each value in data table in class C#

I have class get Serial number using web service below:

  class SeriNumber
    {
        private string serialNumber;
        public SeriNumber(string serial,string Customer)
        {
            this.serialNumber = serial;
            if (Customer.Equals("ABC"))
            {
                Service.SoapClient b = new Service.SoapClient();
                string resultz = b.GetPanelSerializeResult(Customer,serial).Trim();
                var xml = resultz;
                using (var textReader = new StringReader(xml))
                {
                    var ds = new DataSet();
                    ds.ReadXml(textReader);
                    DataTable data = new DataTable();
                    data = ds.Tables[0];
                    foreach (DataRow row in data.Rows)
                    {
                      serial = row[3].ToString();
                      serialNumber = serial;
                    }
                }

            }
        }
        public string GetSeriNumber()
        {
            return serialNumber;
        }
    }

Code in Form

 SeriNumber seriNumber = new SeriNumber("1162200033501","ABC");
    string serial = seriNumber.GetSeriNumber();

I know the generic Serials has 24 serials child:

| Serinumber     |
| -------------- |
| 21162200033501 | 
| 21162200033502 | 
| 21162200033503 | 
| -------------- | 
| 21162200033523 | 
| 21162200033524 | 

I know Generic Series has 24 sub-serial and I want to get it. In the loop result get 24 serials but return I get only the last serial. Please help me correct it: for each sub-serials will return 1 time. so will return total 24 times.

Upvotes: 0

Views: 425

Answers (1)

JonasH
JonasH

Reputation: 36341

You likely need to add the sub-serials to a list or array instead of just overwriting the serial field repetedly. For example:

private List<string> subSerials = new ();
...
subSerials.Add(row[3].ToString());
...
public IReadOnlyList<string> SubSerials => subSerials;

However, I would be concerned about mixing different concerns into a single class. Doing complex work like calling a soap service in a constructor may not be the best idea. So I might recommend splitting the components fetching the serial number from the class that represents a serial number.

Upvotes: 2

Related Questions