问题
this is my part of code to parse a NMEA data (source here Plot chart using values from richTextBox C#) . This function get back latitude from richTextBox, and convert it to decimal notation. I need it, to plot charateristic from 4 another GPS module (to compare accuracy all four GPS).
This is my problem. I want to use a "numericUpDown" to change Math.Round (below I have value - 11). I cant get inside Tuple because I have error (I tried to do something, but it didnt work). Can anybody know, what should I do?
private static Tuple<double>[] szerokosc(string[] lines)
{
return Array.ConvertAll(lines, line =>
{
string[] elems = line.Split(',');
double we = 0.01 * double.Parse(elems[3], EnglishCulture);
int stopnie = (int)we;
double minuty = ((we - stopnie) * 100) / 60;
double szerokosc_dziesietna = stopnie + minuty;
return new Tuple<double>(Math.Round(szerokosc_dziesietna, 11));
});
;
}
I want something like this:
return new Tuple<double>(Math.Round(szerokosc_dziesietna, round_value));
where (for example):
int round_value = (int)numericUpDown1.Value;
and I declare:
numericUpDown1.Minimum = 1;
numericUpDown1.Maximum = 11;
Please help :).
回答1:
As you figured out, and as I suspected but couldn't be sure as I didn't have all the code, the problem was caused by the static
keyword in your method. Removing it fixed the problem.
The static
modifier is used to indicate that the method/property/field/etc. belongs to the class, rather than to instances of the class. Inside a static
method or property, you cannot use this
to refer to the 'current' instance of the class, as static
methods don't have a current instance.
In Windows Forms, the fields created for the various controls in the forms, such as your numericUpDown2
, are always 'instance' fields, i.e. not static
, so they belong to each individual instance of the form, rather than to the Form1
class. That way, if you have two different Form1
s open at the same time, the corresponding controls in the two forms can have different values. (You might only need to use one Form1
in your application, but other forms in other applications may need to be used more than once.) A static
method in the Form1
class cannot see any of these controls because it belongs to the Form1
class, where an instance (i.e. non-static) method belongs to each individual form. So a static
method cannot see the controls because it can't know which form of possibly many the controls are in.
As you are a beginner, the advice I would give to you if you are unsure about whether you need to make a method static
would be not to make the method static
. If a method makes no use of any instance-specific fields or methods, you can try making it static
if you wish, and see if doing so introduces any compiler warnings.
来源:https://stackoverflow.com/questions/22879014/how-to-get-inside-private-static-tuple-and-use-numericupdown-to-change-math-rou