Search This Blog

Friday, March 26, 2010

Reducing Duplicated Code

Programmer Question


<p>I have some code that works on the color structure like this</p>

<pre><code>public void ChangeColor()
{
thisColor.R = thisColor.R + 5;
}
</code></pre>

<p>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.</p>

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

<p>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.</p>

<p>I want it to look like this:</p>

<pre><code>public void ChangeColor(int RGBValue)
{
thiscolor.RGBValue = thiscolor.RGBValue;
}
</code></pre>

<p>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?</p>



Find the answer here

No comments:

Post a Comment

Related Posts with Thumbnails