|
-
January 6th, 2009, 04:39 PM
#1
general question about static classes
What I've created seems to work fine, but I'm wondering if it's ok to do it this way... basically I'm using a static method in a static class to set some default properties for a MailMessage.
In another method I create two (or more) MailMessages using that static method. Since CreateMessage returns a new MailMessage, does that actually create separate instances, or do all MailMessages created this way refer to the same object because it's a static method (I recall from my C# class only one copy of a static class/method exists)? It all seems to work fine, but what I remember from my C# class makes me think this isn't proper.
Code:
public static class Email
{
private static MailMessage CreateMessage()
{
MailMessage mm = new MailMessage()
//set some properties for mm
return mm; }
public static void New(int issueID)
{
//create first email
MailMessage mailToTech = CreateMessage();
//set whatever other properties for mailToTech and send mailToTech
mailToTech.Dispose();
//create another email
MailMessage mailToUser = CreateMessage();
//set whatever properties and send mailToUser
mailToUser.Dispose(); } }
Last edited by chrisma; January 6th, 2009 at 07:25 PM.
-
January 6th, 2009, 04:41 PM
#2
Re: general question about static classes
1) Please edit your existing post to use code tags, before making any additional posts.
2) The code (Which is very hard to read) seems that each call to the static method will generate a new instance. I will look again once it is formatted and in code tags.
3) Please go back and enable Priave Messaging in your control panel!!!!!)

TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 6th, 2009, 05:16 PM
#3
Re: general question about static classes
-
January 6th, 2009, 05:57 PM
#4
Re: general question about static classes
Yes, but to save yourself some time trying to format variable width fonts, check out this line (from CPU's sig):
Join the fight, refuse to respond to posts that contain code outside of [ code] ... [ /code] tags.
-
January 6th, 2009, 06:15 PM
#5
Re: general question about static classes
Aww, help the poor fella out. 
 Originally Posted by chrisma
What I've created seems to work fine, but I'm wondering if it's ok to do it this way... basically I'm using a static method in a static class to set some default properties for a MailMessage.
In another method I create two (or more) MailMessages using that static method. Since CreateMessage returns a new MailMessage, does that actually create separate instances, or do all MailMessages created this way refer to the same object because it's a static method (I recall from my C# class only one copy of a static class/method exists)? It all seems to work fine, but what I remember from my C# class makes me think this isn't proper.
Code:
public static class Email
{
private static MailMessage CreateMessage()
{ MailMessage mm = new MailMessage()
//set some properties for mm
return mm; }
public static void New(int issueID)
{ //create first email
MailMessage mailToTech = CreateMessage();
//set whatever other properties for mailToTech and send mailToTech
mailToTech.Dispose();
//create another email
MailMessage mailToUser = CreateMessage();
//set whatever properties and send mailToUser
mailToUser.Dispose(); } }
Well that was about useless trying to quote him and insert the code tags, cuz now its all italicized, even harder to read. Oh well..... How about you dont use static classes and methods, and just create a single instance of that non-static class. Then you can refer to that single instance whenever you want.
Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Email myEmail = new Email()
public Form1()
{
InitializeComponent();
}
}
}
Last edited by Traps; January 6th, 2009 at 06:21 PM.
-
January 6th, 2009, 06:19 PM
#6
Re: general question about static classes
I used the [INDENT] tags in the editor.... and I don't see a sig for CPU, what am I missing?
-
January 6th, 2009, 06:37 PM
#7
Re: general question about static classes
Because there's no need to create separate instances, and I don't want to have to instantiate an instance just to use it... isn't that the purpose of static classes?
Plus this is actually for an ASP webpage, and having it as a data member in the page class can create unnecessary postback data. Plus if this was a Forms app and several forms needed to use it, now you'd have multiple instances that don't need to store unique states taking up memory just to use the same methods.. seems very inefficient for many reasons.
 Originally Posted by Traps
How about you dont use static classes and methods, and just create a single instance of that non-static class. Then you can refer to that single instance whenever you want.
Last edited by chrisma; January 6th, 2009 at 07:01 PM.
-
January 6th, 2009, 07:29 PM
#8
Re: general question about static classes
 Originally Posted by chrisma
I used the [INDENT] tags in the editor.... and I don't see a sig for CPU, what am I missing?
Now I see sigs... seems like that would be an option on by default.
-
January 6th, 2009, 08:28 PM
#9
Re: general question about static classes
So many things "should" be a default (IMHO) but that is another subject....
Looking at your code, it appears that you really DO want to send different e-mails. So a static instance is probably going to cause more harm than good.
People are oftent "shy" about creating instances (especially coming for other languages), but (in general) the best .Net paradigm is
1) Create
2) Use
3) Let Go (ie have no outstanding references)
As fast as possible.
Remember the time a GC takes is directly related to the number of surviv8ing objects (and the complexity of theuir relationships) and has nothing to do with the number of "unreachable" (garbage) objects. This is the exact opposite of Reference Counting type implementations.
Most (but by no means all) static classes are those which do NOT have ANY state (ie they have methods, but not properties or fields). For instances where you want "1 of some class", then the Singleton pattern is much more appropriate (in the vas majority of cases.
Of course there is always the possibility that I am missing something, so if you have "special" needs in your program, or dont understand what I posted, please provide the information or questions..
--soory it took a bit to get back to you, but we all have to eat sometime .
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|