Help-document view architecture in c#

G

Greg

Is this a good idea? From a brief dabble with MFC years ago, I
understand that this is a worthy goal.


Therefore I'm trying to remove most of the guts of an app from the main
form code to another class. This class starts the program with
Application.Run(new frmMain()) within its static void Main() method.

However, ..... how can I get frmMain to update the data held within
this data class? Pass events back to it? Contain a refernce to the
class and access its properties? I'd like to be able to send frmMain a
reference to the class when I isntantiate it within Application.Run(new
frmMain()) but becuase void main is declared statically, this isn't an
option.

Feeling a little lost now...!
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

Why isn't it an option? To me, it would seem like you just create a
constructor for your form which would take an instance of the data class.
You can create an instance of the data class before you call
Application.Run, and then do this:

Application.Run(new frmMain(dataClass));

Hope this helps.
 
G

Greg

Thanks Nicolas
I'm pretty chuffed that you think it is an option - at least I'm not
barking up the wrong tree.

The problem is that I cant get the class ("clsApplicationData") to send
an instance of itself. the this keyword doesn't work since void main()
is static.
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

Um, so why not just create it, like so?

// Create the instance of the application data:
clsApplicationData appData = new clsApplicationData();

// Run the program.
Application.Run(new frmMain(appData));

Of course, this doesn't help if you want to have a universal instance.
In which case, you should expose a singleton of the app data, and then you
could do this:

// Get the singleton instance.
clsApplicationData appData = new clsApplicationData.Instance;

// Run the program.
Application.Run(new frmMain(appData));

Or, you could just access the instance directly in your form.

I don't see what the problem is.

Also, you might want to re-consider your naming of the classes/forms.
The suggested public naming conventions indicate you should not prefix class
names with an indicator of the type (cls, frm).
 
G

Greg

Thanks Nick.
Thanks also for the advice on naming conventions - I've just migrated
from VB so I haven't found my feet yet. For the purpose of this thread
though, I'll stick with what I've already used, to save confusion.

The issue is that it is an instance of clsApplicationData that
kickstarts the program. Therefore for your suggestion to work, this
would need to create a new instance of itself. That doesn't make sense,
does it? Maybe my form class should start the application and create an
instance of clsApplicationData. That would definitely solve the
problem, though it doesn't seem best practice to me - surely
clsAppplicationData should be running the whole show, not being created
by a class that is GUI related.
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

If you have a Main method on clsAppplicationData, then in that Main
method, no instance is created.

I prefer the approach taken by the template for the windows forms
application in VS.NET 2005. Basically, there is a class in your project,
"Program" which has the entry point "Main", which creates the instance of
your form class (which is separate) and then calls the static Run method on
the Application class, passing the instance. It allows you easily place
whatever other code you need for startup somewhere that is separate from
your UI code (or any other code, for that matter).
 
G

Greg

Excellent. That appears to have worked.
within the Program class:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

static void Main()
{
Application.Run(new frmMain(new clsApplicationData()));
}


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I'll see how it behaves tomorrow, but from the logic it would appear
that frmMain will know all about clsApplicationData, which is what I've
been striving for!

Thanks a lot Nicholas, I really appreciate your help.

Greg.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top