Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#: Converting a Get / Set from vb.net to c#

Can anyone help.

I have the following in vb.net and need to convert it to c#. Seemed rather simple at first but I need to pass in the NamedObject variable as welll which is supported in vb.net but not in c#..

What are my options.

Here is the vb.net - notice the NamedObject

    Public Property Datos(ByVal NamedObject As String) As T
        Get
            Return CType(HttpContext.Current.Session.Item(NamedObject ), T)
        End Get
        Set(ByVal Value As T)
            HttpContext.Current.Session.Item(NamedObject ) = Value
        End Set
    End Property

and this is c# but it errors as it appears i can't pass in a parameter on a property

    public T Datos(string NamedObject)
    {
        get { return (T)HttpContext.Current.Session[NamedObject]; }
        set { HttpContext.Current.Session[NamedObject] = Value; }
    }

I would appreciate any input..

Thanks

like image 591
mark smith Avatar asked Feb 28 '23 10:02

mark smith


1 Answers

You are looking for the indexer property. Basically implement a property called this. There's a nice tutorial here.

True, you are restricted to the one indexer, but you will be able to implement something like this:

public T this[string namedObject]
{
    get { return (T)HttpContext.Current.Session[namedObject]; }
    set { HttpContext.Current.Session[namedObject] = Value; }
}
like image 53
Peter Lillevold Avatar answered Mar 08 '23 03:03

Peter Lillevold