James Dawson
James Dawson

Reputation: 5409

Calling a class method from form with no relation to class

Sorry for the bad title, my mind is full of confusion and I don't know how best to describe it. Here goes.

I have a class called FileHandler. This class reads data from a file and puts it into a List, and it also has a method to write the List back to the file after I've made modifications to it. The class looks like this:

http://pastebin.com/wei5FFpB

Now, the readDataFile method of that class is called in my main form (the one that initially loads when you run the program). From there, I open another form called BranchOverview. From there, another one can be opened called EditProduct. Within this edit product form, I can edit the product details by reference and have it change in the myArgus list. What I'm having trouble with is calling the readDataFile method in the FileHandler class from EditProduct.

Since I have no way to reference myArgus from EditProduct, I can't call the writeDataFile method because it accepts myArgus as a parameter.

The only way I can think of doing it is having a method in each form class that calls the class below it, all the way down until it gets to my main form where myArgus can be passed.

Is there a graceful way to achieve this or have I completely screwed up my entire program in terms of design? If I have, I'd appreciate being pointed in the right direction on how to achieve this.

Thanks!

Upvotes: 1

Views: 118

Answers (2)

psubsee2003
psubsee2003

Reputation: 8751

In addition to Wiktor's answer, your StreamReader and StreamWriter objects should be wrapped in a using statement so they are disposed of properly, even if there is an error.

In your readDataFile method:

using(StreamReader reader = new StreamReader(dataFileLoc))
{

    int branchCounter = 0;
    while (reader.Peek() >= 0)
    {
        .
        .
        .
    }
}

In your writeDataFile method:

using(StreamWriter writer = new StreamWriter(dataFileLoc))
{
    for (int i = 0; i < myArgus.Count; i++)
    {    
        .
        .
        .
    }
}

Upvotes: 0

Wiktor Zychla
Wiktor Zychla

Reputation: 48314

If I understand correctly, your concern is sharing the instance of myArgus between three (or more) forms.

But there are at least few different ways to do it. You can pass this between forms (e.g. as a constructor parameter), you can pass instance of one form to another, you can have a separate class to hold a shared (static) instance of myArgus.

There are other minor problems in your code. For example, you don't really have to pass lists by ref since you are not changing the reference itself but rather the contents of your lists.

Then, this looks ugly:

myArgus.Add(new Branch());
myArgus[branchCounter]._branchID = Convert.ToInt32(reader.ReadLine());
myArgus[branchCounter]._branchName = reader.ReadLine();
...

while you really mean:

Branch newBranch = new Branch();

newBranch._branchID = ...;
newBranch._branchName = ...;

myArgus.Add( newBranch );

Then, when a method is supposed to return the data (readDataFile) I think everyone would expect the signature to be:

public List<Branch> readDataFile()

instead of

public void readDataFile(ref List<Branch> myArgus)

Upvotes: 1

Related Questions