471,627 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Help-document view architecture in c#

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...!

Nov 29 '05 #1
6 2687
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg" <gr*************@yahoo.co.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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...!

Nov 29 '05 #2
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.

Nov 29 '05 #3
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).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg" <gr*************@yahoo.co.uk> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

Nov 29 '05 #4
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.

Nov 29 '05 #5
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg" <gr*************@yahoo.co.uk> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.

Nov 29 '05 #6
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.

Nov 29 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom | last post: by
4 posts views Thread by Sarir Khamsi | last post: by
6 posts views Thread by wukexin | last post: by
3 posts views Thread by Colin J. Williams | last post: by
7 posts views Thread by Corepaul | last post: by
5 posts views Thread by Steve | last post: by
8 posts views Thread by Mark | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.