简体   繁体   English

如何在静态方法C#中访问内部私有变量

[英]How To Access Internal Private Variables Inside Static Methods C#

I'm coding in MVVM and have a Calculation class. 我正在MVVM中进行编码,并且具有Calculation类。 Inside this Calc Class I have different static Memberfunctions which each calculate a portion of my calculations. 在此Calc类内部,我有不同的静态Memberfunction,每个函数都会计算一部分计算量。

Eg: Inside Calc Class: 例如:内部Calc类:

one of the Memberfunctions: 成员功能之一:

public static double CalcTimeJerkAcc(double limitedAcceleration, double maxJerk)
{
    return (limitedAcceleration / maxJerk);
}

another Memberfunction in which I use timeJerkAcc: 我使用timeJerkAcc的另一个Memberfunction:

public static double CalcCalculation(double timeJerkAcc, ...) { ... }

now I want to implement these Calculations into a Model: 现在我想将这些计算实现为模型:

public double PositioningTime
    {
        get
        {
             return CalcClassObject.CalcCalculation(CalcClassObject.timeJerkAcc, ...); 
        }
    }

How do I access private Variables such as "timeJerkAcc" from the Calc Class, despite them being static? 我如何从Calc类访问私有变量(例如“ timeJerkAcc”),尽管它们是静态的? My approach with CalcClassObject.timeJerkAcc in the transfer parameters in CalcClassObject.CalcCalculation(...) is clearly wrong. 我有办法CalcClassObject.timeJerkAcc在传输参数CalcClassObject.CalcCalculation(...)显然是错误的。 I'm stuck. 我被卡住了。 Can someone clear this problem up for me? 有人可以帮我解决这个问题吗? I'm fairly new to C# and hope this is not a really dumb question... 我对C#相当陌生,希望这不是一个愚蠢的问题...

It's hard to describe my problem well, but I hope someone will understand what I'm trying to do. 很难很好地描述我的问题,但是我希望有人能理解我正在尝试做的事情。

I'm not sure if I understood right, but you want to create a model that calls static methods and variables. 我不确定我是否理解正确,但是您想创建一个调用静态方法和变量的模型。

So I created a small snippet that will hopefully help you: 因此,我创建了一个小片段,希望对您有所帮助:

This is the main Class: 这是主要的类:

class Solution
{
    static void Main(string[] args)
    {
        MyModel model = new MyModel();

        Console.WriteLine(model.PositioningTime);
        Console.ReadLine();
    }
}

And the Calculation class: 和Calculation类:

public class Calculation
{
    public static double timeJerkAcc = 12;
    public static double maxJerk = 30;
    public static double CalcTimeJerkAcc(double limitedAcceleration, double maxJerk)
    {
        return limitedAcceleration / maxJerk;
    }

    public static double CalcCalculation(double timeJerkAcc, double temp)
    {
        return timeJerkAcc / temp;
    }
}

And finally, the Model class: 最后是Model类:

public class MyModel
{
    public double PositioningTime
    {
        get
        {
            return Calculation.CalcCalculation(Calculation.timeJerkAcc,Calculation.maxJerk);
        }
    }
}

To call static variables or methods that are outside of the current class, you have to set them as public . 要调用当前类之外的静态变量或方法,必须将它们设置为public

Hope this helped you, if it didn't, please try to explain your problem with more details, 希望这对您有所帮助,如果没有帮助,请尝试用更多详细信息解释您的问题,

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM