【问题标题】:C#: The type name <type_name> does not exist in the type <type>; In one class, calling a method from another classC#:类型名称 <type_name> 不存在于类型 <type> 中;在一个类中,调用另一个类的方法
【发布时间】:2018-08-23 23:25:45
【问题描述】:

C# 我搜索了但找不到我自己所处的具体情况。如何从另一个类调用一个类中的方法?

public class Box
{
    public double length;   // Length of a box
    public double breadth;  // Breadth of a box
    public double height;   // Height of a box

    public double Volume(double len, double bre, double hei)
    {
        double totvolume;
        totvolume = len * bre * hei;
        return totvolume;
    }

}

public class Boxtester
{

    static void Main(string[] args)
    {
        Box Box1 = new Box();   //create new class called Box1
        Box Box2 = new Box();   //create new class called Box2

        double returnvolume;    //create variable to hold the returned data from method


        // box 1 specification
        Box1.length = 6.0;
        Box1.breadth = 7.0;
        Box1.height = 5.0;

        // box 2 specification
        Box2.length = 11.0;
        Box2.breadth = 16.0;
        Box2.height = 12.0;


        //Calculate and display volume of Box1
        Box.Volume volumebox1 = new Box.Volume();                                 //creating new instance of Volume method called volumebox1
        returnvolume = volumebox1.Volume(Box1.length, Box1.breadth, Box1.height); //giving variables to method
        Console.WriteLine("Volume of Box1 : {0}", volumebox1);                    //write return value

        //Calculate and display volume of Box2
        Box.Volume volumebox2 = new Box.Volume();                                 //creating new instance of Volume method called volumebox2
        returnvolume = volumebox2.Volume(Box2.length, Box2.breadth, Box2.height); //giving variables to method
        Console.WriteLine("Volume of Box1 : {0}", volumebox2);                    //write return value

        Console.ReadKey();
    }

这给出了错误“类型名称'Volume'不存在于类型'Box'中

【问题讨论】:

  • 您是否尝试将Box.Volume volumebox1 = new Box.Volume(); //creating new instance of Volume method called volumebox1 returnvolume = volumebox1.Volume(Box1.length, Box1.breadth, Box1.height); 更改为:var returnvolume = Box1.Volume(Box1.length, Box1.breadth, Box1.height);

标签: c# class methods types typename


【解决方案1】:

Box.Volume volumebox1 = new Box.Volume(); 这行不行。需要自己调用box1和box2的方法。

正确的调用是:

double Box1Volume = Box1.Volume(Box2.length, Box2.breadth, Box2.height);

我会将音量设置为属性,这样您可以访问就像一个变量但获得正确的值。

就我个人而言,我更喜欢使用属性,这样您就可以在需要时在访问之前操作值(如果需要),例如音量。

这是我的例子:

using System;

public class Program
{
    public static void Main()
    {

        Box Box1 = new Box();   //create new class called Box1
        Box Box2 = new Box();   //create new class called Box2

        // box 1 specification
        Box1.Length = 6.0;
        Box1.Breadth = 7.0;
        Box1.Height = 5.0;

        // box 2 specification
        Box2.Length = 11.0;
        Box2.Breadth = 16.0;
        Box2.Height = 12.0;

        Console.WriteLine("Volume of Box1 : {0}", Box1.Volume);
        Console.WriteLine("Volume of Box2 : {0}", Box2.Volume);     

    }
}

public class Box
{
    public double Length {get;set;}   // Length of a box
    public double Breadth {get;set;} // Breadth of a box
    public double Height {get;set;}// Height of a box
    public double Volume { get { return Length * Breadth * Height;}}
}

这给出了输出

Volume of Box1 : 210
Volume of Box2 : 2112

【讨论】:

  • 音量设置器完全没用,而且您正在创建一个带有支持字段的属性,您也不能使用。你可以让它像public double Volume { get { return Length * Breadth * Height; } }一样简单
  • @Antry 是的,也是如此,尽管我认为这样做看起来更容易理解。我将修复该集合并将您的示例添加为评论。
  • 你还是对的 :p 哦该死,现在一切都是财产哈哈
  • 好吧,我个人在 9/10 的情况下使用属性。不确定这是否不是一个好习惯,但它有时对我来说非常方便。
  • 据我所知,它们在性能影响方面确实没有太大差异(FieldProperty)。需要注意的是,当 Local 合适时,不要使用字段和属性。
【解决方案2】:
  public static class Box1
    {
        public static double length;   // Length of a box
        public static double breadth;  // Breadth of a box
        public static double height;   // Height of a box

        public static double Volume(double len, double bre, double hei)
        {
            double totvolume;
            totvolume = len * bre * hei;
            return totvolume;
        }

    }
   public static class Box2
    {
        public static double length;   // Length of a box
        public static double breadth;  // Breadth of a box
        public static double height;   // Height of a box

        public static double Volume(double len, double bre, double hei)
        {
            double totvolume;
            totvolume = len * bre * hei;
            return totvolume;
        }

    }
    public class Boxtester
    {

        static void Main(string[] args)
        {
            // box 1 specification
            Box1.length = 6.0;
            Box1.breadth = 7.0;
            Box1.height = 5.0;

            // box 2 specification
            Box2.length = 11.0;
            Box2.breadth = 16.0;
            Box2.height = 12.0;

            Console.WriteLine("Volume of Box1 : {0}", Box1.Volume(Box1.length, Box1.breadth, Box1.height));
            Console.WriteLine("Volume of Box1 : {0}", Box2.Volume(Box2.length, Box2.breadth, Box2.height));


            Console.ReadKey();
        }
    }

【讨论】:

    【解决方案3】:

    您的代码应该调用Volume() 方法,而不是创建不存在的Volume 类型的变量。此外,Volume() 应该使用类成员值,而不是参数:

    public class Box
    {
        public double length;   // Length of a box
        public double breadth;  // Breadth of a box
        public double height;   // Height of a box
    
        public double Volume() 
        {
            double totvolume;
            totvolume = length * breadth * height;
            return totvolume;
        }
    
    }
    
    public class Boxtester
    {
    
        static void Main(string[] args)
        {
            Box Box1 = new Box();   //create new variable of type Box called Box1
            Box Box2 = new Box();   //create new variable of type Box called Box2
    
            double returnvolume;    //create variable to hold the returned data from method
    
    
            // box 1 specification
            Box1.length = 6.0;
            Box1.breadth = 7.0;
            Box1.height = 5.0;
    
            // box 2 specification
            Box2.length = 11.0;
            Box2.breadth = 16.0;
            Box2.height = 12.0;
    
    
            //Calculate and display volume of Box1
            returnvolume = Box1.Volume(); 
            Console.WriteLine("Volume of Box1 : {0}", volumebox1);                    //write return value
    
            //Calculate and display volume of Box2
            returnvolume = Box2.Volume(); 
            Console.WriteLine("Volume of Box2 : {0}", volumebox2);                    //write return value
    
            Console.ReadKey();
        }
    

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多