Results 1 to 5 of 5

Thread: [2.0] static variables in an app that can only open one file at a time

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    [2.0] static variables in an app that can only open one file at a time

    if my app can only have one file open at a time, is it a good idea to start sticking static methods all over my code? such as:

    Code:
    internal static class Program {
    	public static MyFileClass curFile;
    
    	// ...
    }
    Code:
    internal class MainForm : Form {
    	public static MainForm inst;
    
    	public MainForm() {
    		inst = this;
    
    		InitializeComponent();
    	}
    
    	// ...
    }
    i know that seems kind of vague, but i began coding a project and didn't plan well, so some methods took a MyFileClass as a parameter while some just used the static variable. Which would be a better idea?

    Thanks in advance,
    John

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] static variables in an app that can only open one file at a time

    The "static" key word doesn't just exist for convenience. If a variable, property or method is logically a member of the class, rather than a member of an instance of that class, then it should be declared static.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] static variables in an app that can only open one file at a time


  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    Re: [2.0] static variables in an app that can only open one file at a time

    doh! thanks. btw that's the best discussion of singletons in c# i've seen so far. thanks

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] static variables in an app that can only open one file at a time

    Yoda is wise.

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