0

Actually I can not even say exactly how that thing is called correctly but I need something that can assign variables / properties / fields in one method. I will try to explain... I have the following code:

txtBox.Text = SectionKeyName1; //this is string property of control or of any other type
if (!string.IsNullOrEmpty(SectionKeyName1))
{
    string translation = Translator.Translate(SectionKeyName1);
    if (!string.IsNullOrEmpty(translation))
    {
        txtBox.Text = translation;
    }
}

stringField = SectionKeyName2; //this is class scope string field
if (!string.IsNullOrEmpty(SectionKeyName2))
{
    string translation = Translator.Translate(SectionKeyName2);
    if (!string.IsNullOrEmpty(translation))
    {
        stringField = translation;
    }
}

stringVariable = SectionKeyName3; //this is method scope string variable
if (!string.IsNullOrEmpty(SectionKeyName3))
{
    string translation = Translator.Translate(SectionKeyName3);
    if (!string.IsNullOrEmpty(translation))
    {
        stringVariable = translation;
    }
}

As I see, this code can be refactored to one method, that receives the "object" to set and SectionKeyName. So it can be something like:

public void Translate(ref target, string SectionKeyName)
{
    target = SectionKeyName;
    if (!string.IsNullOrEmpty(SectionKeyName))
    {
        string translation = Translator.Translate(SectionKeyName);
        if (!string.IsNullOrEmpty(translation))
        {
            target = translation;
        }
    }
}

BUT: I will can not use that method in case when I want to assign texBox.Text, since properties can not be passed byref.... I found topic on SO where is solution for properties, but it solves the properties and I stuck with fields / variables....

Please help me to find the way to write a single method that will handle all of my cases...

//This method will work to by varFieldProp = Translate(SectionKeyName, SectionKeyName), but would like to see how to do it with Lambda Expressions.
public string Translate(string SectionKeyName, string DefaultValue)
{
    string res = DefaultValue; //this is string property of control or of any other type
    if (!string.IsNullOrEmpty(SectionKeyName))
    {
        string translation = Translator.Translate(SectionKeyName);
        if (!string.IsNullOrEmpty(translation))
        {
            res = translation;
        }
    }

    return res;
}

Thank you !!!

Community
  • 1
  • 1
Alex Dn
  • 5,465
  • 7
  • 41
  • 79
  • 1
    "but would like to see how to do it with Lambda Expressions" - that will not be an improvement. Just use that last function variant. It's simple and to the point. – H H Jul 05 '12 at 12:30

1 Answers1

3

I think you want something like this:

public void Translate(Action<string> assignToTarget, string SectionKeyName)
        {
            assignToTarget(SectionKeyName);
            if (!string.IsNullOrEmpty(SectionKeyName))
            {
                string translation = Translator.Translate(SectionKeyName);
                if (!string.IsNullOrEmpty(translation))
                {
                    assignToTarget(translation);
                }
            }
        }

but it would be better if you simply remove the lambda and let the function returns the translated string to use when needed. Just for completeness, to call this function you can use:

Translate(k=>textBox1.Text=k,sectionKeyName);
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • I think I will use the simple function that returns the string, but for my education, can you show me how I would call the function with Action? Thanks ! – Alex Dn Jul 05 '12 at 13:00