静态字符串中的访问复选框

时间:2011-02-15 01:09:06

标签: c# .net forms

您好 我有一个问题,我无法弄清楚我知道问题,但不知道如何解决它

private static string Characters()
    {
        string str = null;

        const string lower = "abcdefghijklmnopqrstuvwxyzæøå";
        const string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ";
        const string numeric = "0123456789";

        // connot acces non-static field 'checkLower' in static context
        if (checkLower.Checked)
        {
            str += lower;
        }
        if (checkUpper.Checked)
        {
            str += upper;
        }
        if (checkDigits.Checked)
        {
            str += numeric;
        }

        return str;
    }

    // if i change Characters to non-static this will say cannot access non-staic method 'Characters' in static context
    private static char[] fCharList = Characters().ToCharArray();

2 个答案:

答案 0 :(得分:0)

试试这个

 private static string Characters(bool checkLowerChecked, bool checkUpperChecked, bool checkDigitsChecked )
        {
            string str = null;

            const string lower = "abcdefghijklmnopqrstuvwxyzæøå";
            const string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ";
            const string numeric = "0123456789";

            // connot acces non-static field 'checkLower' in static context
            if (checkLowerChecked)
            {
                str += lower;
            }
            if (checkUpperChecked)
            {
                str += upper;
            }
            if (checkDigitsChecked)
            {
                str += numeric;
            }

            return str;
        }

并致电

Characters(checkLower.Checked, checkUpper.Checked, checkDigits.Checked).ToCharArray();

答案 1 :(得分:0)

静态函数/字段无法访问成员字段,因为它们是静态的。如果需要引用checkUpper / Lower等,请从fCharList和Characters中删除'static'关键字。