Microsoft .NET parser C# error ?
-
if (foo == null) System.Data.Foo _Foo = new System.Data.Foo; ∙ ^ Error ^ if (foo == null) { System.Data.Foo _Foo = new System.Data.Foo; } ∙ ^ No Error ^ Becouse?:eek:
You can't define a variable in an inline
if
statement. The correct way would be:System.Data.Foo _Foo=null;
if(foo==null)
_Foo = new System.Data.Foo;//or better yet:
System.Data.Foo _Foo=(foo==null ? new System.Data.Foo : null);**"Peace is not merely a distant goal that we seek, but a means by which we arrive at that goal." -- Martin Luther King Jr.
-
You can't define a variable in an inline
if
statement. The correct way would be:System.Data.Foo _Foo=null;
if(foo==null)
_Foo = new System.Data.Foo;//or better yet:
System.Data.Foo _Foo=(foo==null ? new System.Data.Foo : null);**"Peace is not merely a distant goal that we seek, but a means by which we arrive at that goal." -- Martin Luther King Jr.
If you use:
if (foo == null)
{
System.Data.Foo _Foo = new System.Data.Foo;
}Now
_Foo
is only available within the{
and}
. Now, if you could use the short version:if (foo == null)
System.Data.Foo _Foo = new System.Data.Foo;...then where would
_Foo
be available? In the{
and}
, which you left out, or in the parent's context (e.g. between the methods{
and}
)? Because of this confusion, it is not possible. -
You can't define a variable in an inline
if
statement. The correct way would be:System.Data.Foo _Foo=null;
if(foo==null)
_Foo = new System.Data.Foo;//or better yet:
System.Data.Foo _Foo=(foo==null ? new System.Data.Foo : null);**"Peace is not merely a distant goal that we seek, but a means by which we arrive at that goal." -- Martin Luther King Jr.
If you use:
if (foo == null)
{
System.Data.Foo _Foo = new System.Data.Foo;
}Now
_Foo
is only available within the{
and}
. Now, if you could use the short version:if (foo == null)
System.Data.Foo _Foo = new System.Data.Foo;...then where would
_Foo
be available? In the{
and}
, which you left out, or in the parent's context (e.g. between the methods{
and}
)? Because of this confusion, it is not possible. -
If you use:
if (foo == null)
{
System.Data.Foo _Foo = new System.Data.Foo;
}Now
_Foo
is only available within the{
and}
. Now, if you could use the short version:if (foo == null)
System.Data.Foo _Foo = new System.Data.Foo;...then where would
_Foo
be available? In the{
and}
, which you left out, or in the parent's context (e.g. between the methods{
and}
)? Because of this confusion, it is not possible.