J
John Aldrin
Hi,
I'm confused about when compiler error "CS0165: Use of unassigned
local variable" should be generated. If I have a struct w/public
fields the error is not generated when the new operator is not used.
If there are no public fields the error is generated. Does this make
sense?
Ex 1 Public Fields
public struct TstStruct
{
public int x;
public int y;
public int X
{
get { return x;}
set { x = value;}
}
public int Y
{
get { return y;}
set { Y = value;}
}
};
// No compile errors
static void TestStruct()
{
TstStruct tstStruct; // new operator not used
tstStruct.x = 100;
tstStruct.y = 200;
tstStruct.X = 100;
tstStruct.Y = 200;
int aValue = tstStruct.X;
}
Ex 2 No Public Fields
public struct TstStruct
{
private int x;
private int y;
public int X
{
get { return x;}
set { x = value;}
}
public int Y
{
get { return y;}
set { Y = value;}
}
};
// Yes compile errors
static void TestStruct()
{
TstStruct tstStruct;
tstStruct.X = 100; // Compile error here
tstStruct.Y = 200;
int aValue = tstStruct.X;
}
Thanx
jra
I'm confused about when compiler error "CS0165: Use of unassigned
local variable" should be generated. If I have a struct w/public
fields the error is not generated when the new operator is not used.
If there are no public fields the error is generated. Does this make
sense?
Ex 1 Public Fields
public struct TstStruct
{
public int x;
public int y;
public int X
{
get { return x;}
set { x = value;}
}
public int Y
{
get { return y;}
set { Y = value;}
}
};
// No compile errors
static void TestStruct()
{
TstStruct tstStruct; // new operator not used
tstStruct.x = 100;
tstStruct.y = 200;
tstStruct.X = 100;
tstStruct.Y = 200;
int aValue = tstStruct.X;
}
Ex 2 No Public Fields
public struct TstStruct
{
private int x;
private int y;
public int X
{
get { return x;}
set { x = value;}
}
public int Y
{
get { return y;}
set { Y = value;}
}
};
// Yes compile errors
static void TestStruct()
{
TstStruct tstStruct;
tstStruct.X = 100; // Compile error here
tstStruct.Y = 200;
int aValue = tstStruct.X;
}
Thanx
jra