Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I was wondering if anyone had some thoughts on assigning a Delegate to a Property, as defining a "Property Delegate" does not seem to be allowed in VB.NET. However, since the underlying accessor methods are really just get_Property() and set_Property(value) methods, this restriction seems odd.

 

In VB.NET, the accessor methods do not appear to be publicly exposed. Are they in C#? (I'm guessing no?)

 

I guess I could create a Public Get and Set method pair and have those methods called by the respective Property Get and Property Set functions. But since the methods would have to be public (if I wish to assign delegates to them) it is then redundant to also have a public property doing exactly the same thing -- so I may as well throw out the property altogether and just stick with a Get and Set method pair only.

 

I could use an interface that includes a property and then pass in a reference to the class that implements the property. This is fine, I guess, but not as flexible as a delegate, unless one is willing to make a lot of pre-defined interfaces with different property return types.

 

A final thought is that I could use Reflection to find the property's accessor methods and then create an instance of the PropertyInfo class, which would effectively become my *Delegate*. A little ugly, but doable.

 

Funny that I could create a "surrogate delegate" via a PropertyInfo class or via a class+interface, but directly using a Delegate class appears to be off limits to Properties...

 

Does anyone have any thoughts or ideas on this?

Mike

Edited by Mike_R

Posting Guidelines

 

Avatar by Lebb

Posted
delegates exposed as properties in C# are usually events. But you also expose a public property that is a delegate. Here's both:
public class MyClass
{
   public delegate void MyDelegate();

   private MyDelegate _delegateProperty;
   
   public MyDelegate DelegateProperty
   {
       get { return _delegateProperty; }
       set { _delegateProperty = value; }
   }

   public event MyDelegate SomeEvent
   {
       add { _delegateProperty += value; }
       remove { _delegateProperty -= value; }
   }
}

Posted

Ok, thanks guys, I appreciate it... But that wasn't quite what I was looking for. I'm sorry, I guess I was not clear on what I meant.

 

In VB.NET (and I would assume in C# as well) a delegate cannot be defined as a Property. Only a Method can be defined as part of a delegate's signature. This means that in VB.NET a Delegate class can be defined for a Sub or a Function, but not for a Property.

 

For example, the following is permitted in VB.NET:

Delegate Function MyFunction As String

while the following is not legal:

Delegate ReadOnly Property MyFunction As String

This would seem a bit of an odd restriction given that a read only Property and a parameterless Function are essentially the same.

 

But I guess in general, the problem with having a Delegate that defines a Property is that most properties are Read-Write and so defining a Delegate to it would be misleading as one would actually need TWO delegates: one for 'Get' and the other for 'Set'. So restricting Delegate definitions to Methods-only really does makes sense after all...

 

Thanks guys,

Mike

Posting Guidelines

 

Avatar by Lebb

  • Administrators
Posted

Ahhh, get what you mean. As far as I am aware delegates cannot be defined as a property, this could be due to the fact that not all languages support properties (but I'm guessing), or the fact that a property doesn't seem to be a method call.

 

If you require the multicast functionality of delegates then you are probably out of luck, if it is just as a method signature then you could always define an interface with a single property defined instead.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
Mike_R, you sure manage to find the really interesting stuff. Very cool find. It might be worth noting that the names of the properties change in the compiled IL. When you reflect on a property, two methods are indeed generated for a general property: "get_MyProperty" and "set_MyProperty". So I suppose it does make it sort of difficult to get the address of something that doesn't yet have a permanent name.
  • Leaders
Posted

I second that, mskeel. Interesting point indeed. It is unfortunate that Microsoft felt the need to completely hide the accessors from you without providing some kind of complementary feature to provide a delegate-like operation with a property. How about something like:

delegate get int myGetAccessor();
void stuff() {
   myGetAccessor g = (get)this.SomeIntProperty;
}

Delegate Property Get MyGetAccessor() As Integer
Sub Stuff()
   Dim g As MyGetAccessor = AddressOf Me.SomeIntProperty
End Sub

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...