#296 – You Must Assign a Value to All Fields Before Using a struct

Before you can reference a struct in your code, all of its fields must first be initialized.  If all fields have not been initialized, the compiler will issue an error.

In the example below, we assign only two of the three public fields of a Point3D struct.  The compiler complains with the error:

Use of unassigned local variable ‘pt’

        static void Main()
        {
            Point3D pt;
            pt.x = 5.0;
            pt.y = 5.0;
            DoSomethingWithPoint(pt);
        }

        static void DoSomethingWithPoint(Point3D pt)
        {
            Console.WriteLine("{0},{1},{2}", pt.x, pt.y, pt.z);
        }
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: