私はボタンでこれを持っている:C#このコードを最適化できますか?
クラス「MyMath」はprivate void Button_Click(object sender, RoutedEventArgs e)
{
string s = "x12y04";
//make new instance for MyMath class
MyMath dRet01 = new MyMath();
//use the doubleArrayXY (in MyMath class) to get doubble array back
double[] retD = dRet01.doubleArrayXY(s);
//use the calcResultFromDoubleArray (in MyMath class) to get result
MyMath dRet02 = new MyMath();
double result = dRet02.calcResultFromDoubleArray(retD[0], retD[1]);
//DEBUG!
/*
string TEST1 = Convert.ToString(returnedDouble[0]);
MessageBox.Show(TEST1);
string TEST2 = Convert.ToString(returnedDouble[1]);
MessageBox.Show(TEST2);
string TEST3 = Convert.ToString(result);
MessageBox.Show(TEST3);
*/
}
:
public double[] doubleArrayXY(string inputValue)
{
//in case there are upper case letters, set all to lower
string inpLow = inputValue.ToLower();
//split the string on the Y (so this tech should also work for x89232y329)
//so this will create res[0] that is x89232 and an res[1] that is 329
string[] res = inpLow.Split(new string[] { "y" }, StringSplitOptions.None);
//in the first string that looks like x89232, remove the x
string resx = res[0].Replace("x", null);
//now get the x value to a double
double x = double.Parse(resx);
//now get the y valye to a double
double y = double.Parse(res[1]);
//return in a double array the x and then the y (x=double[0] and y=double[1])
return new double[] {x,y};
}
public double calcResultFromDoubleArray(double one, double two)
{
return (one * two);
}
今、私は「calcResultFromDoubleArray」であるクラスの一部は、これを一種の役に立たない知っています私はそれが後でいくつかの余分なものを行うようにしたい。
私はこの新しいdRet10を作成するメインコードの中で最も不思議なものがあり、後でdRet02を作成します。
double result = dRet01.calcResultFromDoubleArray(retD[0], retD[1]);
だから、その場合には、私はMyMathの新しいインスタンスを作成する必要はありませんが、これは動作しません:私が最初に考えていた
私はこのような何かを行うことができます。
私はクラスのために新しいインスタンスを呼び出す必要があります(これは私のように)、これをより洗練された方法で行うことはできますか? 私はまだC#の新機能ですから、うまく機能させるだけでなく、すてきでエレガントな方法でプログラミングする方法を学ぼうとしています。
:誰かがこの-1をわずか数分で投票する理由を理解していません... – Dante1986
メソッドオーバーロードを見ていますか? – MethodMan
私はC#を勉強していて、私が今作ったこのコードがそれを作る正しい方法であったかどうか、ちょうどそれがうまく働いているのかどうか疑問に思っていました。 (なぜ私も人々がstackoverflowでそれを投票したい理由はない...) – Dante1986