Click to See Complete Forum and Search --> : general question about static classes


chrisma
January 6th, 2009, 03:39 PM
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.


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();
}
}

TheCPUWizard
January 6th, 2009, 03:41 PM
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!!!!!)

:wave::wave:

chrisma
January 6th, 2009, 04:16 PM
Does that look better?

BigEd781
January 6th, 2009, 04:57 PM
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.

Traps
January 6th, 2009, 05:15 PM
Aww, help the poor fella out. :wave:

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.

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.

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

private Email myEmail = new Email()

public Form1()
{
InitializeComponent();
}
}
}

chrisma
January 6th, 2009, 05:19 PM
I used the [INDENT] tags in the editor.... and I don't see a sig for CPU, what am I missing?

chrisma
January 6th, 2009, 05:37 PM
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.

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.

chrisma
January 6th, 2009, 06:29 PM
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.

TheCPUWizard
January 6th, 2009, 07:28 PM
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 ;) :wave:.