用户输入修改数组?

时间:2014-11-19 06:50:34

标签: c# arrays

单击“添加”按钮时,我正在使用此代码将用户输入添加到我的数组中。

int newcompartment;
string newproductcode, newname, newaddress;
float newweight;

newcompartment = int.Parse(txtcompartment.Text);
newproductcode = txtproduct.Text;
newname = txtname.Text;
newaddress = txtaddress.Text;
newweight = float.Parse(txtqty.Text);

if ((newcompartment > 0) && (newcompartment < productcode.Length) && (rbtnoccupied.Checked == true))
{
      if (productcode[newcompartment - 1] != "")
      {
           MessageBox.Show("Compartment is occupied !");
      }   
      else
      {
           MessageBox.Show("You have successfully added a new array data!");
           compartmentno[newcompartment - 1] = newcompartment;
           productcode[newcompartment - 1] = newproductcode;
           name[newcompartment - 1] = newname;
           weight[newcompartment - 1] = newweight;
           address[newcompartment - 1] = newaddress;   
      }
}                    

我想知道的是,是否可以使用“MODIFY”按钮,用户可以输入新输入来替换阵列中部分或全部先前添加的数据?

如果需要,我的GUI包含5个文本框和2个单选按钮(compartment noproduct codenameweightaddress )&amp; (Occupied / Empty)。

1 个答案:

答案 0 :(得分:0)

如果删除嵌套的if else,那么逻辑是否会起作用?所以你可以用它作为ADD / MODIFY按钮。如果它是一个新的隔间号码,它将被添加。否则,如果它是旧的,那么它将得到更新。要卸下隔间,您只能接受隔间编号。如果存在,请从所有阵列中删除该条目。

if ((newcompartment > 0) && (newcompartment < productcode.Length) ) 

//removed this condition
//&& (rbtnoccupied.Checked == true))

{
      //Remove this if else condition. Always update the values.
      //if (productcode[newcompartment - 1] != "")
      //{...}
      //else
      //{...}

      MessageBox.Show("You have successfully added/modified an array data!");
      compartmentno[newcompartment - 1] = newcompartment;
      productcode[newcompartment - 1] = newproductcode;
      name[newcompartment - 1] = newname;
      weight[newcompartment - 1] = newweight;
      address[newcompartment - 1] = newaddress;   
}

修改

更好的方法是创建一个名为Compartment的类型并使用可以修改的集合,而不是处理多个数组。

 public class Compartment
    {
        private int compartmentID;
        private string address;
        private string weight;
        private string name;

        public int CompartmentID
        {
            get { return compartmentID; }
            internal set { compartmentID = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Weight
        {
            get { return weight; }
            set { weight = value; }
        }           

        public string Address
        {
            get { return address; }
            set { address = value; }
        }
    }



 public class Form1
    {
        private List<Compartment> compartments;

        public void Form_Load()
        {
            compartments = new List<Compartment>();
        }

        private void AddCompartment(string name, string weight, string address)
        {
            Compartment newCompartment = new Compartment() { CompartmentID = compartments.Count + 1, Address = address, Name = name, Weight = weight };

            compartments.Add(newCompartment);
        }

        private void UpdateCompartment(int id, string name, string weight, string address)
        {
            //Update based on id
        }

        private void RemoveCompartment(int id)
        {
            compartments.Remove(id);
        }
    }