Re: Damn frustrating problem :-(

From: Peter N Roth (refusing.spam_at_mycompany.com)
Date: 12/08/04


Date: Tue, 7 Dec 2004 23:20:44 -0500


"thechaosengine" <sh856531@microsofts_free_email_service.com> wrote in
message news:uEZle9D3EHA.3908@TK2MSFTNGP12.phx.gbl...
>
>> Field initialization
>> The initial value of a field, whether it be a static field or an instance
>> field, is the default value (§5.2) of the field's type. It is not
>> possible to observe the value of a field before this default
>> initialization has occurred, and a field is thus never "uninitialized".

> I'm not really sure what to make of that. How come, if I dont initialise,
> by means of a constructor a string for example - its null until I give it
> something?

Let me chop your code down a little so I can see what's happening.

#region Using directives

using System;

#endregion

namespace TestInitialization {

      class Program {

            static void Main( string[] args )

            { // Role = null

                  Role currentRole = new Role( 1, "RoleName",
"RoleDescription" );

                  // Role = TestInitialization.Role

            }

      }

      class Role{

            private Int16 roleID = -1;

            private string roleName;

            private string roleDescription;

            private PermissionsCollection permissions;

            public Role( Int16 i, string n, string d ) // roleID
= -1, all others = null

            {

                  roleID = i;

                  roleName = n;

                  roleDescription = d;

            } // roleID = 1, rolename = n, roledescription = d,
permissions = null

      }

      class PermissionsCollection {} // to allow compilation

}

The comments indicate values at the points of execution.

Because you haven't initialized permissions, it remains null. C# knows it's
an object; the default value of an object is null, hence, permissions IS
null.

This is not the best way to construct an object; after construction, the
object is supposed to be 'complete'. So if permissions = null is not
complete, then you need to redesign that.

-- 
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET