views:

152

answers:

4

Hi there,

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

+3  A: 

C# doesn't support named parameterful properties like VB.NET does. In order to make this work you will have to create two methods like this:

public T GetDatos(String NamedObject)
{
    return (T)HttpContext.Current.Session[NamedObject];
}
public void SetDatos(String NamedObject, T Value)
{
    HttpContext.Current.Session[NamedObject] = Value;
}
Andrew Hare
You'll need to pass Value trough as a parameter in the set method.
Cornelius
@Cornelius - Nice catch!
Andrew Hare
+4  A: 

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; }
}
Peter Lillevold
This isn't an indexer, this is a named, parameterful property which C# does not support.
Andrew Hare
@Andrew: sure. But the OP asks for options. Imo the indexer is a good one.
Peter Lillevold
@James: imo, neither is @Andrews answer. I will not downvote him still, because it is just another viable option.
Peter Lillevold
@Peter, apologies I actually revoked my downvote as I never actually read the question properly.
James
@James: thanks!
Peter Lillevold
A: 

In C# you can make an indexer, but not a named indexer:

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

You can make a collection that uses the Session state as storage, and declare a property that returns the collection:

public class DatosCollection {

  public T this[string name] {
    get { return (T)HttpContext.Current.Session[name]; }
    set { HttpContext.Current.Session[name] = value; }
  }

}

private _collection = new DatosCollection();

public DatosCollection Datos { get { return _collection; } }
Guffa
A: 

In order to emulate named indexed properties in C# you need to create a proxy object as follows. However, I would suggest a less direct translation if possible, for example as a function.

class DatosProxy<T> {
    public T this[string name] {
        get { return (T)HttpContext.Current.Session[name]; }
        set { HttpContext.Current.Session[name] = value; }
    }
}

And then, in your actual class, simply provide a read-only getter:

private readonly DatosProxy _datos = new DatosProxy();

public DatosProxy Datos { get { return _datos; } }

Notice that this is not 100% equivalent code but the usage from the user’s point of view is identical.

Konrad Rudolph