none
string this[string name] { get;}

    Question

  • what is the meaning of this code and how is it usefull  any idea??

    public interface IDataErrorInfo
    {
     
     string
    this[string name] { get;}

    }

    Thursday, June 10, 2010 3:33 PM

Answers

  •  
    public class MyBusinessClass
        {
            IDataErrorInfo myErrorInfo;
    
            public MyBusinessClass(IDataErrorInfo errorInfoClass)
            {
                myErrorInfo = errorInfoClass;
            }
    
            public string GetNameErrorInformation(string errorKey)
            {
                return myErrorInfo[errorKey];
            }
        }
     

     This will enforce implementing classes to provide an indexer to access internal list value.

     This is useful where you want to expect other classes getting some key-value kind of inforamation during the runtime.

     If you implement this interface on any class you will have to implement this indexer as well.

      

    public class TestInterface : IDataErrorInfo
        {
    
            private Dictionary<string, string> internalValue = new Dictionary<string, string>();
    
            public string this[string name]
            {
                get {
    
                    if (!internalValue.ContainsKey(name))
                        internalValue[name] = "New test value populated for " + name;
                    return internalValue[name];
                }
            }
    
           
        }
    

     So, if you are to implement a business process class which will expect your standard class (as per the contrat implemented by this interface) you can expect this indexer. You business class could be developed with this assumption as follwoing.

     

     

    Thursday, June 10, 2010 5:13 PM

All replies

  •  
    public class MyBusinessClass
        {
            IDataErrorInfo myErrorInfo;
    
            public MyBusinessClass(IDataErrorInfo errorInfoClass)
            {
                myErrorInfo = errorInfoClass;
            }
    
            public string GetNameErrorInformation(string errorKey)
            {
                return myErrorInfo[errorKey];
            }
        }
     

     This will enforce implementing classes to provide an indexer to access internal list value.

     This is useful where you want to expect other classes getting some key-value kind of inforamation during the runtime.

     If you implement this interface on any class you will have to implement this indexer as well.

      

    public class TestInterface : IDataErrorInfo
        {
    
            private Dictionary<string, string> internalValue = new Dictionary<string, string>();
    
            public string this[string name]
            {
                get {
    
                    if (!internalValue.ContainsKey(name))
                        internalValue[name] = "New test value populated for " + name;
                    return internalValue[name];
                }
            }
    
           
        }
    

     So, if you are to implement a business process class which will expect your standard class (as per the contrat implemented by this interface) you can expect this indexer. You business class could be developed with this assumption as follwoing.

     

     

    Thursday, June 10, 2010 5:13 PM
  • Thanks Sujit , your example  helped

    Monday, June 28, 2010 4:26 PM