开发者

Class and static variable

开发者 https://www.devze.com 2023-04-02 21:14 出处:网络
OutputFormatsBase is base class for managing Output Formats. OutputFormats1 and OutputFormats2 classes are inherited from OutputFormatsBase class.

OutputFormatsBase is base class for managing Output Formats. OutputFormats1 and OutputFormats2 classes are inherited from OutputFormatsBase class.

I have the problem with static variable _listOfObjects which is in OutputFormatsBase class and can't find a way to solve it.

If I make _listOfObjects static everything works fine except that OutputFormats1 and OutputFormats2 classes static instances are shared, not good.

Could anyone suggest how to solve this? I completely lost.

public class OutputFormats1 : OutputFormatsBase
{
    public static readonly OutputFormats1 Bmp = new OutputFormats1 { Value = "BMP", FileExtension = "bmp", Id = 1 };
    public static readonly OutputFormats1 Jpg = new OutputFormats1 { Value = "JPG", FileExtension = "jpg", Id = 2 };
    public static readonly OutputFormats1 Png = new OutputFormats1 { Value = "PNG", FileExtension = "png", Id = 3 };
    public static readonly OutputFormats1 Tiff = new OutputFormats1 { Value = "TIFF", FileExtension = "tif", Id = 4 };
    public override OutputFormatsBase Selected { get; set; }
    public override OutputFormatsBase Default
    {
        get { return Png; }
    }
}

public class OutputFormats2 : OutputFormatsBase
{
    public static readonly OutputFormats2 Pdf = new OutputFormats2 { Value = "PDF", FileExtension = "pdf", Id = 1 };
    public override OutputFormatsBase Selected { get; set; }
    public override OutputFormatsBase Default
    {
        get { return Pdf; }
    }
}

public abstract class OutputFormatsBase
{
     private static readonly List<OutputFormatsBase> _listOfObjects = new List<OutputFormatsBase>();
     public string Value { get; protected internal set; }
     public string FileExtension { get; protected internal set; }
     public int Id { get; protected internal set; }

     public abstract OutputFormatsBase Selected { get; set; }
     public abstract OutputFormatsBase Default { get; }


     protected OutputFormatsBase()
     {
         _listOfObjects.Add(this);
     }


     public bool Validate(string format)
     {
         for (var i = 0开发者_StackOverflow社区; i < _listOfObjects.Count - 1; i++)
         {
             var outputFormats = _listOfObjects[i];
             if (format.ToLower() == outputFormats.Value.ToLower())
             {
                 Selected = outputFormats;
                 return true;
             }
         }
         return false;
     }
 }


You could try something like that :

public abstract class OutputFormatsBase<T> where T : OutputFormatsBase
{
 private static readonly List<T> _listOfObjects = new List<T>();

 protected OutputFormatsBase()
 {
  _listOfObjects.Add((T)this);
 }
}

You'll have one instance of _listOfObjects per template instanciation.


If you use generics then the static values will be unique to each generic type

public class OutputFormats1 : OutputFormatsBase<OutputFormats1> 
{ 
    public static readonly OutputFormats1 Bmp = new OutputFormats1 { Value = "BMP", FileExtension = "bmp", Id = 1 }; 
    public static readonly OutputFormats1 Jpg = new OutputFormats1 { Value = "JPG", FileExtension = "jpg", Id = 2 }; 
    public static readonly OutputFormats1 Png = new OutputFormats1 { Value = "PNG", FileExtension = "png", Id = 3 }; 
    public static readonly OutputFormats1 Tiff = new OutputFormats1 { Value = "TIFF", FileExtension = "tif", Id = 4 }; 
    public override OutputFormatsBase Selected { get; set; } 
    public override OutputFormatsBase Default 
    { 
        get { return Png; } 
    } 
} 

public class OutputFormats2 : OutputFormatsBase<OutputFormats2> 
{ 
    public static readonly OutputFormats2 Pdf = new OutputFormats2 { Value = "PDF", FileExtension = "pdf", Id = 1 }; 
    public override OutputFormatsBase Selected { get; set; } 
    public override OutputFormatsBase Default 
    { 
        get { return Pdf; } 
    } 
} 

public abstract class OutputFormatsBase<T> where T:OutputFormatsBase 
{ 
     private static readonly List<T> _listOfObjects = new List<T>(); 
     public string Value { get; protected internal set; } 
     public string FileExtension { get; protected internal set; } 
     public int Id { get; protected internal set; } 

     public abstract OutputFormatsBase Selected { get; set; } 
     public abstract OutputFormatsBase Default { get; } 


     protected OutputFormatsBase() 
     { 
         _listOfObjects.Add((T)this); 
     } 


     public bool Validate(string format) 
     { 
         for (var i = 0; i < _listOfObjects.Count - 1; i++) 
         { 
             var outputFormats = _listOfObjects[i]; 
             if (format.ToLower() == outputFormats.Value.ToLower()) 
             { 
                 Selected = outputFormats; 
                 return true; 
             } 
         } 
         return false; 
     } 
 } 
0

精彩评论

暂无评论...
验证码 换一张
取 消