如何进入私有静态元组,并使用numericUpDown更改Math.Round [英] How to get inside private static Tuple, and use numericUpDown to change Math.Round

查看:137
本文介绍了如何进入私有静态元组,并使用numericUpDown更改Math.Round的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我解析NMEA数据的代码的一部分(此处的源

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).

这是我的问题.我想使用"numericUpDown"更改Math.Round(在我下面的值为-11).我无法进入Tuple,因为我遇到了错误(我尝试做一些事情,但是没有用).谁知道,我该怎么办?

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));
        });
        ;
    }

我想要这样的东西:

   return new Tuple<double>(Math.Round(szerokosc_dziesietna, round_value));

其中(例如):

int round_value = (int)numericUpDown1.Value; 

我声明:

numericUpDown1.Minimum = 1;
numericUpDown1.Maximum = 11;

请帮助:).

推荐答案

正如您所想,并且我怀疑但由于我没有所有代码而无法确定,问题是由您的方法中的关键字.删除它可以解决问题.

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.

static修饰符用于指示方法/属性/字段/等.属于该类,而不是该类的实例.在static方法或属性内,您不能使用this来引用该类的当前"实例,因为static方法没有当前实例.

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.

在Windows窗体中,为窗体中的各种控件(例如您的numericUpDown2)创建的字段始终是实例"字段,即不是static,因此它们属于窗体的每个单独实例,而而不是Form1类.这样,如果您同时打开两个不同的Form1,则两种形式的相应控件可以具有不同的值. (您可能只需要在应用程序中使用一个Form1,但是在其他应用程序中的其他形式可能需要使用一次以上.)Form1类中的static方法无法看到这些控件中的任何一个,因为它属于Form1类,其中实例(即非静态)方法属于每种单独的形式.因此,static方法无法看到控件,因为它不知道许多控件所处的形式.

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 Form1s 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.

由于您是初学者,如果不确定是否需要创建方法static,我将向您提供的建议是不创建方法static.如果某个方法不使用任何特定于实例的字段或方法,则可以根据需要尝试将其设置为static,并查看这样做是否会引入任何编译器警告.

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.

这篇关于如何进入私有静态元组,并使用numericUpDown更改Math.Round的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆