使用静态变量从不同的方法调用

时间:2017-03-18 10:30:46

标签: c# static

我试图在C#中的方法之间“共享”变量。我是C#的新手,我知道没有“全局变量”这样的东西,我不太确定如何正确使用静态变量。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectOne
{
    static bool tooHigh;
    static internal waistMeasurment;
    class Main
    {
        static void Main(string[] args)
        {
            GetVariables();
        }

        public static void GetVariables() //This method gets the users height & waist measurments 
                                           //and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
        {
            Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
            waistMeasurment = Console.ReadLine(); //This writes the users input under the string 'waistMeasurment'
            ValidateWaist();
                if tooHigh ==true
                {
                waistMeasurment = Console.ReadLine();
                ValidateWaist();
                }
        }
        public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
        {
            if (waistMeasurment < 60) //Checks the lower bound of the waist limit
            {
                Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
                tooHigh = true;
            }
            else
            {
                tooHigh = false;
            }
        }
    }
}

我有太高和腰部的问题

3 个答案:

答案 0 :(得分:2)

C#是一种面向对象的编程语言,这意味着它由namespaces组成,其中包含classesstructs,其中包含fieldsproperties (所以,变量)和methods

你是绝对正确的,C#中没有全局变量。虽然,你可以将一个带有静态变量的类破解并将其用作全局变量,但最好将所有变量保持在本地并受到控制。

您希望实现的目标是将两个变量(tooHighwaistMeasurment)作为静态变量置于静态Main类中。

你也不会以Python的风格使用if语句,internal是为方法而不是变量而发明的。根据您的代码,您正在寻找integer的类型,因为稍后,您正在检查变量waistMeasurment是否小于60.为了做到这一点,您首先必须投射变量为integer。正确的方法是使用int.TryParse方法。

namespace ProjectOne
{
    class Main
    {
        static bool tooHigh;
        static int waistMeasurment;

        static void Main(string[] args)
        {
            GetVariables();
        }

        public static void GetVariables() //This method gets the users height & waist measurments 
                                           //and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
        {
            Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
            int.TryParse(Console.ReadLine(), out waistMeasurment); //This writes the users input under the string 'waistMeasurment'
            ValidateWaist();
                if (tooHigh)
                {
                int.TryParse(Console.ReadLine(), out waistMeasurment); 
                ValidateWaist();
                }
        }
        public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
        {
            if ((int)waistMeasurment < 60) //Checks the lower bound of the waist limit
            {
                Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
                tooHigh = true;
            }
            else
            {
                tooHigh = false;
            }
        }
    }
}

答案 1 :(得分:0)

如评论中所述,您必须更改代码才能将变量置于Type内:

你得到了什么:

namespace ProjectOne
{
    static bool tooHigh;
    static internal waistMeasurment;

    class Main
    {
        //your code
    }
}

你要写的是什么:

namespace ProjectOne
{
    class Main
    {
        static bool tooHigh;
        static internal waistMeasurment;

        //your code again
    }
}

答案 2 :(得分:0)

静态可用于共享变量,但需要在Class中声明,程序中有更多错误,我修改了程序,你可以使用它并调试它用于学习

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
class Program
{
    static bool tooHigh;
    static int waistMeasurment;    
    static void Main(string[] args)
    {
        GetVariables();
    }

    public static void GetVariables() //This method gets the users height & waist measurments 
                                      //and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
    {
        Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
        int.TryParse(Console.ReadLine(), out waistMeasurment); //This writes the users input under the string 'waistMeasurment'
        ValidateWaist();

        if (tooHigh == true)
            {
            int.TryParse(Console.ReadLine(), out waistMeasurment); ;
            ValidateWaist();
        }
    }
    public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
    {
        if (waistMeasurment < 60) //Checks the lower bound of the waist limit
        {
            Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
            tooHigh = true;
        }
        else
        {
            tooHigh = false;
        }
    }
}

}