[351] - Local Delegate Vairable Not Initialised Error

The following code give an error that the local variable 'harvest' is not
initialised. Am I missing something or is this an incorrect error?

public void Foo()
{
List result = new List(); Action]]> harvest =
delegate(Control obj)
{
if (Bar(obj))
{
result.Add(obj);
}
else
{
foreach (Control child in obj.Controls)
{
// Local variable 'harvest' might not be initialised
before accessing.
harvest(child);
}
}
};

harvest(this);
}


Thanks

Sean


5 comments
Comment actions Permalink

I believe the problem is that the variable harvest is not initialized
until after the delegate is created and the delegate refences harvest.

It's not just R# thats complaining, the code will not compile either.

I think the following code does the same thing and works:

public void Foo() {
List result = new List(); Action> harvest = Harvest; harvest(result); } private void Harvest(List result) { if (Bar()) { result.Add(this); } else { foreach (Control child in Controls) { child.Harvest(result); } } } I had change the semantics a bit for this to work. The Action now takes a result argument instead of a Control since the delegate is a class method and already has access to the Control object via the this pointer. It's also possible to maintain the semantics of an Action but requires a bit more trickery to get access to the result object. One way would be to create a class that derives from List (the type of the result) and make the delegate a method of that class. In article <4f7eb0ed10ea68c92997d91ce57c@news.jetbrains.com>]]>,
no@spam.org says...

The following code give an error that the local variable 'harvest' is not
initialised. Am I missing something or is this an incorrect error?

public void Foo()
{
List<Control> result = new List<Control>();
Action<Control> harvest =
delegate(Control obj)
{
if (Bar(obj))
{
result.Add(obj);
}
else
{
foreach (Control child in obj.Controls)
{
// Local variable 'harvest' might not be initialised
before accessing.
harvest(child);
}
}
};

harvest(this);
}


Thanks

Sean


0
Comment actions Permalink

Thanks Alon. I did end up refactoring, similar to your suggestion. It does
make sense that the error arises from the inside reference to delegate.
However, as this is contained inside the same delegate then it will always
be initialised. Maybe R# could detect recursive calls inside delegates and
not issue the warning?


0
Comment actions Permalink

NB: I've posted this to the r#.community group as the EAP group is pretty
quiet.


0
Comment actions Permalink

Hello sean,

Alon Albert gave you correct answer, this code doesn't compile, at least
by C# 2.0 compiler.
Also, variable is not initialized in its initialization expression. This
is pretty same as
int i = i +1;
What if your delegate is of the following type:

public delegate ActionDelegate ActionDelegate();

and you use it as follows:

ActionDelegate actionDelegate = new ActionDelegate(delegate { return actionDelegate;
})();

;) Yes, I know, it is pretty artificial example...

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


sk> NB: I've posted this to the r#.community group as the EAP group is
sk> pretty quiet.
sk>


0
Comment actions Permalink

Hi Ilya

That makes sense....I missed the part when Alon said it didn't compile....pretty
dumb of me not to notice that in both the IDE and Alon's post!!! I had focussed
on Alon's refactoring advise.

Thanks again

Sean


0

Please sign in to leave a comment.