how to apply more than one .Tag property in C#?

坚强是说给别人听的谎言 提交于 2019-12-24 01:36:08

问题


when I want to store/pass a value, I always use the .Tag property. for example, when I store the value:

Form prosesEdit = new FormProsesChemicalRawWbEdit();
                        prosesEdit.Tag = (int)this.proses_chemicalDataGridView.Rows[e.RowIndex].Cells[1].Value;
                        prosesEdit.ShowDialog();

Then, I would pass the value into my EDIT form like this:

proses_chemical_id = (int) this.Tag;
            this.proses_chemicalTableAdapter.FillByChemId(this.mcd_softwareDataSet.proses_chemical, proses_chemical_id);
            this.proses_chemical_listTableAdapter.FillByChemId(this.mcd_softwareDataSet.proses_chemical_list, proses_chemical_id);

but recently, I was supposed to store & pass 2 different values. let's say (int)this.proses_chemicalDataGridView1.Rows[e.RowIndex].Cells[1].Value; and (int)this.proses_chemicalDataGridView2.Rows[e.RowIndex].Cells[1].Value; how exactly do I do that? Thanks


回答1:


Both of the answers given so far will work but I want to elaborate on the answers a little and provide an alternative. Let's say you create your own class like this:

public class YourClass
{
    public int ProsesChemicalId1 { get; set; }
    public int ProsesChemicalId2 { get; set; }
}

Then you can assign an instance of your class to the Tag property like this:

YourClass yourClass = new YourClass();
yourClass.ProsesChemicalId1 = this.proses_chemicalDataGridView1.Rows[e.RowIndex].Cells[1].Value;
yourClass.ProsesChemicalId2 = this.proses_chemicalDataGridView2.Rows[e.RowIndex].Cells[1].Value;

Form prosesEdit = new FormProsesChemicalRawWbEdit();
prosesEdit.Tag = yourClass;
prosesEdit.ShowDialog();

And you can get the instance of the class back out of the Tag property like this:

yourClass = (YourClass) this.Tag;
this.proses_chemicalTableAdapter.FillByChemId(this.mcd_softwareDataSet.proses_chemical, yourClass.ProsesChemicalId1);

However, it's generally bad practice to use the Tag property to pass values around in forms because it requires a lot of type casting and knowledge of what is stored in each tag.

A more robust way is to use constructor injection since you know the type of form you are creating:

int value1 = this.proses_chemicalDataGridView1.Rows[e.RowIndex].Cells[1].Value;
int value2 = this.proses_chemicalDataGridView2.Rows[e.RowIndex].Cells[1].Value;

Form prosesEdit = new FormProsesChemicalRawWbEdit(value1, value2);  
prosesEdit.ShowDialog();

You would then most likely have to store those values as properties or fields inside the form. The reason this is a more robust approach is that it's resistant to accidental change. In other words, if you need to pass in a 3rd value in future it's less likely you'll forget to change code that needs changing.

You could also use property injection here but I'm not sure of your requirements so I'll leave it to you to decide.




回答2:


Create a new class for it. Create two properties in it. Send object of that class in tag. Tomorrow if third value is required, then add a new property.




回答3:


You can do as already suggested and make a class which holds two+ fields - OR - create an list (or an array, whatever suits best) on the tag:

this.Tag = new List<int>();
((List<int>)this.Tag).Add(value1);
((List<int>)this.Tag).Add(value2);
...

With few values casting isn't a problem, but if you need many values you would of course handle the list externally of the tag and then set it on the tag when finished.

(I'm not really a c# guy so apologies in advance for possible syntax errors)




回答4:


Store your two values in a Tuple and assign that to the tag.



来源:https://stackoverflow.com/questions/13468241/how-to-apply-more-than-one-tag-property-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!