2

I have class which have properties.

For Example

class Caption
{  
   public string AR{get;set;}
   public string EN{get;set;}
   pbulic string Tu{get;set;}   
}

i want for example Assign Caption object to vairable directly without need to call property

I want use This

Caption caption=new Caption();
string myVar = caption;   // this will return EN property directly

Rather Than

string myVar = caption.En;

I don't know but i think there's overload string operator or something for targeted class itself.

Why i need this ? just for localization's purposes and more readability in localization object. rather every time creating switch(language) then assign caption.EN, Caption.AR. it will be ugly.

2
  • You can add a constructor that accepts, for example, a CultureInfo argument. Override ToString() to return the Caption in the language specified in the constructor. You can store all the strings corrensponding to a text in a specific culture in a Dictionary instead of using a public property for each culture. It's easier to load from a JSON file, too. Commented Oct 13, 2019 at 19:01
  • You can override the ToString() method in the Caption class, but then you would have to say string myVar = caption.ToString() to access it. Commented Oct 13, 2019 at 19:02

1 Answer 1

2

The cleanest way might be to implement an indexer. That way you can switch a single time, do whatever checks on the language, handle issues, deal with defaults, etc. inside the Caption class without the ceremony of calling a method, like so:

class Caption
{
    public string AR { get; set; }
    public string EN { get; set; }
    public string Tu { get; set; }

    // var cap = new Caption()[language];
    public string this[string language]
    {
        get
        {
            var which = AR;
            switch(language)
            {
                case nameof(EN):
                    which = EN;
                    break;
                case nameof(Tu):
                    which = Tu;
                    break;
            }
            return which;
        }
    }
}

Note that it's possible to achieve the syntax you're after where you can implicitly convert a Caption to a string. The caveat is that you'd need to know the language at some point before you do the conversion.

You can do so by providing a constructor that handles it on creation and/or with a method (chain-able if you wish) like I do below with For:

class Caption
{
    public string AR { get; set; }
    public string EN { get; set; }
    public string Tu { get; set; }

    // Gets the caption of the current language
    public string Current { get; private set; }

    // If the language is known in advance, you can return
    // the desired language directly.
    public static implicit operator string(Caption caption) 
        => caption.Current;

    public Caption For(string language)
    {
        var which = AR;
        switch (language)
        {
            case nameof(EN):
                which = EN;
                break;
            case nameof(Tu):
                which = Tu;
                break;
        }
        Current = which;
        return this;
    }
}

// Usage
var cap = new Caption
{ 
    AR = "Test-ar",
    EN = "Test-en",
    Tu = "Test-tu"        
}.For("EN");

string s = cap;
Console.WriteLine(s); // Test-en
Sign up to request clarification or add additional context in comments.

5 Comments

Just so that I understand - what is the benefit of the indexer over say a method in the class GetLanguageString(string strLanguage) that pretty has the same method body as the indexer.
@Rakesh, convenience is the primary benefit but of course a method would work fine if that's preferred. Based on their wish to get the value as directly as possible, I'd rather do caption[lang] rather than caption.GetLanguageString(lang).
Got it, never used an indexer myself, so was wondering if there was more under the hood.
Thats what i need excactly. Chief ! Thats make a class used as string ! that what i need, Thanks.
@devmuhammadkamal, glad it helped you. I updated the answer to add a bit on the implicit conversion; it may work for your case and be exactly what you're looking for :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.