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 !!!

1
  • 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. Commented Jul 5, 2012 at 12:30

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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<string>? Thanks !

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.