当从另一个类中调用静态整数时,它将丢失其值并默认为零?

问题描述 投票:0回答:2

我正在尝试从一个类调用另一个类的静态整数。该整数被成功调用,但是该值为零,而不是原始类中的值。

到目前为止,我已经尝试了很多事情,但是现在陷入了困境。我假设这个问题是菜鸟的错误。任何帮助将不胜感激。

在Class1 ...

public static int countSAO;
...
countSAO = SAO_Num.count;

此后,countSAO的值为9017。在其他类中...

button.Text = Class1.countSAO.toString();

按钮文本为0,而不是预期的9017。

更大的代码块。

public static List<String> SAO_Num = new List<String>();
...
 while ((line = fileSAO.ReadLine()) != "#End")
                {                        
                    string[] items = line.Split('\t'); //Store strings into aray.  Items seperated by tabs.
                    //Add to our lists, items[0] and items[1] are to be skipped per the file.
                    SAO_BayerLetter.Add(items[2]);
                    SAO_Constellation.Add(items[3]);
                    SAO_Num.Add(items[4]);
                    SAO_CoordRA.Add(items[5]);
                    SAO_CoordDec.Add(items[6]);
                    SAO_Magnitude.Add(items[7]);
                    SAO_SpectralType.Add(items[8]);
                    SAO_Distance.Add(items[9]);
                    SAO_ProperName.Add(items[10]);
                    SAO_AutoCalFlag.Add(items[11]);

                }
...
textBox1.Text = SAO_Num.Count.ToString();// this is 9017
...
public static int MyCountSAOValue()
    {

        return SAO_Num.Count;
    }

全部来自Class1。

这是我在Class2中调用MyCountSAOValue方法。

button.Text = UserControlRotator.MyCountSAOValue().ToString();//This sets the button text to 0.
c# visual-studio static integer
2个回答
0
投票

静态成员(变量,属性,方法)可以直接访问。如果您尝试访问countSAO而不进行更改,它将返回0。

使用前,您需要更新该值。您可以使用类构造函数,也可以具有将更新它的静态属性。

protected void Page_Load(object sender, EventArgs e)
{
    int beforeInstantiation = Class1.countSAO; // output: 0

    Class1 myclass = new Class1();

    int afterInstantiation = Class1.countSAO; // output: 9017

    Class1.updatedCount(); // update the Static variable before calling

    int updatedSao = Class1.countSAO; // output 500

    int saoProperty = Class1.countSAOProperty; // output 9017
}

class Class1
{
    public static int countSAO; // this would not 

    public static int countSAOProperty
    {
        get
        {
            return SAO_Num.Count;
        }
    }

    // this will be called only when the class is instantiated using New Class1();
    public static List<int> SAO_Num
    {
        get
        {
            List<int> intlist = new List<int>();

            for (int i = 0; i < 9017; i++)
            {
                intlist.Add(i);
            }

            return intlist;
        }
    }
    // this will only run if class is Instantiate using New Class();
    public Class1()
    {
        countSAO = SAO_Num.Count;
    }

    public static void updatedCount()
    {
        countSAO = 500;
    }
}

0
投票

不确定SAO_Num.count的值是什么,但是我做了以下操作,它工作正常:

更新代码:

class Program
{
    static void Main(string[] args)
    {
        // method 1
        // simulate reading lines from file
        List<String> SAO_Num = new List<String>() {
            "test1",
            "test2",
            "test3",
            "test4",
            "test5",
            "test6",
            "test7",
            "test8",
            "test9",
            "test10"
        };

        Class1.countSAO = SAO_Num.Count;
        Console.WriteLine("1) My Button Text: " + Class1.countSAO.ToString());

        // method 2
        Console.WriteLine("2) My Button Text: " + Class1.MyCountSAOValue().ToString());
    }
}

public static class Class1
{
    public static int countSAO;

    // simulate reading lines from file
    public static List<String> SAO_Num = new List<String>() {
        "test1",
        "test2",
        "test3",
        "test4",
        "test5",
        "test6",
        "test7",
        "test8",
        "test9",
        "test10"
    };


    public static int MyCountSAOValue()
    {
        countSAO = SAO_Num.Count;

        return countSAO;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.