3

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

  • Maybe you can store as comma separated or by another limiter, you can split it up and then use it – V4Vendetta Nov 20 '12 at 07:20
  • hmmm, I don't think that separating it with a comma is a right way to do it. because I have to store 2 different values then pass them both to my Load function so that both data are loaded – Aurelius Anugrah Sugianto Nov 20 '12 at 07:27

4 Answers4

4

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.

craftworkgames
  • 9,437
  • 4
  • 41
  • 52
  • So, I've tried your solution but the `Form prosesEdit = new FormProsesChemicalRawWbEdit(value1, value2);` throw an error saying that `MercindoLeather.Chemical.Proses_Chemical.Raw_WB.FormProsesChemicalRawWbEdit' does not contain a constructor that takes '2' arguments` – Aurelius Anugrah Sugianto Nov 21 '12 at 01:25
1

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.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
1

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)

J. Steen
  • 15,470
  • 15
  • 56
  • 63
  • Aye, almost. No worries, the gist was there. VB.NET and C# are still miles apart in some very specific cases. =) – J. Steen Nov 20 '12 at 09:53
0

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

Paul Mitchell
  • 3,241
  • 1
  • 19
  • 22