The Setup
Most of our code base is in VB.NET. I'm developing a project in C# that uses a lot of the assemblies from the VB.NET code.
There are three relevant classes in VB.NET:
Public MustInherit Class mdTable
Public Sub New(ByVal sqlConnectionStr As String, Optional ByVal maxSecsToDisableConnection As Integer = 60)
ReDim pConnStr(0)
pConnStr(0) = sqlConnectionStr
pDBName = parseDBName(sqlConnectionStr)
m_colLastConnectStatus.disablePeriod = maxSecsToDisableConnection
End Sub
Public MustInherit Class xsTable //uses the constructor above since second parameter is optional
Inherits mdTable
Public Sub New(ByVal sqlConnectionString As String)
MyBase.New(sqlConnectionString)
End Sub
Public Class SharedCallsTable //the only constructor available in this class
Inherits xsTable
Public Sub New(Optional ByRef lErrStr As String = "", _
Optional ByVal checkTableExists As Boolean = False, _
Optional ByVal sqlServerIndex As Integer = -1)
MyBase.New(forceServerIndex:=sqlServerIndex)
defineTable(DBUser, checkTableExists)
lErrStr &= CStr(IIf(errStr <> "", vbCrLf & errStr, ""))
End Sub
All of these are in Visual Basic, obviously.
There are many different versions of the SharedCallsTable that deal with other table types in our SQL database, SharedCallsTable is just one example.
The problem:
I can not create an instance of SharedCallsTable by using the xsTable constructor that takes a single string as a constructor, because it calls the mdTable constructor which has an optional parameter(maxSecsToDisableConnection). C# does not support optional parameters.
So when I do this:
SharedCallsTable myTable = new SharedCallsTable(connectionString);
I get
SharedCallsTable does not contain a constructor that takes '1' argum开发者_JAVA技巧ents
Progress so far
I have created another class, xsToolboxTable, in C# that inherits xsTable and just calls the single string constructor like so:
class xsToolboxTable : xsTable
{
public xsToolboxTable(string connectionString) : base(connectionString)
{
}
}
However, this means I can only instantiate an xsTable, but not an instance of SharedCallsTable since they both inherit from the same class.
I have also tried making my extension class inherit from SharedCallsTable, but then it gives me the same.
I get
SharedCallsTable does not contain a constructor that takes '1' arguments
What I really need to do is call the base of the base class constructor, that is, the base of xsTableExtension is SharedCallsTable. The base of SharedCallsTable is xsTable which has the single string constructor that I need to use.
I know this is really convoluted and there may be a really simple solution that I'm just completely missing.
This is kind of complicated, so please bear with me.
Is there some reason you can't just pass values for the optional parameters? e.g.
SharedCallsTable myTable = new SharedCallsTable(connectionString, false, -1);
Optional parameters are really just a compiler trick whereby if you don't supply the value for a parameter the compiler has a look what the default value is and supplies for you. So by supplying values that are the same as the defaults, you'll observe the same behaviour as if optional parameters were supported and you omitted them.
Edit:
OK from the comment I think I misunderstood your question. It sounds like what you're asking for is to be able to create an instance of SharedCallsTable
with a user-provided connection string, bypassing its constructor logic, as if it had a constructor that chained directly to the base class constructor.
You can't do this, and for a good reason. Constructors do the work to get an object in the right state to be used, and you cannot simply bypass that logic an expect an object to work.
If you want to be able to provide a connection string, you'll need to add a constructor to SharedCallsTable
which chains to the base class and ensures the class is in a reasonable state to be used.
No, you don't want to be calling the "base of the base class constructor" - you want to be calling the base class constructor as normal, just explicitly passing in all arguments. Just treat it as a method which doesn't have any optional parameters, basically. This is true for both the constructor call elsewhere and chaining:
SharedCallsTable myTable = new SharedCallsTable(connectionString, false, -1);
and you can derive from the class like this, if you want to:
class xsTableExtension : xsTable
{
public xsToolboxTable(string connectionString) : base(connectionString, 60)
{
}
}
(Don't derive from the class just to call the constructor with some defaults though.)
Note that C# 4.0 does support optional parameters btw, so there's light at the end of the tunnel.
When I do this as suggested by Jon
class xsTableExtension : xsTable
{
public xsToolboxTable(string connectionString) : base(connectionString, 60)
{
}
}
I get the same error as before
xsTable does not contain a constructor that takes '2' arguments
My solution (for future reference) is to override the constructor in the grandparent class mdTable with one that does not take an optional parameter and just accepts a connection string.
Until C# 4.0 is out that is.
精彩评论