Quantcast

Maximum PC

It is currently Sat Dec 20, 2014 4:22 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Making a class visible to entire app in Visual C#.Net?
PostPosted: Wed Jun 30, 2010 6:58 pm 
Willamette
Willamette

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1493
Is there a way I can make a custom class I made visible to the entire program that I'm writing (which consists of several Windows Forms and other class modules)?

I'm basically trying to get the "global variable approach" that was available in VB so that any form in my app can access those values.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2010 6:04 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
What are you trying to do?

If you have one Namespace you use across your entire application, you can. But then you lose the benefit of a tiered application and multiple namespaces.

http://msdn.microsoft.com/en-us/library/z2kcy19k(VS.80).aspx
http://www.csharp-station.com/Tutorials/Lesson06.aspx


Top
  Profile  
 
 Post subject: Re: Making a class visible to entire app in Visual C#.Net?
PostPosted: Thu Jul 01, 2010 11:49 am 
8086
8086

Joined: Tue Dec 15, 2009 11:27 am
Posts: 47
cbassett01 wrote:
Is there a way I can make a custom class I made visible to the entire program that I'm writing (which consists of several Windows Forms and other class modules)?

I'm basically trying to get the "global variable approach" that was available in VB so that any form in my app can access those values.


If you need the "global variable approach" in the first place, you should probably rethink your design.

global variables == tight coupling == bad


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2010 12:52 pm 
Willamette
Willamette

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1493
Yes, I know about this.

I actually found a way that works... create a static class and add static member variables to it. Then, if you create a public instance of the class in one of your class source-code files, the rest of the app should be able to see it. This is how I found out how to do it. Not the prettiest, but it does get the job done.

Basically, I needed to have a series of values that act as flags to tell me if a dialog box was closed (as in the user clicked either OK, Yes, No, Cancel, etc).

Can someone else think of a better way to get this value from another form via code?

Basically, Form1 opens Form2. Form1 waits until Form2 is closed. But I need to know which button (OK or Cancel) was pressed on Form2 so that Form1 can act accordingly without guessing or crashing the app.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2010 3:59 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
Do you want a class that is accessible to the app or a set of values?

If it is the latter, you just want global variables.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2010 5:12 pm 
Willamette
Willamette

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1493
Really just a set of global variables, but when I tried to just have global variables, c# would seem to complain that they needed to belong to a class. Whether it's a class or a set of variables, I don't care. I just need to be able to share values easily and quickly between classes and objects.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2010 6:04 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24238
Location: Granite Heaven
Yep, no global variables in C# apparently.

It seems you were on to the right idea. A static class (doesn't need to be instantiated and thus only one of each of its members will exist) with static (thus, unique) variables that is publically accessible. This should solve your problem.

Caveat: I know very little about C# and I did only about 4 minutes of google 'research' for this post. I could be so far out in left field that I left the stadium without noticing.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 01, 2010 10:02 pm 
Willamette
Willamette

Joined: Tue Feb 01, 2005 12:17 pm
Posts: 1493
well, the static class seems to be doing it for me. Although this may not be the best approach, it is the fastest (and simplest that I've found). The app doesn't really deal with a lot of data so there really is no data to be screwed up. It just takes SQL commands and sends them to the SQL server and then displays the results in a textbox. That's all.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2010 4:41 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 987
Location: Earth
Jipstyle wrote:
It seems you were on to the right idea. A static class (doesn't need to be instantiated and thus only one of each of its members will exist) with static (thus, unique) variables that is publically accessible. This should solve your problem..


Global variables are evil. Needless to say. A static class is similar in behavior as a VB module (last I checked, it's been a while and I don't use static classes).

cbassett01 wrote:
Basically, I needed to have a series of values that act as flags to tell me if a dialog box was closed (as in the user clicked either OK, Yes, No, Cancel, etc).

Can someone else think of a better way to get this value from another form via code?

Basically, Form1 opens Form2. Form1 waits until Form2 is closed. But I need to know which button (OK or Cancel) was pressed on Form2 so that Form1 can act accordingly without guessing or crashing the app.


The ShowMessageBox() method returns a DialogResult enum. Use that instead. If you're trying to capture the output of a form, why don't you try attaching an event in Form1 from Form2? That or create a property that returns the message it needs to return.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2010 6:43 am 
8086
8086

Joined: Tue Dec 15, 2009 11:27 am
Posts: 47
DJSPIN80 wrote:
Jipstyle wrote:
It seems you were on to the right idea. A static class (doesn't need to be instantiated and thus only one of each of its members will exist) with static (thus, unique) variables that is publically accessible. This should solve your problem..


Global variables are evil. Needless to say. A static class is similar in behavior as a VB module (last I checked, it's been a while and I don't use static classes).

cbassett01 wrote:
Basically, I needed to have a series of values that act as flags to tell me if a dialog box was closed (as in the user clicked either OK, Yes, No, Cancel, etc).

Can someone else think of a better way to get this value from another form via code?

Basically, Form1 opens Form2. Form1 waits until Form2 is closed. But I need to know which button (OK or Cancel) was pressed on Form2 so that Form1 can act accordingly without guessing or crashing the app.


The ShowMessageBox() method returns a DialogResult enum. Use that instead. If you're trying to capture the output of a form, why don't you try attaching an event in Form1 from Form2? That or create a property that returns the message it needs to return.


This.

What you're describing is simply a modal dialog box. If all you care about is what button was clicked, the DialogResult enum DJSPIN80 mentioned does exactly what you want. If you want a form that's not modal and need to know when someone clicks a button you'd need to add event handlers from Form1 to the button click events in Form2.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2010 3:42 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Further you could have any number of statuses returned from that Enum if you create a custom dialog form and set your buttons to either return the DialogResult value you are looking for, or set the Form.DialogResult property.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.