Undefined value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how to tell if a structure exists in an expression? If I
have a structure

struct hits {
uint numhits,maxhits;
char ch;
}

public void test( )
{
hits result, oldresult;

if (result) // in C/C++ I can test if result exists.
oldresult = result;
}

How do I find out if result has been set or not if the value is not defined?
 
Value types (including structs) cannot be null, which is what I assume
you mean by "not defined" or not existing. When you declare a value
type variable, it is automatically initialized.


Joshua Flanagan
http://flimflan.com/blog
 
if (result) // in C/C++ I can test if result exists.
oldresult = result;

this is equivalent of
if (result != 0)
oldresult = result;

in C#
Value types are automatically assigned the default value (0 in case of int)

Mimi said:
Does anyone know how to tell if a structure exists in an expression? If I
have a structure

struct hits {
uint numhits,maxhits;
char ch;
}

public void test( )
{
hits result, oldresult;

if (result) // in C/C++ I can test if result exists.
oldresult = result;
}

How do I find out if result has been set or not if the value is not
defined?
 
Mimi said:
Does anyone know how to tell if a structure exists in an expression? If I
have a structure

struct hits {
uint numhits,maxhits;
char ch;
}

public void test( )
{
hits result, oldresult;

if (result) // in C/C++ I can test if result exists.
oldresult = result;
}

How do I find out if result has been set or not if the value is not defined?

AFAIK this wouldn't work in C either. Your code is equivalent to:
if (result == 0)

which would have to cast a struct to a boolean in order to do the
comparison. My (embedded, but ASNSI-compliant) C compiler barfs at this
with the message "Scalar expression required".

From the C# programmer's reference:

"When you create a struct object using the new operator, it gets created
and the appropriate constructor is called. Unlike classes, structs can
be instantiated without using the new operator. If you do not use new,
the fields will remain unassigned and the object cannot be used until
all of the fields are initialized."


HTH,
-rick-
 
Back
Top