473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Delegate Gurus: Can I create a delegate for a property (as opposed to a method)?

All:

I'd like to create a delegate for a property (as opposed to a method). Can
I do this? If so, please teach me the syntactic magic.

For example, suppose I have a boolean ready-only property defined in an
instance of a class name SomeObj as follows:

public bool TheValue
{
get
{
// do some work
return someBoolValue;
}
}

I've tried the following two approaches to creating the delegate but
received compilation errors:

private delegate bool BoolDelegate();

// The following results in compilation error: error CS0118: denotes a
'property' where a 'method' was expected
BoolDelegate del = new BoolDelegate(SomeObj.TheValue);

// The following results in compilation error: error CS0571: cannot
explicitly call operator or accessor
BoolDelegate del = new BoolDelegate(SomeObj.get_TheValue);

Any other ideas?

TIA,
Bill
Nov 17 '05 #1
6 1497
"Bill Davidson" <Ra************@newsgroup.nospam> wrote:
I'd like to create a delegate for a property (as
opposed to a method). Can I do this?


Delegates only work for methods, *but* if you don't mind losing some
run-time type safety, you can achieve what you want using reflection.
The PropertyInfo class represents a property, and its SetValue method
will change the value.

What makes you want this feature? There might be a neater solution.

P.
Nov 17 '05 #2
Paul:

Our .Net app calls into a 3rd-party .Net library to obtain the value of a
certain property. For whatever reason, sometimes when we access this
property it just 'hangs' (doesn't return in any reasonable timeframe). It
isn't absolutely essential that we have the value of this property; however
is *is* essential that we don't hang our app while waiting on this @#%!
property to return.

I'd like to create a delegate and invoke this property asynchronously (e.g.
BeginInvoke), so that we can move on if the property doesn't return in a
reasonable timeframe.

Bill
"Paul E Collins" <fi******************@CL4.org> wrote in message
news:dc**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
"Bill Davidson" <Ra************@newsgroup.nospam> wrote:
I'd like to create a delegate for a property (as
opposed to a method). Can I do this?


Delegates only work for methods, *but* if you don't mind losing some
run-time type safety, you can achieve what you want using reflection. The
PropertyInfo class represents a property, and its SetValue method will
change the value.

What makes you want this feature? There might be a neater solution.

P.

Nov 17 '05 #3
Hi Bill,

According to C# language specification, The declaration of a delegate type
establishes a contract that specifies the signature of one or more methods.
A delegate is an instance of a delegate type, and references one or more of
the following:

1. A target object that is not a null reference (Nothing in Visual Basic)
and an instance method of the target object
2. A static method

So property cannot be used. I suggest you contact the library vendor to
resolved the blocking issue instead of calling it using a delegate.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #4

"Bill Davidson" <Ra************@newsgroup.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Paul:

Our .Net app calls into a 3rd-party .Net library to obtain the value of a
certain property. For whatever reason, sometimes when we access this
property it just 'hangs' (doesn't return in any reasonable timeframe). It
isn't absolutely essential that we have the value of this property;
however is *is* essential that we don't hang our app while waiting on this
@#%! property to return.

I'd like to create a delegate and invoke this property asynchronously
(e.g. BeginInvoke), so that we can move on if the property doesn't return
in a reasonable timeframe.


Wouldn't the simple answer just be to write a short method that sets the
value and invoke *that* method asynchronously?
Nov 17 '05 #5


"Daniel O'Connell [C# MVP]" wrote:

"Bill Davidson" <Ra************@newsgroup.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Paul:

Our .Net app calls into a 3rd-party .Net library to obtain the value of a
certain property. For whatever reason, sometimes when we access this
property it just 'hangs' (doesn't return in any reasonable timeframe). It
isn't absolutely essential that we have the value of this property;
however is *is* essential that we don't hang our app while waiting on this
@#%! property to return.

I'd like to create a delegate and invoke this property asynchronously
(e.g. BeginInvoke), so that we can move on if the property doesn't return
in a reasonable timeframe.


Wouldn't the simple answer just be to write a short method that sets the
value and invoke *that* method asynchronously?


my choice will also be what Daniel has suggested. I think this approach is
much cleaner and simple.

--
Cheers,
Rahul Anand

Nov 17 '05 #6
Rahul (and Daniel):

I like Daniel's idea as well. 'Should work like a champ. Thanks Daniel
and to all of those who replied.

Happy Coding,
Bill

"Rahul Anand" <Ra********@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...


"Daniel O'Connell [C# MVP]" wrote:

"Bill Davidson" <Ra************@newsgroup.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> Paul:
>
> Our .Net app calls into a 3rd-party .Net library to obtain the value of
> a
> certain property. For whatever reason, sometimes when we access this
> property it just 'hangs' (doesn't return in any reasonable timeframe).
> It
> isn't absolutely essential that we have the value of this property;
> however is *is* essential that we don't hang our app while waiting on
> this
> @#%! property to return.
>
> I'd like to create a delegate and invoke this property asynchronously
> (e.g. BeginInvoke), so that we can move on if the property doesn't
> return
> in a reasonable timeframe.


Wouldn't the simple answer just be to write a short method that sets the
value and invoke *that* method asynchronously?


my choice will also be what Daniel has suggested. I think this approach is
much cleaner and simple.

--
Cheers,
Rahul Anand

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Rohan Hattangdi | last post by:
I get the following error message on a line that attempts to create an instance of an ADODB connection object. "Object does not support this property or method: 'Server.CreateObject' ". Now...
3
by: Matt | last post by:
I tried to display all html control types in the form. But it has run time error "object doesn't support this property or method" on document.write(obj.type); Even I do document.write('hello...
2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
3
by: Clinton Pierce | last post by:
I can create a delegate like this, and everything works fine: class Foo { private delegate void NextPanel(); private NextPanel myself; // And later in a method private void EffStart() {
0
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops...
1
by: Nate | last post by:
I have my own data type, and I want to create a property page (dialog) for it. Basically, I want to display my object using the PropertyGrid control. If I have a variable of type "font", then...
7
by: stellstarin | last post by:
hi all, I have a HTML page with two forms. While trying to add a hidden element in a first form by the default javascript function, the error message 'object does not support this property or...
5
by: John Olbert | last post by:
Subject: Error is Object doesn't support this property or method I am trying to pass a C# string under Vs2005 (Net2) to an Vb6 ActiveX Control. I get the following runtime error-- "Object...
2
by: Charles | last post by:
I have a validation script used before submitting a form. When executed it says "Object doesn't support property or method". I'm using onclick="return validate();" which should be fine. But when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.