cannot create an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • memo85
    New Member
    • Feb 2008
    • 1

    cannot create an object

    Code:
    namespace ResourcePack {
    
    internal class ResourcePackBuilder
    			{
    public static readonly ResourcePackBuilder Instance = new ResourcePackBuilder();
    
    public string Build(){
    return "Hello";
    }
    }//end ResourcePackBuilder
    
    }//end namespace
    i can't create an object of instance and call a method Build();
    (i.e method build doesn't be executed)
    this is the declaration in other class :[When trying to call Build()]
    [codde]
    string st = ResourcePack.Re sourcePackBuild er.instance.Bui ld();
    [/codde]
    Note: working under Vista Os, C#.Net Frmework 1.1, Windowa Application
  • todashah
    New Member
    • Feb 2008
    • 26

    #2
    Dear how u r able to create object of object ? bcoz Instance is an object
    of ResourcePackBui lder class & u try to create object of Instance which
    is not possible because it is an object & u can call Built() function using this Instance
    object.

    Originally posted by memo85
    Code:
    namespace ResourcePack {
    
    internal class ResourcePackBuilder
    			{
    public static readonly ResourcePackBuilder Instance = new ResourcePackBuilder();
    
    public string Build(){
    return "Hello";
    }
    }//end ResourcePackBuilder
    
    }//end namespace
    i can't create an object of instance and call a method Build();
    (i.e method build doesn't be executed)
    this is the declaration in other class :[When trying to call Build()]
    [codde]
    string st = ResourcePack.Re sourcePackBuild er.instance.Bui ld();
    [/codde]
    Note: working under Vista Os, C#.Net Frmework 1.1, Windowa Application

    Comment

    • jjvainav
      New Member
      • Feb 2008
      • 25

      #3
      Code:
      namespace ResourcePack {
       
      internal class ResourcePackBuilder
                  {
      public static readonly ResourcePackBuilder Instance = new ResourcePackBuilder();
       
      public string Build(){
      return "Hello";
      }
      }//end ResourcePackBuilder
       
      }//end namespace
      It appears to me as though you are trying to implement a singleton pattern. I would create your ResourcePackBui lder class as such

      Code:
      internal class ResourcePackBuilder
      {
          private static ResourcePackBuilder instance;
      		
          private ResourcePackBuilder()
          {
          }
      		
          public static ResourcePackBuilder Instance
          {
              get
              {
                  if(instance == null)
                  {
                     instance = new ResourcePackBuilder();
                  }
      				
                  return instance;
              }
          }
      		
          public string Build()
          {
              return "Hello";
          }
      }
      And then make the following call:

      Code:
      string result = ResourcePackBuilder.Instance.Build();
      I always setup my singleton classes like above, this way, you only create your single instance if ResourcePackBui lder.Instance is called during runtime.

      I hope this helps

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by jjvainav
        I always setup my singleton classes like above, this way, you only create your single instance if ResourcePackBui lder.Instance is called during runtime.

        I hope this helps
        That is probably one of the better examples of a singleton class creation I have seen.

        Comment

        Working...