Reputation: 57
I have a class as follows:
public abstract class cDBBase
{
public cDBBase()
{
some codes
}
Some codes;
}
I have another class
public class cSQL : cDBBase
{
public void cSQL()
{
Some codes;
}
}
Why do I get error "member name cannot be the same name as their enclosing type" on the child class constructor?
Here are complete code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Uploader
{
public abstract class cDBBaseClass
{
public string ServerName { get; set; }
public string DBName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string ConnString;
public cDBBaseClass(string serverName, string dBName, string userName, string password)
{
ServerName = serverName;
DBName = dBName;
UserName = userName;
Password = password;
}
public abstract string SetConnString();
public abstract void SetConn();
}
}
and the child class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Uploader.Classes
{
public class cSQL:cDBBaseClass
{
private SqlConnection Conn = new SqlConnection();
public cSQL()
{
SetConnString();
SetConn();
}
public override string SetConnString()
{
return "data source = " + this.ServerName +
"; database = " + this.DBName +
"; User ID = " + this.UserName +
"; Password = " + this.Password;
}
public override void SetConn()
{
Conn.ConnectionString = this.ConnString;
}
public SqlDataReader ExecSQL(string Query)
{
using (SqlConnection Conn = new SqlConnection(SetConnString()))
{
Conn.Open();
SqlCommand cmd = new SqlCommand(Query , Conn);
return cmd.ExecuteReader();
}
}
public SqlDataReader ExecStoredProcedure(string SPName)
{
using (SqlConnection Conn = new SqlConnection(SetConnString()))
{
Conn.Open();
SqlCommand cmd = new SqlCommand(SPName, Conn);
cmd.CommandType = CommandType.StoredProcedure;
return cmd.ExecuteReader();
}
}
}
}
Upvotes: 1
Views: 178
Reputation: 66439
tnw really did fix your original issue, but to fix your other issue, your class must accept the same parameters as the base class, and pass them to it:
public class cSQL : cDBBaseClass
{
private SqlConnection Conn = new SqlConnection();
public cSQL(string serverName, string dBName, string userName, string password)
: base(serverName, dBName, userName, password)
{
SetConnString();
SetConn();
}
...
Upvotes: 1
Reputation: 13877
You're defining a method cSQL
which has the exact same name as the class it's contained in. You can't do that. Did you mean to make a constructor instead? Just remove void
.
Upvotes: 6