Hi.
Could you tell me - what is wrong here -

public class MAP 
    {
        public string MAP(string FilePath) 
       {
           FileStream fs; 
           try { fs = new FileStream(FilePath, FileMode.Open); }  
           catch (FileNotFoundException exp) { return exp.Message; }
           catch  { return "Не удаётся открыть файл"; }
           return "all right!";
           
           
       }
        
    }

compiler "says" that -

Error 1 'MAP': member names cannot be the same as their enclosing type

but,as I know - that constructor name should be the same as the name of the class....
thanks in advance )

Recommended Answers

All 10 Replies

YOu names the class as "MAP" and the same name is the method in it.
The constructor can be the only method which can have the same name as the class.

Example:

class ExampleClass
{
    public ExampleClass()
    {
         //constructor of class ExampleClass!
    }

    private void Method1()
    {        
        //method in ExampleClass
    }
   
    private string Method2()   
    {
        method in ExampeClass
        string a = null;
        //some code in here
        return a;
    }
}

remember... no method or even handler can have the same name as the class name (except the class`s constructor).

If you wanted to have some code in the constructor, you can not declare the return type.
You can see the example of constructor declarations here.
Hope this helps,
Mitja

If you wanted to have some code in the constructor, you can not declare the return type.

I didn't know about it.....thank you Mitja Bonca)

You`re welcome.
If there is anything you would like to know, just go ahead and ask.
And please, don`t forget to mark the thread as answered, if we`ve answered on your question. If there is another question in your mind, please start a new thread.
bye

Mitja

Ah ... one more question - Ah ... one more question -Can I transfer the values to the constructor? like this -

public ExampleClass(int a)

You can, like you pass values to for example SqlConnection constructor.

commented: ++++ +1

Of course you can, you can even have things like

public ExampleClass(int a, string b, double c)

See also the link Mitja gave you.

commented: +++ +1

please tell me - what's wrong with this constructor-

public class [B]DinArray[/B] // 1 - int ; 2 - масссив городов -City
    {
        public void [B]DinArray[/B](int size, int type)
        {
            switch (type) 
            {
                case 1: 
                    this.ArrayOfInt = new int[size];
                    this.Length = size;
                break;
                case 2:
                this.ArrayOfCities = new CITY[size];
                this.Length = size;
                break;

            }
         }
     }

error -

Error 1 'DinArray': member names cannot be the same as their enclosing type

a constructor shouldn't have a return type (in your case it's void, this is why you are getting that error message), instead write it like this:

public DinArray(int size, int type)
        {
...
commented: +++++ +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.