Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否使用用户输入修改数组?_C#_Arrays - Fatal编程技术网

C# 是否使用用户输入修改数组?

C# 是否使用用户输入修改数组?,c#,arrays,C#,Arrays,我使用此代码在单击add按钮时将用户输入添加到数组中 int newcompartment; string newproductcode, newname, newaddress; float newweight; newcompartment = int.Parse(txtcompartment.Text); newproductcode = txtproduct.Text; newname = txtname.Text; newaddress = txtaddress.Text; neww

我使用此代码在单击add按钮时将用户输入添加到数组中

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;   
      }
}                    
我想知道的是,是否有可能有一个修改按钮,用户可以输入新的输入来替换数组中以前添加的部分或全部数据


为了以防万一,如果需要,我的GUI由5个文本框和2个单选按钮组成:隔间编号、产品代码、名称、重量、地址和占用/空。

如果删除嵌套的if-else,相同的逻辑难道不起作用吗?因此,您可以将其用作“添加/修改”按钮。如果它是一个新的车厢号,它将被添加。否则,如果它是旧的,那么它将得到更新。要卸下隔室,您只能接受隔室编号。如果该条目存在,请从所有数组中删除该条目

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;   
}
编辑:

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

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

你想要一个可以输入文本的按钮吗?你的问题是什么?是否要获取索引以修改数组数据?如果使用列表而不是字符串数组,则可以使用其.Add方法向其中添加项目。我希望允许用户在文本框和单选按钮中输入数据,单击“修改”按钮时,输入的新数据将替换旧数组数据。