-5

I would like to us the value of a String i have created and write out the value in my code. In this example i don't know the value of the String "attribut":

public void EditUser(Int32 user_no, String attribut, String change)
{
    tmpUser = GetUser(user_no);

    if (attribut.Equals("username"))
    {
        tmpUser.username = change;
    }
    else if (attribut.Equals("mail"))
    {
        tmpUser.mail = change;
    }
    else
    {
        tmpUser.password = change;
    }
}

I know I can't do it like this:

tmpUser.attribut = change;

Is there a way do this and avoid using the if else statements.

Sean
  • 1,534
  • 12
  • 20
  • Possible using reflection, but not very practical. I think that you should ask about the problem that you are trying to solve, instead of asking about the way that you think that it could be solved. – Guffa Jan 30 '15 at 22:57
  • Huh, You completely changed your question......Are you kidding? – EZI Jan 30 '15 at 23:07
  • Sorry for the confusion – Axel Reinholdz Jan 30 '15 at 23:09
  • I'm totally voting to close this `mess` don't change the rules in the bottom of the `9th inning` as we say in the coding world.. you need to read some basic C# tutorials on how to assign variables wow http://www.tutorialspoint.com/csharp/csharp_variables.htm do you know how to use the debugger? also what is the value of `change when you enter the method .. you have some serious `Logic flaws` or perhaps you're looking for an `out Parameter` – MethodMan Jan 30 '15 at 23:09
  • I know how to assign variables, I'm using a MVC, this being form my model, and I can't know what the user is going to write in view. – Axel Reinholdz Jan 30 '15 at 23:12
  • I am leaving this one alone since you altered your code and took out the necessary / main part dealing with `MVC` good luck I am out of here – MethodMan Jan 30 '15 at 23:13

1 Answers1

-1

No, not like that. The closest thing you can do is to use Reflection to get the correct overload of WriteLine method from Console class. And Invoke it.

For example:

var method= typeof(Console)
                 .GetMethod("WriteLine",
                            BindingFlags.Public | BindingFlags.Static,
                            null,
                            new [] { typeof(string) },
                            null);

method.Invoke(null, new object[] { "Hello" });
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Reflection is not specific to Console class. – Selman Genç Jan 30 '15 at 22:53
  • `Not sure who downvoted this` however this is an awesome / working solution.. it's quite obvious without even having to run the code that the `Console would write out the word Hello` great sample – MethodMan Jan 30 '15 at 23:01