chum of chance
chum of chance

Reputation: 6290

How do I implement this class given the base class in C#?

I'm trying to implement a class in C# with a base class (DataStore) in a compiled assembly. It wants a signature like this (the implementation of RavenDb is not pertinent):

    public RavenDbDataStore(TimeSpan objectLifetime) : base(objectLifetime)
    {
    }

    public RavenDbDataStore(TimeSpan objectLifetime, bool enableCaching) : base(objectLifetime, enableCaching)
    {
    }

However the object that is calling this class is expecting a constructor taking two strings. I get a warning about the base class, DataStore not containing any parameterless constructors.

This doesn't make sense, peeking at the API of the project I'm working on, another class is doing what I'm attempting to do and taking in two strings as a constructor while only inheriting from the DataStore class.

Any ideas on how to accomplish this?

EDIT: I made a mistake and had the second constructor marked as "private" it should be public.

When trying:

public RavenDbDataStore(TimeSpan objectLifetime, string param1, string param2) : base(objectLifetime) { }

I still get:

Could not find constructor in ReflectionUtil.CreateObject: RavenDbDataStore. The constructor parameters may not match or it may be an abstract class. Parameter info: Count: 2. Parameter types: System.String, System.String

Upvotes: 2

Views: 471

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564413

The issue is that the constructor of your base class which takes two parameters is marked private, which means you can't use it.

You need to only use the constructor which takes a single parameter (the TimeSpan), and provide that to the base class:

public YourClass(TimeSpan lifetime) : base(lifetime)
{
}

If you want a second constructor that provides other arguments, that's possible:

public YourClass(TimeSpan lifetime, string someOtherParam) : base(lifetime)
{
     // use someOtherParam
}

Edit in response to edit:

The error message:

Parameter info: Count: 2. Parameter types: System.String, System.String

Suggests that you need to pass two strings, but NOT the lifetime, to the base class:

public RavenDbDataStore(TimeSpan objectLifetime, string param1, string param2) 
    : base(param1, param2) { }

Upvotes: 5

Related Questions