SHRI
SHRI

Reputation: 2466

member names cannot be the same as their enclosing type C#

The code below is in C# and I'm using Visual Studio 2010.

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

namespace FrontEnd
{
    class Flow
    {
        long i;
        private int x,y;
        public int X
        {
            get;set;
        }
        public int Y
        {
            get;set;
        }

        private void Flow()
        {
            X = x;
            Y = y;
        }

        public void NaturalNumbers(int x, int y)
        {
            for (i = 0; i < 9999; i++)
            {
                Console.WriteLine(i);
            }
            MessageBox.Show("done");
        }
    }
}

When I compile the above code I get this error:

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

Why? How can I resolve this?

Upvotes: 97

Views: 238613

Answers (6)

mshsayem
mshsayem

Reputation: 18008

Method names which are same as the class name are called constructors. Constructors do not have a return type. So correct as:

private Flow()
{
   X = x;
   Y = y;
}

Or rename the function as:

private void DoFlow()
{
   X = x;
   Y = y;
}

Though the whole code does not make any sense to me.

Upvotes: 139

Kunj Sangani
Kunj Sangani

Reputation: 41

As Constructor should be at the starting of the Class , you are facing the above issue . So, you can either change the name or if you want to use it as a constructor just copy the method at the beginning of the class.

Upvotes: 4

Qasim Khan
Qasim Khan

Reputation: 81

Constructors don't return a type , just remove the return type which is void in your case. It would run fine then.

Upvotes: 6

Wouter de Kort
Wouter de Kort

Reputation: 39898

The problem is with the method:

private void Flow()
{
    X = x;
    Y = y;
}

Your class is named Flow so this method can't also be named Flow. You will have to change the name of the Flow method to something else to make this code compile.

Or did you mean to create a private constructor to initialize your class? If that's the case, you will have to remove the void keyword to let the compiler know that your declaring a constructor.

Upvotes: 28

Hoby
Hoby

Reputation: 1077

just remove this because constructor don't have a return type like void it will be like this :

private Flow()
    {
        X = x;
        Y = y;
    }  

Upvotes: 3

Subhranshu
Subhranshu

Reputation: 224

A constructor should no have a return type . remove void before each constructor .

Some very basic characteristic of a constructor :

a. Same name as class b. no return type. c. will be called every time an object is made with the class. for eg- in your program if u made two objects of Flow, Flow flow1=new Flow(); Flow flow2=new Flow(); then Flow constructor will be called for 2 times.

d. If you want to call the constructor just for once then declare that as static (static constructor) and dont forget to remove any access modifier from static constructor ..

Upvotes: 2

Related Questions