Hi,
OK, after working on this for a while I've come to this:
I have a simple class, something like this:
class MyClass
{
private int a;
private int b;
public MyClass()
{
a = 5;
b = 2;
}
public MyClass(int first, int second)
{
a = first;
b = second;
}
public int GetOneField(int Fieldindex)
{
switch (Fieldindex)
{
case 1:
return a;
break;
case 2:
return b;
break;
default:
return 0;
break;
}
}
}
And another class which contains an array of type MyClass, like this:
class MyClassRecords
{
private MyClass[] RecordsArray;
private MyClass OneRecord;
private static int index = 0;
public MyClassRecords()
{
OneRecord = new MyClass();
RecordsArray = new MyClass[3];
}
public MyClass[] GetRecordsArray()
{
return RecordsArray;
}
public void Insert()
{
RecordsArray[index] = OneRecord;
index++;
}
public void RecordSet(int a, int b)
{
OneRecord = new MyClass(a, b);
}
}
What I want is to insert data to the records array at the beginning, let's
say, in Form1, and after that use that data in some of the other forms of the
application.
In Form1 I have an instance of MyClassRecords like this:
private MyClassRecords Records = new MyClassRecords();
Then I insert data with parameters a,b which I received during the
application like this:
Records.RecordSet(a,b);
Records.Insert();
In other forms I want to use these data this way:
MyClassRecords Records = new MyClassRecords();
And then : if(Records.GetRecordsArray()[0].GetOneField(1) == 1)
{
//do something
}
I don't want to change the data in the other forms, only read and use the
data that has been inserted in Form1.
Now, of course this doesn't work, because here I have a new instance, but I
want to read the data that I've inserted in Form1.
In other words, the instance of MyClassRecords which I had in Form1 is no
longer "alive".
The question is, then, how to transfer information between forms?
In my case, I thought of making the class MyClassRecords as a singleton
class (a technique which I've learnt only few days ago) and this way its
array will always remain the same.
I don't know whether it's good to do it this way here.
Someone also told me I can pass the instance of MyClassRecords as a
parameter to the constructor of another form. Can I do it? And if so, is it
worth it? (considering I have to carry this instance with me through all the
forms of the application).
Sorry for the very long explanation,
Thanks a lot for your answer
There are a couple of problems with your code.
First, you create one instance of MyClass which you put in
MyClassRecords.OneClass. Then in Insert() you assign that one
instance to the next entry in the array. That means that each entry
in the array points to the same instance of the class. You need to
create a new instance each time.
Why is SetRecord separate from Insert? Don't you always want to call
both? If so, get rid of Insert and change SetRecord to:
public void RecordSet(int a, int b)
{
RecordsArray[index] = New MyClass(a, b);
index++;
}
Do you really want the class hard coded to have at most three
instances of the class? You at least need a check to make sure you
don't call Insert more than 3 times. I would probably use and
ArrayList or the generic List instead of the array.
I would pass the reference to the instance of MyClassRecords to the
other forms. You can to it either as a constructor parameter or by
calling a method in the other forms.