Phrasing of overloaded constructors

M

Mike Labosh

Below, I want the first constructor to pass a default to the second
constructor, but I can't seem to work out the syntax to do so. For example,
in VB.NET I can do this:

Sub New()
Me.New(passDefaultStuff)
End Sub
Sub New(stuff)
'do stuff
End Sub

Here is my class in C#:

public enum DataProvider {Odbc, OleDb, Oracle, SqlServer}

public abstract class DataProcessor {

private DataProvider _provider = DataProvider.SqlServer;
private string _connectionString;

public DataProcessor() {
// I want to have the default constructor pass
// a default value to the other constructor
DataProcessor(DataProvider.SqlServer);
~~~~~~~~~ <-- blue error line when I try to build:
"DataProcessor denotes a class, which is not valid
in the given context"

}
public DataProcessor(DataProvider dataProvider) {
this.Provider = dataProvider;
try {
this.ConnectionString =
ConfigurationSettings.AppSettings["connectionString"];
}
catch {} // Throw away
}

public string ConnectionString {
get {return _connectionString;}
set {

if (value == null | value.Length == 0)
throw new ArgumentNullException("connectionString",
"connectionString cannot be null / Nothing / ZLS");

_connectionString = value;
}
}
}
 
W

W.G. Ryan eMVP

Oops, I hit send too quickly:
public DataProcessor(): this(DataProvider.SqlServer) {
// I want to have the default constructor pass
// a default value to the other constructor
DataProcessor(DataProvider.SqlServer);
~~~~~~~~~ <-- blue error line when I try to build:
"DataProcessor denotes a class, which is not valid
in the given context"

}

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Mike Labosh said:
Below, I want the first constructor to pass a default to the second
constructor, but I can't seem to work out the syntax to do so. For example,
in VB.NET I can do this:

Sub New()
Me.New(passDefaultStuff)
End Sub
Sub New(stuff)
'do stuff
End Sub

Here is my class in C#:

public enum DataProvider {Odbc, OleDb, Oracle, SqlServer}

public abstract class DataProcessor {

private DataProvider _provider = DataProvider.SqlServer;
private string _connectionString;

public DataProcessor() {
// I want to have the default constructor pass
// a default value to the other constructor
DataProcessor(DataProvider.SqlServer);
~~~~~~~~~ <-- blue error line when I try to build:
"DataProcessor denotes a class, which is not valid
in the given context"

}
public DataProcessor(DataProvider dataProvider) {
this.Provider = dataProvider;
try {
this.ConnectionString =
ConfigurationSettings.AppSettings["connectionString"];
}
catch {} // Throw away
}

public string ConnectionString {
get {return _connectionString;}
set {

if (value == null | value.Length == 0)
throw new ArgumentNullException("connectionString",
"connectionString cannot be null / Nothing / ZLS");

_connectionString = value;
}
}
}

--
Peace & happy computing,

Mike Labosh, MCSD
"I have no choice but to believe in free will."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top