9

I got something strange into VB.NET, never notice before...

I have a class in VB.NET having a property with parameter and I want to use that property in other C# Class by making object of VB.NET class but class object not showing that property, could any one tell me whether I can access that property into C# or not.

  • If yes, how?
  • If no, what does CLR mean?

Here is my code...

Public Property AsString(ByVal name As String) As String
    Get
            //Some code
    End Get
    Set(ByVal value As String)
            //Some code
    End Set
End Property

Note: I can not change VB.NET code as it is compiled DLL.

Thanks in Advance

ANKIT
  • 431
  • 1
  • 10
  • 22
  • what do you mean by a property with parameter? – Sleiman Jneidi Dec 21 '11 at 23:06
  • 1
    Why do you mention the [CLR](http://en.wikipedia.org/wiki/Common_Language_Runtime)? – Tim Schmelter Dec 21 '11 at 23:07
  • I know this is 4 years later, but I've not had to do this until now. So is it still the case that C# doesn't support properties that have parameters? I've seen it in VB, but have never seen it in C#. I presume the answer is yes, C# doesn't support that. I just want to verify that. – Rod Nov 18 '15 at 21:02
  • @Rod Yes, C# doesn't support that, except for a single, default property implemented as an indexer via `this`. See [this](http://www.codeproject.com/Questions/348242/how-to-make-parameterized-properties-in-csharp) – ErikE May 13 '16 at 17:55

3 Answers3

10

Parameterised Properties are converted to get_ and set_ methods.

string name = "Foo";
string value = "Bar";
MyObject.set_AsString(name, value);
string fooValue = MyObject.get_AsString(name);
Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80
6

C# doesn't support indexed properties that don't have the Default keyword. You simple use get_AsString() to call the property getter and set_AsString() to call the setter. Methods, not properties. They should readily show up in the IntelliSense list.

Also note that set_AsString() requires two arguments even though you made the property setter non-indexed. Pass anything, null will do.

Fwiw, this is perhaps illustrative of why the C# team decided to not support indexed properties in the general case. The mismatch between the getter and the setter is painful. The vb.net team had no choice, Visual Basic had them long before .NET came around. It did make a partial comeback in C# version 4 though, indexed properties are supported on COM interfaces. COM interop programming is too painful without them. Particularly in the Office object model.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • What "mismatch" are you talking about? The property setter is indexed; the "parameter list" for the setter only includes the value, because it's the only thing which wasn't specified in the property definition itself. There can be a slight apparent ambiguity if an indexed and non-indexed property have the same name, and the type of the non-indexed property has a default indexer, but in reality there is no ambiguity: `thing.prop(x)` will bind to a suitable indexed property if one exists; if one wants to use the non-indexed one and index the result, one can use `(thing.prop)(x)`. – supercat Jun 26 '12 at 17:24
  • Such a thing might be useful if one had e.g. a dictionary which kept keys and values in the order they were added, and allowed keys or values to be accessed via numerical index, in addition to allowing values to be accessed via keys. `MyDict.Keys(2)` would be the third key added, and `MyDict.Values(2)` would be its corresponding value. One could achieve similar results by having `MyDict.Keys` and `MyDict.Values` each return a struct which held a reference to the dictionary, and had an indexer which would call a suitable function on that dictionary, but that would be ickier. – supercat Jun 26 '12 at 17:38
  • If they did it like in C++/CLI and enforced that getter and setter have to define and use the same parameters, then there shouldn't be any confusion: `public string Source[int i_ixSource] { get (int i_ixSource) {...} set (int i_ixSource) {...} }` Currently it's a big pain in the ass for me to work around dozens of nice parameterized properties that I implemented in my C++/CLI code while converting to C#. – Tobias Knauss Jul 11 '17 at 08:54
0

If u have noticed actually there are two parameters in your property one is your's name and other is the default value so u have to give two parameter value when u work with that

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154