views:

260

answers:

5

I found the following syntax as a VB.NET property and I'm trying to convert it to c#, but I'm not sure how to accomplish that.

Public Property SomeText(ByVal someEnumThing as SomeEnum) As String
    Get
        Select Case someEnumThing
            //figure out what string to return
        End Select
    End Get
    Set(ByVal Value as String)
        Select Case someEnumThing
            //figure out what string to set
        End Select
    End Set
End Property

I've never seen a property done like this before, any ideas?

+4  A: 

Hmm... maybe the switch statement?

Donut
+1  A: 

The Select Case will be a switch statement. Is that what you are specifically referring to?

EDIT: here's what I was referring to in my comment to @Lucero's answer to get close to the VB.NET syntax.

private SomeEnum SomeEnumThing { get; set; }
public string SomeText {
    get {
        switch (SomeEnumThing) {
        //figure out what string to return
        }
    }
    set {
        switch (SomeEnumThing) {
        //figure out what string to set
        }
   }

}

Ahmad Mageed
+11  A: 

I guess you're referring to the arguments for the property. Well, as far as I know, C# only supports them for indexers, which cannot have a name (e.g. this[SomeEnum someEnumThing] {}).

If you want to get a similar behavior, you can create a helper class with an indexer property and use it to expose the "name" of the property:

public class YourClass {
 public struct SomeTextProperty {
  private readonly YourClass owner;

  internal SomeTextProperty(YourClass owner) {
   this.owner = owner;
  }

  public string this[SomeEnum someEnumThing] {
   get {
    return owner.GetSomeText(someEnumThing);
   }
   set {
    owner.SetSomeText(someEnumThing, value);
   }
  }
 }

 public SomeTextProperty SomeText {
  get {
   return new SomeTextProperty(this);
  }
 }

 private string GetSomeText(SomeEnum someEnumThing) {
  // implementation to get it
 }

 private void SetSomeText(SomeEnum someEnumThing, string value) {
  // implementation to set it
 }
}
Lucero
+1 I'd wager this is what the OP is referring to.
Ben M
FWIW, Ayende wrote a small trick to "get" named indexers in C# in his Rhino.Commons library.
Martinho Fernandes
@Lucero You nailed it, I was hoping there would be a c# equivalent, but I'm thinking I'm just screwed now. =(
Joseph
+1 This is the right answer.
Andrew Hare
@Joseph: well, not really. You *could* conceivably have another property to represent the SomeEnum parameter. Then inside the get/set as provided by @Andrew Hare you would reference the new property.
Ahmad Mageed
+1 - and updated my post with an example of what I mentioned above.
Ahmad Mageed
@Ahmad Yeah but that would break legacy code so that won't work. I'm going to have to refactor the legacy code first and then migrate it.
Joseph
Ah you beat me. It is possible to create the exact same functionality in C# by creating a specialized class that uses indexes and takes the parent class as a parameter. In this way it can notify the parent class of the index being used and would allow you to give it a name. Depending on how much time it would take to refactor the legacy code you may want to consider this option instead.
Spencer Ruport
+4  A: 

It is impossible to create a Property in C# which has arguments, unless it's the default property:

public double this[int index]
{
  get {...}
  set {...}
}

Just one of those areas where VB differs from C#. It is not recommended to use syntax like this since you will not be able to use that property from a C# project that references this assembly.

David Rutten
That's what I was afraid of...
Joseph
+2  A: 

If you're talking about the fact that the property is parameterized...

There's no direct translation for this in c# that I know of. Basically this is carryover from VB6 where you could make this weird quasi-collection property for a class. The easiest way to get similar functionality is to create a dictionary object and either publicly expose it or create an accessor. Where the VB code accesses this property like Class.SomeText("SomeKey") your C# code will become Class.SomeDictionaryProperty["SomeKey"]

Unfortunately this still isn't quite the same since the collection accessor won't be able to "see" the index value. This has been a minor source of frustration for me as well in the past coming from a VB background.

Spencer Ruport