Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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:


C#
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 :)
Posted

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
 
Share this answer
 
Comments
ProEnggSoft 10-Mar-12 22:32pm    
Good clarification. My 5.
Sergey Alexandrovich Kryukov 10-Mar-12 22:46pm    
Thank you.
--SA
Your class must be Public


C#
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;
        }
    }
 
Share this answer
 
v4
Comments
[no name] 10-Mar-12 15:01pm    
It still gives me the same error...
Shahin Khorshidnia 10-Mar-12 17:16pm    
I just adjusted your code because methods and properties can not have a more access modifier level than the class.

But about the warnings, hard to say it without reviewing all the solution. But I guess you have more than 1 class named "Vehicle".
Check the solution. Refrences and ....
For example rename this class to "Vehicle2" and check for warnings.

And: Thank you for voting 1 !
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.
 
Share this answer
 
Comments
riced 10-Mar-12 17:27pm    
AFAIK a class can have the same name as the enclosing namespace, don't know of anything that prevents.
ekolis 10-Mar-12 17:39pm    
Oh, maybe it was that a property cannot have the same name as its enclosing class - sorry!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900