简体   繁体   English

C# 如何更改静态类的值?

[英]C# How can I change the value from static class?

So this is the Program cs.所以这是程序cs。 I want to change the value of int in my StaticClass.cs ** I want to delcare a printing sentence " How long do you want me to generate " then change the value of N in StaticClass.我想在我的 StaticClass.cs 中更改 int 的值 ** 我想打印一个打印句“你希望我生成多长时间”然后更改 StaticClass 中 N 的值。

class Program
{
    

    static void Main(string[] args)
    {
        Constructor ct = new Constructor(StaticClass.n);
        //ct.xmethod(StaticClass.n);
        Console.ReadKey();
    }
}

Here my constructor class.这是我的构造函数类。 Method of Pascal Triangle帕斯卡三角法

class Constructor
{
    
    int[][] triangle = new int[StaticClass.n][];

    public Constructor(int n)
    {
        xmethod(n);
    }

    public void xmethod(int n)
    {
        for (int row = 0; row < triangle.Length; row++)
        {

            //Each row is a subarray of length 1 greater than row number
            triangle[row] = new int[row + 1];
            //set the values in a row

            for (int k = 0; k < triangle[row].Length; k++)
            {

                // if not first or last element in the row


                if (k > 0 & k < triangle[row].Length - 1)
                    //set it to sum of the element above it
                    //and the one above and to the left
                    triangle[row][k] = triangle[row - 1][k - 1] + triangle[row - 1][k];

                //otherwise this is an end point, set it to 1
                else


                    triangle[row][k] = 1;

                //display value
                Console.Write(triangle[row][k] + "\t");
            }
            // Finished processing row-send cursor to next line
            Console.WriteLine();
            
        }
    }
}

Here my StaticClass for the long of Pascal Triangle这里我的静态类很长帕斯卡三角形

class StaticClass
{
    public const int n = 15;

    // I want to change the value of N using the Console Writeline. 

    

}

n needs to be updatable then instead of const use static ; n需要是可更新的,然后使用static代替const but Console.WriteLine() is not something you would use to change the value.但是Console.WriteLine()不是您用来更改值的东西。 You'll need to be more clear about what you want to do.你需要更清楚你想要做什么。 Do you want someone to input the value of n - with a Console.ReadLine?您是否希望有人使用 Console.ReadLine 输入n的值?

So ... from what I gather -- I think you want to change your Main to write your question: " How long do you want me to generate " then read the line for user input.所以......从我收集的信息来看——我认为你想改变你的Main来写你的问题:“你希望我生成多长时间”然后阅读用户输入的行。 Then print your triangle.然后打印你的三角形。 Something like :就像是 :

    static void Main(string[] args)
    {
        Console.WriteLine("How long do you want me to generate? ");
        int inNum = int.Parse(Console.ReadLine());
        Constructor ct = new Constructor(inNum);
        Console.ReadKey();
    }

Then from what I see ... your Constructor needs to change:然后从我所看到的......你的Constructor需要改变:

    public Constructor(int n)
    {
        triangle = new int[n][];
        xmethod(n);
    }

Make your public const field a public static property instead:将您的公共 const 字段改为公共静态属性:

public static int N { get; set; } = 15;

Change it like改成这样

StaticClass.N = 16;

You said "change it with WriteLine" - WriteLine outputs things, it doesn't change thing.你说“用 WriteLine 改变它” - WriteLine 输出东西,它不会改变东西。 Perhaps you meant ReadLine:也许你的意思是 ReadLine:

Console.WriteLine("How many levels? ");
string input = Console.ReadLine();
StaticClass.N = int.Parse(input);

Do please find a better name for StaticClass;请为 StaticClass 找一个更好的名字; classes should be named for what they represent in a more real world sense (like.. Configuration?) not what they are in a C# sense (yes, it's a static class, but it doesn't mean we should call it that)类应该根据它们在更真实世界的意义上代表的东西(比如..配置?)而不是它们在 C# 意义上的名称(是的,它是一个静态类,但这并不意味着我们应该这样称呼它)

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

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