473,322 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

error CS0165 and structs

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
Nov 15 '05 #1
1 4280
makes perfect sense to me.

The compiler doesn't want to generate an error if there is a *Remote
Possibility* that your code is correct as coded. If you have public fields,
someone COULD create the object correctly and assign values to those fields
before any internal code is called (especially since your example doesn't
include a constructor).

On the other hand, if the fields are private, the compiler now *Knows* that
no outside code is initializing those fields... and therefore raises the
error. I suppose that you could say that the compiler should warn you in
the first case, but it doesn't. (Perhaps there's a flag somewhere that
tells it to... ;-o)

Both of your examples are bad code... the compiler can only be certain of
one of them.
Note that all error messages are specific to the class where the code is
declared.

Personally, I think the fact that you didn't call "new" in your static void
TestStruct() should probably have generated an error, but it doesn't. Alas,
the compiler didn't catch that you have a unassigned ref variable. no big
surprise there. Personally, I'm not used to compilers doing that much for
me anyway.

I hope this helps,
--- Nick

"John Aldrin" <jr******@yahoo.com> wrote in message
news:46********************************@4ax.com...
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

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Fahad Ashfaque | last post by:
qaHi, I am trying to assign value to a member of object of a struct, the object is treated under foreach. I got the error : error CS0170: Use of possibly unassigned field 'price' according...
1
by: Michael | last post by:
Hi all, I've written a COM-Dll (with ATL) that holds a database of marathon runners. I wrote a function that returns the needed information of all runners and it works fine in VB but not in...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
8
by: Pedro Pinto | last post by:
When compiling my program i got this error: Error: 'for' loop initial declaration used outside c99 mode What is it and how can i solve it? Thanks in advance! Regards
7
by: Ray Dillinger | last post by:
Hi. I'm having a problem and I really want to understand it. Here's the situation: I have an array of pointers, and each pointer is the head of a linked list of structs. The structs are...
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.