Checking Validity of uninitialized object

  • Thread starter Thread starter lavu
  • Start date Start date
L

lavu

I have a struct that I am creating an object for but not initializing
it. Later on under certain conditions it gets
set to the values of another struct. However I have to check this
struct for an valid instance and sometimes it is
not a valid instance and gives an exception. Since I cannot use a null
for a value type how would I check for the
validity of the object.

For example:

one.cs contains
------------------------
in a loop :
{
myObject firstInstance;

if firstInstance is valid then
do...something.

setvals ();


}

second.cs contains:
------------------------------
setvals()
{
firstInstance = validInstance;


}
 
You need an "isValid" boolean or something like it in your struct. The
CLR guarantees that when your struct is first created all booleans will
be "false".

If you are using .NET 2.0 then you can get this same effect by using
the "?" suffix to your type, e.g.:

myStruct? firstInstance;

will create a "nullable" struct.
 
Back
Top