Reducing Duplicated Code

Posted by cam on Stack Overflow See other posts from Stack Overflow or by cam
Published on 2010-03-27T00:45:57Z Indexed on 2010/03/27 0:53 UTC
Read the original article Hit count: 197

Filed under:
|

I have some code that works on the color structure like this

public void ChangeColor()
{
thisColor.R = thisColor.R + 5;
}

Now I need to make a method that changes a different variable depending on what it is passed. Here is what the code looks like now.

 public void ChangeColor(int RGBValue)
    {
    switch(RGBValue)
{
case 1:
    thisColor.R = thisColor.R + 5;
break;
case 2:
    thiscolor.B = thisColor.B + 5;
break;
}
    }

Now, this is something I would normally never question, I'd just throw a #region statement around it and call it a day, but this is just an example of what I have, the actual function is quite long.

I want it to look like this:

public void ChangeColor(int RGBValue)
    {
    thiscolor.RGBValue = thiscolor.RGBValue;
    }

So essentially the value would refer to the variable being used. Is there a name for this? Is this what Reflection is for? Or something like that... Is there a way to do this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about readability