帮助歧义错误请:) [英] Help with ambiguity error please :)

查看:85
本文介绍了帮助歧义错误请:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我正在尝试编写一个小程序,需要一些帮助.因为我目前是学生,所以我使用Microsoft Visual C#Express 2008进行编码.这是我遇到麻烦的代码:

I''m new to C# and need some help with a small program I''m trying to code. I use Microsoft Visual C# Express 2008 for my coding as I am currently a student. Here is the code that I am having trouble with:

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

namespace Vehicle
{
    class Vehicle
    {
        //Variable declaration
        public string Make = "";
        public string Model = "";
        public int Condition = 0;
        public double Price = 0;
        
        //Empty constructor
        public Vehicle()
        {
        }

        //Constructor
        public Vehicle(string newMake, string newModel, int newCondition, double newPrice)
        {
            Make = newMake;
            Model = newModel;
            Condition = newCondition;
            Price = newPrice;
        }
    }
}



我收到以下错误消息:




The following error messages are what I am receiving:


1 Ambiguity between 'Vehicle.Vehicle.Make' and 'Vehicle.Vehicle.Make'
2 Ambiguity between 'Vehicle.Vehicle.Model' and 'Vehicle.Vehicle.Model'
3 Ambiguity between 'Vehicle.Vehicle.Condition' and 'Vehicle.Vehicle.Condition'
4 Ambiguity between 'Vehicle.Vehicle.Price' and 'Vehicle.Vehicle.Price'



任何有关如何解决此问题的帮助将不胜感激:)



Any help as to how to resolve this will be much appreciated :)

推荐答案

它可以完美地编译.您很可能在其他文件中使用了相同的名称.

具有与某些类相同的名称空间是一种不好的命名方式,没有多大意义,但并不是真正的错误.我没有提到这段代码的质量,但至少不要使用",而应使用string.Empty.

有趣的是,两个人给出了不尝试编译此代码的错误答案.它会编译,不用担心.

—SA
Of course there is nothing wrong about this code; it compiles perfectly. Most likely, you used the same name in some other file, that''s it.

Having the namespace same as some class is a bad naming style and does not make much sense, but not really a mistake. I did not mention the quality of this code, but at least never use "", use string.Empty instead.

It''s funny that two people gave wrong answers not trying to compile this code. It compiles, don''t worry.

—SA


您的课程必须是公共的


Your class must be Public


public class Vehicle
    {
        //Variable declaration
        public string Make{get; set;}        
        public string Model{get; set;}
        public int Condition{get; set;}
        public double Price{get; set;}

        //Empty constructor
        public Vehicle():this(string.Empty, string.Empty, 0, 0)
        {
        }

        //Constructor
        public Vehicle(string newMake, string newModel, int newCondition, double newPrice)
        {
            Make = newMake;
            Model = newModel;
            Condition = newCondition;
            Price = newPrice;
        }
    }


奇怪的是,您应该会收到一条错误消息,指出该类与其封闭的名称空间不能使用相同的名称...尝试重命名该名称空间(改名为VehicleExercise之类的名称,说),看看错误是否消失了.
Odd, you should be getting an error saying that the class cannot have the same name as its enclosing namespace... try renaming the namespace (to something like VehicleExercise, say) and see if the errors go away.


这篇关于帮助歧义错误请:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆