C# 使用类中的变量在其他地方使用

C# 使用类中的变量在其他地方使用,c#,.net,C#,.net,我对使用C语言中的类是相当陌生的,所以这似乎是一个相当简单的问题。我的程序中出现一个错误,告诉我Math.Round旁边的区域在当前上下文中不存在,那么如何使Windows窗体代码中的类中的区域变量可访问 类别: class Area { private static double area(double radius) { return (Math.PI * radius * radius); } private static double a

我对使用C语言中的类是相当陌生的,所以这似乎是一个相当简单的问题。我的程序中出现一个错误,告诉我Math.Round旁边的区域在当前上下文中不存在,那么如何使Windows窗体代码中的类中的区域变量可访问

类别:

class Area
{
    private static double area(double radius)
    {
        return (Math.PI * radius * radius);
    }

    private static double area(int width, int length)
    {
        return (width * length);
    }

    private static double area(double radius, double height)
    {
        return (Math.PI * radius * radius * height);
    }
}
表格编号:

private void GetCircleAreaButton_Click(object sender, EventArgs e)
    {
        double radius;
        radius = Convert.ToInt32(radiusTextBox.Text);
        circleAreaResultLabel.Text = Math.Round(area(radius), 2).ToString();
    }

    private void GetRectangleAreaButton_Click(object sender, EventArgs e)
    {
        int length, width;
        length = Convert.ToInt32(widthTextBox.Text);
        width = Convert.ToInt32(lengthTextBox.Text);
        rectangleAreaResultLabel.Text = Math.Round(area(length, width), 2).ToString();
    }

    private void GetCylinderAreaButton_Click(object sender, EventArgs e)
    {
        double radius, height;
        radius = Convert.ToDouble(radiusTextBox.Text);
        height = Convert.ToDouble(heightTextBox.Text);
        cylinderAreaResultLabel.Text = Math.Round(area(radius, height), 2).ToString();
    }

您需要创建类的实例。在您的情况下:

Area area = new Area();
然后你可以打电话给say area.area 2.2等

或者,使您的类成为静态的,这也需要使您的方法成为静态的,因为静态类只能有静态方法

public static class Area
{
    private static double area(double radius)
    {
        return (Math.PI * radius * radius);
    }

    private static double area(int width, int length)
    {
        return (width * length);
    }

    private static double area(double radius, double height)
    {
        return (Math.PI * radius * radius * height);
    }
}
在您的代码区域中是方法,而不是变量。要调用静态方法,应将类名放在其前面,如Area.arearadius、height。或者将using static区域添加到文件中的using指令

你还必须公开所有方法,而不是私有方法

最后,从大写字母开始创建方法名称是一个很好的实践,如下所示

班级面积 { 公共静态双区域双半径 { 返回Math.PI*半径*半径; } 公共静态双GetAreaint宽度,int长度 { 返回宽度*长度; } 公共静态双通道双半径双高度 { 返回Math.PI*半径*半径*高度; } } 方法重载允许您使用不同的参数和返回类型调用GetArea方法

var circleArea=面积。GetArea1.1; var rectrangleArea=Area.GetArea1,2; var cylinderArea=面积。GetArea1.2,2.1; 您只需要将区域方法公开

因为您有静态方法,所以可以直接从类名访问,如

{
    public static double area(double radius)
    {
        return (Math.PI * radius * radius);
    }

    public static double area(int width, int length)
    {
        return (width * length);
    }

    public static double area(double radius, double height)
    {
        return (Math.PI * radius * radius * height);
    }
}


面积是方法,不是变量。要调用静态方法,应将类名放在其前面,如Area.arearadius、height。或者将using static区域添加到using指令。让你的方法成为公共的,而不是私有的`这似乎适用于第一个形状,但是第二个和第三个有两个参数的方法,例如圆柱体有半径和高度,给了我一个重载错误并要求2个参数不能重现重载错误的问题,你的方法在参数类型和返回类型上是不同的,除非您传递了一个类型错误的参数,否则应该没有问题。您不需要创建一个实例来调用静态方法
Area.area(2) //area(double radius)
Area.area(2,2) //area(int width, int length)