943,558 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2748
  • C# RSS
Jun 19th, 2009
0

A property or indexer may not be passed as an out or ref parameter

Expand Post »
why cant i pass property as ref parameter?
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jun 19th, 2009
0

Re: A property or indexer may not be passed as an out or ref parameter

Narue if you see this thread please help, you usually know all the logical reasons behind programming concepts.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jun 19th, 2009
0

Re: A property or indexer may not be passed as an out or ref parameter

Because ref uses the address of the variable. Imagine a property like this:

c# Syntax (Toggle Plain Text)
  1. private bool connected;
  2. private string _field1;
  3. private string _field2;
  4. public string Field
  5. {
  6. get
  7. {
  8. if (connected)
  9. return _field1;
  10. else
  11. return _field2;
  12. }
  13. set
  14. {
  15. if (connected)
  16. _field1 = value;
  17. else
  18. _field2 = value;
  19. }
  20. }

Which underlying string field should "out" reference?

That should explain it all. This example is a little absurd as far as behavior but it is not uncommon for similar properties to be implemented.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 19th, 2009
0

Re: A property or indexer may not be passed as an out or ref parameter

Narue if you see this thread please help, you usually know all the logical reasons behind programming concepts.
It looks like you're asking for trouble...?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 19th, 2009
0

Re: A property or indexer may not be passed as an out or ref parameter

no, i really respect her programming skills..
thanks for your answer by the way, i will examine it when i get my work done.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jun 19th, 2009
0

Re: A property or indexer may not be passed as an out or ref parameter

I thought of an even better example

c# Syntax (Toggle Plain Text)
  1. public string FormText
  2. {
  3. get { return default(string); }
  4. set { }
  5. }

How do you propose the compiler would know what to do here
Last edited by sknake; Jun 19th, 2009 at 3:16 pm. Reason: typo
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 2nd, 2009
0

Re: A property or indexer may not be passed as an out or ref parameter

but Scott then tell me how you pass a function by reference(function pointer or delegate)?

Properties are almost the same thing with class methods except for snytax differences.

That is why i mentioned Narue's name in this thread, this is just something for her.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jul 2nd, 2009
0

Re: A property or indexer may not be passed as an out or ref parameter

In this case passing a function reference is more accurate for a property than the out parameter. A property is basically the same thing as a method except how you call it. A property can throw exceptions and have logic just like a method. Think of properties in the way they were implemented in java:
java Syntax (Toggle Plain Text)
  1. // Java getter & setter
  2. private String foo;
  3. public String getFoo() {
  4. return this.foo;
  5. }
  6. public void setFoo(String foo) {
  7. this.foo = foo;
  8. }
  9. // access the private variable foo like this:
  10. bar = obj.getFoo();

versus c#
c# Syntax (Toggle Plain Text)
  1. // C# public property
  2. private String _foo;
  3. public String Foo {
  4. get { return _foo; }
  5. set { _foo = value; }
  6. }
  7. // access the private variable _foo like this:
  8. bar = obj.Foo;
* Code borrowed from http://cephas.net/blog/2004/02/16/c-...getx-and-setx/
Its just a pain to call getters and setters like Java implemented them (according to most developers out there, and myself) so the CLR handles it differently.

A field cannot throw exceptions and is just in memory storage of a type. You can throw an exception while trying to set a field: (null as string).ToString() but not the field itself.

Take a look at a delegate:
c# Syntax (Toggle Plain Text)
  1. public delegate void OnOpenTableEventHandler(object sender, OpenTableEventArgs e);

That is just defining a signature for a method and by using delegates you can call code outside of your assembly that you have no knowledge of at design time. A delegate is basically an 'interface' for a method -- it must implement these parameters (instead of a true interface enforcing any number of member definitions).

So think of Properties as methods and delegates as interfaces .. those two concept remain independant of field and writing with out .

out says -- write <datatype> in to memory at 0x00000
function reference -- execute method in memory at 0x00000

I don't know how else to explain it... I hope it is a little more clear
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Activesync API
Next Thread in C# Forum Timeline: how to detect previously installed components?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC