Reputation: 61
I've largely self taught myself C# code, I took a class in college that didnt help much...sure learned how to follow a book. So i've created tools here and there learning by example online. Stackoverflow is my favorite site for that, usually the community is helpful...
Anyway my question is this, im creating a WPF program that does multiple calculations on a number of decimals, the I've been getting by reusing the same code calculation over an over again, it works fine but I know there is a simpler way of doing it with much, much fewer lines. I seem to be missing that knowledge.
here is an example of how i would do things.
int int1 = 4;
int int2 = 2;
int int3 = 6;
int int4 = 8;
int calc1 = 0;
int calc2 = 0;
int calc3 = 0;
calc = int1 * int4
calc2 = int1 * int2
calc3 = int3 * int3
if (calc >= calc3)
{
do something;
}
else
{
calc = 33
}
if (calc2 >= calc3)
{
do something;
}
else
{
calc2 = 33
}
if (calc3 >= calc2)
{
do something;
}
else
{
calc3 = 33
}
if (calc3 >= calc)
{
do something;
}
else
{
calc2 = 33
}
I hope that is clear enough.. I can repeat the code but I am just unsure how to use C# better, I know it has built in ways of reducing the repeating code, just dont know how to find them.
Any help or examples are appreciated.
Upvotes: 1
Views: 2119
Reputation: 726479
The simplest way to reuse code among methods of the same class like that is to define a private method for that calculation. This way you'd be able to reference that code by calling a method, rather than by copy-pasting some code. In fact, every time you copy-paste, you know you're missing a method.
If you need to share code among related classes, you can make a protected method in the base class.
Finally, for project-wide "horizontal" reuse you can define a static helper class, and your methods to it as public static. This way every class in your project would be able to reuse your calculation.
Upvotes: 2
Reputation:
How about creating a private method within your class, then call the method when you need a calculation done. This eliminates rewriting code over and over again.
Example:
int int1 = 4;
int calc1 = 0;
Calculation(int1, calc1);
int int2 = 2;
int calc2 = 0;
Calculation(int2, calc2);
//private method
private Calculation(int integer, int calculation)
{
//calculate
}
side note: I prefer to arrange all variables first, then act (function calls, etc.) on it (based on Arrange-Act-Assert associated with unit testing). However, I did it this way to emphasize my point.
Upvotes: 1
Reputation: 67065
The simplest solution that pops out to me is to turn it into a method. (I will leave the access modifier for the function up to you...it depends on where you will be reusing this code)
int CustomCompare(int leftHandSide, int rightHandSide)
{
int calc;
if (leftHandSide >= rightHandside)
{
do something;
}
else
{
leftHandSide= 33
}
return leftHandSide
}
You would just pass in your variables:
calc = CustomCompare(calc, calc3)
You could even change the do something portion to be a custom action that you pass in if you want. Take a look at Action in MSDN
int CustomCompare(int leftHandSide, int rightHandSide, Action doSomething)
{
int calc;
if (leftHandSide >= rightHandside)
{
doSomething();
}
else
{
leftHandSide= 33
}
return leftHandSide
}
...
calc = CustomCompare(calc, calc3,
()=>{do some stuff that will be executed inside the method});
And Func can allow you to return a value from that doSomething action
Upvotes: 7
Reputation: 78914
Err.... call a function?
doCalc(4, 2, 6, 8)
static public void doCalc(int int1, int int2, int int3, int int4)
{
int calc1 = int1 * int4
int calc2 = int1 * int2
int calc3 = int3 * int3
if (calc >= calc3)
{
do something;
}
else
{
calc = 33
}
if (calc2 >= calc3)
{
do something;
}
else
{
calc2 = 33
}
if (calc3 >= calc2)
{
do something;
}
else
{
calc3 = 33
}
if (calc3 >= calc)
{
do something;
}
else
{
calc2 = 33
}
}
also, mind the indentations. when you start a new scope, add a few spaces to what's inside it.
Upvotes: 0