Zack Antony Bucci
Zack Antony Bucci

Reputation: 591

Inherit a base class in C#

I have a VB class that is from another project that is referenced. It is written in VB and the function GetCachedObject is protected so I cannot use it.

I am trying to use the VB class as a base class so I can derive and use it's functions.

Here is the base class in VB:

 Public MustInherit Class RequestLifetimeCacheBase

    Private ReadOnly _requestContext As HttpContextBase

    Public Sub New(requestContext As HttpContextBase)
        Me._requestContext = requestContext
    End Sub

    Public Sub New(controller As Controller)
        Me.New(controller.HttpContext)
    End Sub

    Protected Function GetCachedValue(Of TValue As Structure)(cacheKey As String) As TValue?
        Dim value = Me._requestContext.Items(cacheKey)
        If TypeOf value Is TValue Then
            Return DirectCast(value, TValue)
        Else
            Return Nothing
        End If
    End Function

    Protected Sub SetCachedValue(Of TValue As Structure)(cacheKey As String, value As TValue?)
        If value.HasValue Then
            Me._requestContext.Items(cacheKey) = value.Value
        Else
            Me._requestContext.Items.Remove(cacheKey)
        End If
    End Sub


    Protected Function GetCachedObject(Of TObject As Class)(cacheKey As String) As TObject
        Return TryCast(Me._requestContext.Items(cacheKey), TObject)
    End Function

    Protected Sub SetCachedObject(Of TObject As Class)(cacheKey As String, value As TObject)
        If value IsNot Nothing Then
            Me._requestContext.Items(cacheKey) = value
        Else
            Me._requestContext.Items.Remove(cacheKey)
        End If
    End Sub

End Class

I have done the following on my end in C#:

public class RequestLifetimeCache : RequestLifetimeCacheBase
{

    public RequestLifetimeCache(Controller controller) : base(controller)
    {

    }

    public WebsiteDatabaseContext CustomerDatabase
    {
        get
        {
            return RequestLifetimeCacheBase.GetCachedObject("Customer database");
        }

    }

}

But I am getting two errors:

: base(controller) - Reference to type 'HttpContextBase' claims it is defind in 'System.Web', but it could not be found

And also (due to the protected function)

return RequestLifetimeCacheBase.GetCachedObject("Customer database") - Cannot access protected member 'RequestLifetimeCacheBase.GetCachedObject(string)' via a qualifier of type 'RequestLifetimeCacheBase'; the qualifier must be of type 'RequestLifetimeCache' (or derived from it

Is there a different way of doing it?

I am using .NET Core 2.2

Cheers

Upvotes: 1

Views: 211

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415715

The first error is unrelated to the inheritance problem of the question. You may just be missing a using directive or reference.

For the second error, there are two problems on this line:

return RequestLifetimeCacheBase.GetCachedObject("Customer database");

First of all, the call to GetCachedObject() also needs a type argument, and that type argument must be a reference type — no values types (structs or primitives) allowed.

Second, use the base keyword rather than the name of the type.

The result will look more like this:

return base.GetCachedObject<WebsiteDatabaseContext>("Customer database");

Upvotes: 2

Related Questions