What is the meaning of `public string this[string columnName]` When implementing IDataErrorInfo interface?
Asked Answered
D

1

9

What is the meaning of public string this[string columnName] When implementing IDataErrorInfo interface?

    public string this[string columnName]
    {
        get
        {
            switch (columnName)
            {
                case "Name":
                    return ValidateName();
                case "PhoneNumber":
                    return ValidatePhoneNumber();
                default:
                    return string.Empty;
            }
        }
    }

I don't get it why there are square parentheses and what it does.


Answer: Thanks to Hans and Scott now I know that is simply the syntax of an indexer. More information here.

Delius answered 28/3, 2016 at 14:50 Comment(6)
What specifically do you not understand the this[string columnName] syntax or are you asking why they do it at all?Nose
Look in your favorite C# language book for "indexer".Symphonic
You have to understand IDataErrorInfo, and then you will understand the meaning of This[columnName], and I think this is why you got a downvote.Weinman
@ScottChamberlain: the syntax. I don't get it why there are square parentheses and what id does.Delius
@HansPassant: thanks, I will check it out.Delius
this was closed????Baroness
N
22

That is a C# indexer, it lets you use your class like

IDataErrorInfo myClass = new MyClass();
string phoneNumberErrorInfo = myClass["PhoneNumber"];`
Nose answered 28/3, 2016 at 15:6 Comment(1)
To the person who downvoted me, what about my answer was incorrect or not useful? I can't fix my question if you don't tell me what I have done wrong.Nose

© 2022 - 2024 — McMap. All rights reserved.