none
Using C# Interfaces RRS feed

  • Question

  • I was reading some documentation on third-party controls on WinForms. The one I was looking at was a search control, basically it is just a textbox where you can type in some data and it will search against another control provided that the control is implementing the interface. So, let's say I have a grid control or even a listView control that implements  from  interface then I can now search their data.  

    { List<string> Data { get;} } public class FirstData : IData { public List<string> Data { get { List<string> data=new List<string>(); data.Add("a"); data.Add("b"); data.Add("c"); return data; } } } public class SecondData : IData { public List<string> Data { get { List<string> data = new List<string>(); data.Add("d"); data.Add("e"); data.Add("f"); return data; } } }

    //Getting the data

    public List<string> GetData(IData data)
    {
    return data.Data;
    }

    Though, this doesn't use a real control but i guess you get the idea.Code will work fine though but i am looking for a better way how to do this right. Other developers might check if a class implements the said interface but i really don't know that yet and how does it differ from my code above. 

    I also do have another concern using interfaces because i only know how to use interface when that interface does have a properties or methods. 

    Let's say i have a sample code like this

    public interface ISetting
    {
    
    }
    
    public class ApplicationSetting:ISetting
    {
        string Setting1 {get;set;}
        string Setting2 {get;set;}
    }
    
    
    public class AnotherSetting : ISetting
    { 
        string AnotherSetting1{get;set;}
        string AnotherSetting2{get;set;}
    }

    How do i use this, i only know that the class implements the interface but i really don't have an idea what are the properties of the class that implements the interface.

    Regards







    • Edited by Dikong42 Saturday, October 27, 2018 5:04 PM
    Saturday, October 27, 2018 4:35 PM

All replies

  • Hi Dikong42,

    for the first part of your question:

    "Though this doesn't use a real control but I guess you get the idea. The code will work fine though but I am looking for a better way how to do this right. "

    the interface is already implemented as it should the only thing that you should change is your properties getters : 

    public class FirstData: IData
    {
            // always initialize backing fields in the constructor
            // 
            public FirstData()
            {
                    data = new List<string>();
    		data.Add("a");
    		data.Add("b");
    		data.Add("c");
            }
            // consider using a backing field if you need to 
            // implement a kind of validation or any interaction while setting values 
            // otherwise auto-properties will generate the same IL with a backing field
    	List<string> data;
    	public List<string> Data
    	{
    		get
    		{
    			return data;
    		}	
    	}
    }
    

    for the second part about interfaces without members, those are used to "let say a flag for the class", which enables us to search them through reflection:

    var type = typeof(IEntity);
    var types = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(s => s.GetTypes())
        .Where(p => type.IsAssignableFrom(p));

    here is an article explaining the situation: get C# classes implementing interface,

    Best Regards,

    Mouad.

    Good Coding;

      
    Sunday, October 28, 2018 1:43 PM
  • Take a look at my blog post and see if it's helpful at all: 

    https://geek-goddess-bonnie.blogspot.com/2010/06/program-to-interface.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, October 29, 2018 1:07 AM
    Moderator
  • Greetings Dikong42.

    There is no reason I know of for creating an interface with no methods or properties. The whole point of an interface is to specify the methods and/or properties a class must have.

    Monday, October 29, 2018 2:17 AM
  • Hi,

    @Ante Meridian using empty interfaces is useful in some cases, for example to mark some classes and identify it again. It's used in a lot of frameworks...

    Greetings, Chris

    Monday, October 29, 2018 9:20 AM
  • Exactly. Marker interfaces.. which are from the OOP viewpoint a misuse of interfaces.

    In C# we should use attributes instead.

    Monday, October 29, 2018 9:31 AM
  • You must invest more time into naming. IData is poor interface name.

    Furthermore, DTO's have no interface, cause it makes no sense for data containers. Cause these have no methods.

    An empty ISetting interface is useless. This way it would make more sense:

    public interface ISetting
    {
        public void ReadFromFile(string fileName);
    }
    
    public class ApplicationSetting : ISetting
    {
        public string Setting1 { get; set; }
        public string Setting2 { get; set; }
        public void ReadFromFile(string fileName)
        {
            // TODO: Implement read file and assign values to SettingX properties.
        }
    }
    
    public class AnotherSetting : ISetting
    {
        string AnotherSetting1{ get; set; }
        string AnotherSetting2{ get; set; }
        public void ReadFromFile(string fileName)
        {
            // TODO: Implement read file and assign values to AnotherSettingX properties.
        }
    }

    But then the interface name is incorrect and we should consider something like this instead:
    public interface ISettingsSerializer
    {
        public void ReadFromFile(string fileName);
        public void SaveToFile(string fileName);
    }
    
    public class ApplicationSetting : ISettingsSerializer
    {
        public string Setting1 { get; set; }
        public string Setting2 { get; set; }
        public void ReadFromFile(string fileName)
        {
            // TODO: Implement read file and assign values to SettingX properties.
        }
        public void SaveToFile(string fileName)
        {
            // TODO: Implement save file.
        }
    }
    
    
    public class AnotherSetting : ISettingsSerializer
    {
        string AnotherSetting1 { get; set; }
        string AnotherSetting2 { get; set; }
        public void ReadFromFile(string fileName)
        {
            // TODO: Implement read file and assign values to AnotherSettingX properties.
        }
        public void SaveToFile(string fileName)
        {
            // TODO: Implement save file.
        }
    }

    Monday, October 29, 2018 9:39 AM