Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the typical get set properties in C#... with parameters

I'd like to do the same in C#. Is there anyway of using properties in C# with parameters in the same way I've done with the parameter 'Key' in this VB.NET example?

Private Shared m_Dictionary As IDictionary(Of String, Object) = New Dictionary(Of String, Object)
Public Shared Property DictionaryElement(ByVal Key As String) As Object
    Get
        If m_Dictionary.ContainsKey(Key) Then
            Return m_Dictionary(Key)
        Else
            Return [String].Empty
        End If
    End Get
    Set(ByVal value As Object)
        If m_Dictionary.ContainsKey(Key) Then
            m_Dictionary(Key) = value
        Else
            m_Dictionary.Add(Key, value)
        End If

    End Set
End Property

Thanks

like image 721
Javier Avatar asked Oct 25 '08 14:10

Javier


3 Answers

Is there anyway of using properties in C# with parameters

No. You only can provide the default property in C# with an argument, to model indexed access (as in a dictionary):

public T this[string key] {
    get { return m_Dictionary[key]; }
    set { m_Dictionary[key] = value; }
}

Other properties can't have arguments. Use a function instead. By the way, it's recommented to do the same in VB so other .NET languages (C# …) can use your code.

By the way, your code is unnecessarily complicated. Four things:

  • You don't need to escape the String identifier. Use the keyword directly.
  • Why not use ""?
  • Use TryGetValue, it's faster. You query the dictionary twice.
  • Your setter doesn't have to test whether the value already exists.

Public Shared Property DictionaryElement(ByVal Key As String) As Object
    Get
        Dim ret As String
        If m_Dictionary.TryGetValue(Key, ret) Then Return ret
        Return "" ' Same as String.Empty! '
    End Get
    Set(ByVal value As Object)
        m_Dictionary(Key) = value
    End Set
End Property
like image 110
Konrad Rudolph Avatar answered Nov 03 '22 02:11

Konrad Rudolph


The "proper" way to do it in C# is to create child class specifically to access the collection. It should either hold the collection itself or have internal linkages to the parent class.

like image 24
Jonathan Allen Avatar answered Nov 03 '22 02:11

Jonathan Allen


A more general-purpose, safer, and reusable solution to your problem might be implementing a generic, "parameterized" property class, like this:

    // Generic, parameterized (indexed) "property" template
    public class Property<T>
    {
        // The internal property value
        private T PropVal = default(T);

        // The indexed property get/set accessor 
        //  (Property<T>[index] = newvalue; value = Property<T>[index];)
        public T this[object key]
        {
            get { return PropVal; }     // Get the value
            set { PropVal = value; }    // Set the value
        }
    }

You could then implement any number of properties within your public class so that clients could set/get the properties with an index, descriptor, security key, or whatever, like this:

    public class ParameterizedProperties
    {
        // Parameterized properties
        private Property<int> m_IntProp = new Property<int>();
        private Property<string> m_StringProp = new Property<string>();

        // Parameterized int property accessor for client access
        //  (ex: ParameterizedProperties.PublicIntProp[index])
        public Property<int> PublicIntProp
        {
            get { return m_IntProp; }
        }

        // Parameterized string property accessor
        //  (ex: ParameterizedProperties.PublicStringProp[index])
        public Property<string> PublicStringProp
        {
            get { return m_StringProp; }
        }
    }

Finally, client code would access your public class's "parameterized" properties like this:

        ParameterizedProperties parmProperties = new ParameterizedProperties();
        parmProperties.PublicIntProp[1] = 100;
        parmProperties.PublicStringProp[1] = "whatever";
        int ival = parmProperties.PublicIntProp[1];
        string strVal = parmProperties.PublicStringProp[1];

Sure, this seems weird, but it definitely does the trick. Besides, from a client-code perspective, it's not weird at all -- it's simple and intuitive and acts just like real properties. It doesn't break any C# rules, nor is it incompatible with other .NET managed languages. And from the class-implementer's perspective, creating a reusable, generic, "parameterized" property template class makes component coding a relative breeze, as shown here.

NOTE: You can always override the generic property class to provide custom processing, such as indexed lookup, security-controlled property access, or whatever-the-heck you want.

Cheers!

Mark Jones

like image 41
Mark Jones Avatar answered Nov 03 '22 00:11

Mark Jones