CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Posts
    5

    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.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  3. #3
    Join Date
    Jan 2009
    Posts
    5

    Re: general question about static classes

    Does that look better?

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  5. #5
    Join Date
    Mar 2007
    Posts
    274

    Re: general question about static classes

    Aww, help the poor fella out.

    Quote Originally Posted by chrisma View Post
    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.

  6. #6
    Join Date
    Jan 2009
    Posts
    5

    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?

  7. #7
    Join Date
    Jan 2009
    Posts
    5

    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.

    Quote Originally Posted by Traps View Post
    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.

  8. #8
    Join Date
    Jan 2009
    Posts
    5

    Re: general question about static classes

    Quote Originally Posted by chrisma View Post
    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.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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
  •  





Click Here to Expand Forum to Full Width

Featured