How to use a property with ref accessor

D

Dom

I did the following in the Form1:

private static DIctionary<string,object> m_Data;
private Dictionary <string,object> Data {get {return m_Data;} set
{m_Data=value;}}

private void Form1_Load()
{
bool Status = GetData (ref Data);
...
}

The compiler complained that I can't use a property with "ref", and I
think I know why -- it is using ref with the result of a function.
But what is the proper way?

I ended up with: bool Status = GetData (ref m_Data)
and then used "Data" elsewhere. This must come up often. Is there a
preferred way?

Dom
 
A

Arne Vajhøj

I did the following in the Form1:

private static DIctionary<string,object> m_Data;
private Dictionary<string,object> Data {get {return m_Data;} set
{m_Data=value;}}

private void Form1_Load()
{
bool Status = GetData (ref Data);
...
}

The compiler complained that I can't use a property with "ref", and I
think I know why -- it is using ref with the result of a function.
But what is the proper way?

I ended up with: bool Status = GetData (ref m_Data)
and then used "Data" elsewhere. This must come up often. Is there a
preferred way?

What you ended up with is OK.

Alternative:

Data = GetData();

and have GetData either throw an exception or return null
depending on how exceptional it is.

Arne
 
M

Marcel Müller

I did the following in the Form1:

private static DIctionary<string,object> m_Data;
private Dictionary<string,object> Data {get {return m_Data;} set
{m_Data=value;}}

private void Form1_Load()
{
bool Status = GetData (ref Data);
...
}

The compiler complained that I can't use a property with "ref", and I
think I know why -- it is using ref with the result of a function.

Basically yes.
But what is the proper way?

The C# language does not have any idea of references to properties, as
well as it does not handle references to data members.
I ended up with: bool Status = GetData (ref m_Data)
and then used "Data" elsewhere. This must come up often. Is there a
preferred way?

No.

Alternatively you can use a temporary.
var temp = Data;
try
{ bool Status = GetData(ref Data);
} finally
{ Data = temp;
}
The main advantage of the temporary is that it also works if you do not
have access to the corresponding fields.

The properties of .NET make the code look more pretty, but they are by
far not implemented in all combinations with other language features.

If you really need references to properties - I mean not only to keep a
ref parameter of a function happy - you need to deal with a struct of
two delegates, one for the getter and one for the setter. Unfortunately
these delegates can only be initialized by reflection. And reflections
with strings of member names foil refactoring and impact code quality.
The .NET way of column data binding by a string with the property name
is a good example of bad code. It would do well to be an open instance
reference to the property.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top