CôaCôaCôa

Un peu de tout et n'importe quoi...

C# et les propriétés

With VBScript and VB6, I can have arguments with the Property Get statement, like that :

Private Property Get OneData(ByVal sOneKey As String) As String
If (sOneKey = "dummy") Then
OneData = "qwerty"
Else
OneData = colDatas.item(sOneKey)
End If
End Property
Private Property Let OneData(ByVal sOneKey As String, ByVal sOneValue As String)
If (sOneKey <> "dummy") Then
colDatas.Add sOneKey, sOneValue
End If
End Property

With VB.NET, I can write that :

Public Property OneData(ByVal sOneKey As String) As String
Get
If (sOneKey = "dummy") Then
OneData = "qwerty"
Else
OneData = colDatas.Item(sOneKey)
End If
End Get
Set(ByVal Value As String)
If (sOneKey <> "dummy") Then
colDatas.Add(sOneKey, Value)
End If
End Set
End Property

But with c#, I don't know how to do that, since VS.NET doesn't accept this code :

public string OneData (string sOneKey) {
get {
if (sOneKey != "dummy") {
return _OneData.Values(sOneKey);
} else  {
return "qwerty";
}
}
set {
if (sOneKey != "dummy")
_OneData.Add(sOneKey, value);
}
}

Some hints ?

Comments

Jesse Ezell said:

You should check out indexers for C#. They are the way to accomplish this type of thing (properties with parameters). For your situation, you would most likely isolate the OneData stuff in its own class and put an indexer on it, and then composite OneData with a standard property on your parent class.
# April 9, 2003 8:28 PM

rick said:

Using an indexer as Jesse mentioned, the syntax would look more like...

public string OneData [string sOneKey] {
# April 9, 2003 11:51 PM

David Stone said:

Actually rick, it's going to be:

public string this[string sOneKey]{}
# April 10, 2003 5:06 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)