views:

412

answers:

3

This is kind of complicated so please bear with me.

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 VB, 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' arguments"

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 i.e.

base of xsTableExtension is SharedCallsTable. 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.

+8  A: 

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.

Greg Beech
+1. A sensible approach.
RichardOD
because the constructor in the SharedCallsTable is designed to work with another constructor in xsTable that takes an integer (notice the call to MyBase.New(forceServerIndex:=sqlServerIndex). the xsTable constructor referenced in this call works with an INI file that stores the connection string.so the call would actually be SharedCallsTable myTable = new SharedCallsTable(lErrString, false, -1);This won't work because we will be running this application from a network machine that doesn't have access to the INI file and needs to pull connection strings from the app.config.
Ryan Muller
Thanks Greg,I was hoping to avoid adding code to the VB side but it looks like that's what I'll have to do. +1
Ryan Muller
+2  A: 

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.

Jon Skeet
+1 for mentioning optional parameters in C# 4.0.
RichardOD
So I could solve this by upgrading to visual studio 2010 and using C# 4.0? I thought it wasn't available yet.
Ryan Muller
@Ryan: It's in beta at the moment, but usable IME. It will be released in March/April next year.
Jon Skeet
A: 

Jon,

when I do this

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.

Ryan Muller