Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ppl

Ive got bit of a question as im trying to understand something. I dont need a code as i already found a walkaround for my problem, so im just trying to uderstand why this does not work.

Im gonna try to explain it in simplest way I can.
I have 2 forms:
- Form 1 is my main app menu form.
- Form 2 is my logger window that holds a big ListView control

Now Form 2 is only created when user presses specific tool strip button. But before user does that (actually that does not matter because it does not work even when form is created earlier) Im trying to feed that ListView in Form2 with some data. Ok so lest go with some code:

Form1:

C#
Form1 form = new Form1();
form.AddToListView(e.Name.ToString(), e.FullPath.ToString(), e.ChangeType.ToString());


Form2:

C#
internal void AddToListView(string fileName, string fileSourceLocation, string fileDestination)
{
    ListViewItem item = new ListViewItem();
    item.Text = fileName;
    item.SubItems.Add(fileSourceLocation);
    item.SubItems.Add(fileDestination);
    myListView.Items.Add(item);
}


I debugged whole thing and it passes all data to "myListView.Items.Add(item);". Im just surprised because it passes all the values to the last stage but nothing appears in my ListView. Like I mentioned earlier i did a little bit of walkaround, I wrote class that handles data exhchanging for me. But Im trying to learn something in here so i would like to know why my first attempt didnt work.

Than you all for Your time in advance. Sorry if I overcomplicated things.
Cheers
Posted

1 solution

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please see all other suggestions. If your application is pretty simple, the solution could be as simple as passing a reference to one form instance to another form and providing some internal (no need for public!) members on that form to be used in another form. In case of more complex projects and several cases of such trivial collaborations, this simple approach could mess up code and invite some bugs, due to lack of more strict encapsulation.

Good luck,
—SA
 
Share this answer
 
v2
Comments
Maciej Los 26-Sep-12 16:45pm    
+5!
Sergey Alexandrovich Kryukov 26-Sep-12 16:52pm    
Thank you, Maciej.
--SA
Wajrak_ 27-Sep-12 13:41pm    
Thank You for a very quick reply. I read it all, and yes it works like a charm. I do get idea how to pass data between forms, but i still have one more question and i hope You dont mind answering it. I graduated uni with masters in computer programming but that was years and years ago and well i abandoned programming for many years (ill kept doing little projects nothin major) so Im "rusty" as hell, but Im not a complete noob. Enough excuses. Can You tell me what is disadvantage of passing data using (sorry i dont have "post code option in comment" so i uploaded it to pastebin) this code:
http://pastebin.com/j9a2S9D4
towards using delegates, props etc.
Cheers
Sergey Alexandrovich Kryukov 27-Sep-12 16:05pm    
First, I like your "enough excuses" attitude. You understand. Even if you have a permanent job, you can get somewhat rusty of you have to do the same things for extended period of time. In programming, though, this is easier: you can often take more then one activity at work; and you can do some work at home. So, in my experience, nothing helps more then doing some projects of your own: hobby, to help children, relatives/friends, etc. Writing to CodeProject is very useful, by the way...

I saw your code. Here is the problem: you use a static member where you could avoid it. Static members adds problems in bigger projects, so, they are best avoided unless really needed. Also, there is no "internal" in the static fileObject declaration, but probably it is actually meant to be public or internal (public would be excessive, so better use internal). But it would mean non-private field, which lacks flexibility. It's better to allow only private fields, non-provide could be just properties and methods and events. For example, working with a static object will require locking if accessed from different threads. You could improve it. Here are the alternatives I can see:

1) Make fileWatcher a member of two forms, but the instance member. Create it outside the forms and pass the reference to both. As this is a reference type, you will have the same object referenced by two form, even though the two members are separate.

2) From your code, it looks like the use of this field is symmetric in the use of fileWatcher. But usually it is assymmetric: for example: both forms add event data, but only one uses it. In this case, keep this object only in one form, the one which uses it more. Provide and implement some interface used only by the second (less using) form, and provide only the minimally requires set of operations.

3) Advanced way of doing (1). Hide the fileWatcher from both forms. Create a "fileWatchingManager" (or something), encapsulating all possible operations with file watching, including resulting data. Provide some interface for both forms, or two separate interfaces (with common base interface) to pass to each form separately. This interface shall not expose the instance fileWatcher at all; you expose only semantic operations like "Add", "Get", or... whatever. Create its instance in local context (like Main) and pass it two form 1 and form 2. The reference on local context will be lost (unreachable), but the two form will, again, have two referenced to the same object.

Use the benefit of having reference-type object and CG -- no need to worry what is the composition for what.

Any questions?
--SA
Wajrak_ 13-Nov-12 16:30pm    
First of all, thank You for Your time and quick reply. Second of all i do apologise for late reply, my work requires a lot of traveling so i have to prioritise. Ive got no questions, your explanation is very clear for me. Im glad there are still ppl who are willing to explain something without taking things for granted or abusing other ppl by saying "you should know this" or "google" it. Again thank you
- Wajrak

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900