| | |
A property or indexer may not be passed as an out or ref parameter
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
![]() |
Because ref uses the address of the variable. Imagine a property like this:
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.
c# Syntax (Toggle Plain Text)
private bool connected; private string _field1; private string _field2; public string Field { get { if (connected) return _field1; else return _field2; } set { if (connected) _field1 = value; else _field2 = value; } }
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.
I thought of an even better example 
How do you propose the compiler would know what to do here

c# Syntax (Toggle Plain Text)
public string FormText { get { return default(string); } set { } }
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
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 118
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.
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.
Due to lack of freedom of speech, i no longer post on this website.
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:
versus c#
* 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:
Take a look at a delegate:
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 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
java Syntax (Toggle Plain Text)
// Java getter & setter private String foo; public String getFoo() { return this.foo; } public void setFoo(String foo) { this.foo = foo; } // access the private variable foo like this: bar = obj.getFoo();
versus c#
c# Syntax (Toggle Plain Text)
// C# public property private String _foo; public String Foo { get { return _foo; } set { _foo = value; } } // access the private variable _foo like this: bar = obj.Foo;
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)
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
![]() |
Similar Threads
- Issue about passing parameter to Crystal Report 11.. (Visual Basic 4 / 5 / 6)
- New To C++, Need Some Help On The Follwing Program(call By Vallue And Reference? (C++)
- arrays passed to methods and keyword param (C#)
- passing parameter to Store Procedure in SqlDataAdapter (VB.NET)
- How to return more than one value from a method? (C#)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Command Line Parameter (C++)
- special keys as inputs (Game Development)
- Data Abstraction (Computer Science)
Other Threads in the C# Forum
- Previous Thread: Activesync API
- Next Thread: how to detect previously installed components?
| Thread Tools | Search this Thread |
.net access algorithm animation array bitmap box c# check checkbox client combobox control conversion csharp customactiondata database datagrid datagridview dataset datastructure date/time datetime datetimepicker degrees directrobot dll draganddrop drawing encryption enum excel file filename files form format forms function gdi+ gis gtk hash image input install java label list mandelbrot math mouseclick mp3 mysql native operator outlook2003 packaging path photoshop picturebox pixelinversion pixelminversion post print process programming radians regex remoting richtextbox safari server sleep snooze socket sql statistics string table tables tcp text textbox thread time timer treeview update usercontrol usercontrols validation visualstudio webbrowser webcam wfa wia winforms wpf xml






