none
Adding Values to List in Class only Once

    Question

  • Hello,

    I have a class as follows:

      public class MyClass {
        private List<String> MyList;
      }

    I need to initialize MyList and add values to it.

    But I would like this to be done only once ...

    Should I use a static class? Or maybe making MyList static?

    And can I add the values using a private method?

    What is the best way to do this?

    Thank You,

    Miguel


    Thursday, November 29, 2012 4:05 PM

Answers

  • You can make MyList a static member. Now you can access the list using the class name as opposed to use objects.

    Can you please add more details, what is the use of MyList? So probably we will be able to give you more accurate solution.


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:10 PM
    Thursday, November 29, 2012 4:20 PM
  • If MyList must be unique for the class MyClass, make it static. If there must be a distinct list for each instance of MyClass, don't make it static.

    If all the members of a class are static, the class should probably be static.

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:10 PM
    Thursday, November 29, 2012 4:26 PM
  • Making MyList static would seem to be what you want.

    You could use a static constructor to fill the list - if you know the values before any instance is constructed. Otherwise a static member might be the way to go; it could check if the list has already been initialised and if so just return it, else populate it first.

    It does depend on just how it is being used in your app.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:32 PM
  • As per understanding static class is best. Please check again before implement static class or static list.

    Please find the static list example form he followin url.

    http://www.dotnetperls.com/static-list

    Please check the Advantages to Using Private Static Methods

    http://stackoverflow.com/questions/135020/advantages-to-using-private-static-methods

    http://stackoverflow.com/questions/4698919/which-is-better-private-static-vs-private


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!


    • Edited by Sambath Raj.C Thursday, November 29, 2012 4:37 PM
    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:34 PM
  • There are several ways of doing this. You can also use static constructor to initialize your static MyList using private static member.

    When you call your private method, it will automatically call the static constructor and also only once to initialize your MyList.

    Check some example here:

    http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx



    Mark it as helpful if so! Thanks

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:43 PM
  • It make sense..

    You can make_fileTypes as a static member. In the constructor you can use something like

    public FileTypeService() {
      if(_fileTypes == null) {
        _fileTypes = new FileTypeList();
        //Add a values here
      }
    }


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:58 PM

All replies

  • You can make MyList a static member. Now you can access the list using the class name as opposed to use objects.

    Can you please add more details, what is the use of MyList? So probably we will be able to give you more accurate solution.


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:10 PM
    Thursday, November 29, 2012 4:20 PM
  • If MyList must be unique for the class MyClass, make it static. If there must be a distinct list for each instance of MyClass, don't make it static.

    If all the members of a class are static, the class should probably be static.

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:10 PM
    Thursday, November 29, 2012 4:26 PM
  • Making MyList static would seem to be what you want.

    You could use a static constructor to fill the list - if you know the values before any instance is constructed. Otherwise a static member might be the way to go; it could check if the list has already been initialised and if so just return it, else populate it first.

    It does depend on just how it is being used in your app.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:32 PM
  • As per understanding static class is best. Please check again before implement static class or static list.

    Please find the static list example form he followin url.

    http://www.dotnetperls.com/static-list

    Please check the Advantages to Using Private Static Methods

    http://stackoverflow.com/questions/135020/advantages-to-using-private-static-methods

    http://stackoverflow.com/questions/4698919/which-is-better-private-static-vs-private


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!


    • Edited by Sambath Raj.C Thursday, November 29, 2012 4:37 PM
    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:34 PM
  • There are several ways of doing this. You can also use static constructor to initialize your static MyList using private static member.

    When you call your private method, it will automatically call the static constructor and also only once to initialize your MyList.

    Check some example here:

    http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx



    Mark it as helpful if so! Thanks

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:43 PM
  • Can you please add more details, what is the use of MyList? So probably we will be able to give you more accurate solution.



    Yes, I can provide a better example. Basically I have the following:

      public class FileTypeService {
    
        private FileTypeList _filesTypes = new FileTypeList();
    
        public FileTypeService() {
          Add(PackMark.AnnexInFile, "css", "text/css");
        }
    
        private void Add(PackMark mark, String extension, String mime, params String[] mimes) {
    
          _files.Add(mark, new Tuple<String, String, List<String>>(extension, mime, mimes.ToList()));
    
        } // Add
    
      }
    
      private class FileTypeList : Dictionary<PackMark, Tuple<String, String, List<String>>> { }

    I am using this class in many places of my application.

    But I do not want to initialize and fill the list every time I use it.

    Does this make sense?

    Thank You,

    Miguel



    Thursday, November 29, 2012 4:43 PM
  • It make sense..

    You can make_fileTypes as a static member. In the constructor you can use something like

    public FileTypeService() {
      if(_fileTypes == null) {
        _fileTypes = new FileTypeList();
        //Add a values here
      }
    }


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    • Marked as answer by MDMoura Thursday, November 29, 2012 5:09 PM
    Thursday, November 29, 2012 4:58 PM
  • I see ... And what about the following approach:

      public class FileTypeService {
    
        private static FileTypeList _filesTypes = new FileTypeList();
    
        static FileTypeService() {
          Add(PackMark.AnnexInFile, "css", "text/css");
        }
    
        private static void Add(PackMark mark, String extension, String mime, params String[] mimes) {
    
          _files.Add(mark, new Tuple<String, String, List<String>>(extension, mime, mimes.ToList()));
    
        } // Add
    
      }
    
      private class FileTypeList : Dictionary<PackMark, Tuple<String, String, List<String>>> { }

    Is this a problem?

    Thank you,

    Miguel

    Thursday, November 29, 2012 5:01 PM
  • Sounds good.

    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    Thursday, November 29, 2012 5:06 PM
  • You could simplify your class by using a collection initializer:
    private static FileTypeList _filesTypes = new FileTypeList {
        {
            PackMark.AnnexInFile,
            new Tuple<String, String, List<String>>("css", "text/css", new List<string>())
        }
    };

    Friday, November 30, 2012 8:55 AM