"Use of possibly unassigned field" compile error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris LaJoie

    "Use of possibly unassigned field" compile error

    Our company has been developing a program in C# for some time now, and we
    haven't had any problems with it, but just last night something cropped up
    that has me, and everyone else, stumped.

    I have a struct that contains several different types of data. This struct
    is used throuout the program. Now, when I compile, I get 6 errors, all of
    them "Use of possibly unassigned field 'awayTime'" or "Use of possibly
    unassigned field 'intlTime'". It is correct, it is possible that these
    fields aren't assigned, but I don't care. It's inside of a struct, and
    everything inside of a struct gets assigned its default value anyway.

    This is difficult to explain, but if anyone else has had this problem and
    fixed it, i'd love to know how. Thanks.

    Chris LaJoie


  • Jon Skeet

    #2
    Re: "Use of possibly unassigned field" compile error

    Chris LaJoie <chris@etriptra der.com> wrote:[color=blue]
    > Our company has been developing a program in C# for some time now, and we
    > haven't had any problems with it, but just last night something cropped up
    > that has me, and everyone else, stumped.
    >
    > I have a struct that contains several different types of data. This struct
    > is used throuout the program. Now, when I compile, I get 6 errors, all of
    > them "Use of possibly unassigned field 'awayTime'" or "Use of possibly
    > unassigned field 'intlTime'". It is correct, it is possible that these
    > fields aren't assigned, but I don't care. It's inside of a struct, and
    > everything inside of a struct gets assigned its default value anyway.[/color]

    Local variables *don't* have default values, according to the C# spec.
    That's what you're running into.
    [color=blue]
    > This is difficult to explain, but if anyone else has had this problem and
    > fixed it, i'd love to know how. Thanks.[/color]

    The easiest way to fix it is to change:

    MyStruct foo;

    to

    MyStruct foo = new MyStruct();

    in your code.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Chris LaJoie

      #3
      Re: &quot;Use of possibly unassigned field&quot; compile error

      The struct var was being used inside of a foreach loop. Just for kicks (a
      hunch) I changed the foreach to a for, looping through the array. This
      solved everything.

      Simply using bla = new StructName() would not have worked in this case
      (because the values were being read from, not assigned to).

      Chris LaJoie

      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.19d11a e78d769bf39896b e@news.microsof t.com...[color=blue]
      > Chris LaJoie <chris@etriptra der.com> wrote:[color=green]
      > > Our company has been developing a program in C# for some time now, and[/color][/color]
      we[color=blue][color=green]
      > > haven't had any problems with it, but just last night something cropped[/color][/color]
      up[color=blue][color=green]
      > > that has me, and everyone else, stumped.
      > >
      > > I have a struct that contains several different types of data. This[/color][/color]
      struct[color=blue][color=green]
      > > is used throuout the program. Now, when I compile, I get 6 errors, all[/color][/color]
      of[color=blue][color=green]
      > > them "Use of possibly unassigned field 'awayTime'" or "Use of possibly
      > > unassigned field 'intlTime'". It is correct, it is possible that these
      > > fields aren't assigned, but I don't care. It's inside of a struct, and
      > > everything inside of a struct gets assigned its default value anyway.[/color]
      >
      > Local variables *don't* have default values, according to the C# spec.
      > That's what you're running into.
      >[color=green]
      > > This is difficult to explain, but if anyone else has had this problem[/color][/color]
      and[color=blue][color=green]
      > > fixed it, i'd love to know how. Thanks.[/color]
      >
      > The easiest way to fix it is to change:
      >
      > MyStruct foo;
      >
      > to
      >
      > MyStruct foo = new MyStruct();
      >
      > in your code.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Chris LaJoie

        #4
        Re: &quot;Use of possibly unassigned field&quot; compile error

        will post some sample code when I get home later today. Stay tuned!

        Chris LaJoie

        "Jon Skeet" <skeet@pobox.co m> wrote in message
        news:MPG.19d123 2a9f5d6f5e9896b f@news.microsof t.com...[color=blue]
        > Chris LaJoie <chris@etriptra der.com> wrote:[color=green]
        > > The struct var was being used inside of a foreach loop. Just for kicks[/color][/color]
        (a[color=blue][color=green]
        > > hunch) I changed the foreach to a for, looping through the array. This
        > > solved everything.[/color]
        >
        > Interesting - could you post some sample code? (Just enough to show
        > what's going on.)
        >[color=green]
        > > Simply using bla = new StructName() would not have worked in this case
        > > (because the values were being read from, not assigned to).[/color]
        >
        > I'm not sure I follow you, to be honest... if you're assuming that the
        > declaration gave you the default values, then assigning the default
        > value to it at the point of declaration should give you *exactly* the
        > same behaviour, but without the compile error.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Chris LaJoie

          #5
          Re: &quot;Use of possibly unassigned field&quot; compile error

          as I said earlier, my problem has been solved, but just to clear some things
          up I'll post some sample code. I have my struct:

          struct TestStruct {
          public int a;
          public int b;
          }

          now I have a function that only assigns the values of the struct. In my
          case, this function did not get any compile errors:

          public TestStruct[] Parse() {
          TestStruct[] ret = new TestStruct[10];
          for(int x = 0; x < ret.Length; x++) {
          ret[x].a = 1;
          ret[x].b = 2;
          }
          return ret;
          }

          as you can see, that function doesn't do any reading of the values, only
          assigns them. I have another function, which reads the data and inserts it
          in our database.

          public bool InsertInDB(Test Struct[] things) {
          StringBuilder sql = new StringBuilder(" INSERT....");
          foreach(TestStr uct t in things) {
          sql.Append(sqli fy(t.a) + ", "); // <-- compile error here
          sql.Append(sqli fy(t.b) + ")"); // <-- compile error here

          [...insert in db...etc...]
          }
          }

          solving this was a matter of simply replacing the foreach with a for. I'm
          not sure exactly what the difference is in the IL code generated, but
          clearly there is one. here's how i fixed it:

          public bool InsertInDB(Test Struct[] things) {
          StringBuilder sql = new StringBuilder(" INSERT....");
          for(int x = 0; x < things.Length; x++) {
          sql.Append(sqli fy(things[x].a) + ", "); // <-- compile error here
          sql.Append(sqli fy(things[x].b) + ")"); // <-- compile error here

          [...insert in db...etc...]
          }
          }

          I appologize for using such silly examples, but I'm not allowed to post
          company code.


          Chris LaJoie


          "Jon Skeet" <skeet@pobox.co m> wrote in message
          news:MPG.19d123 2a9f5d6f5e9896b f@news.microsof t.com...[color=blue]
          > Chris LaJoie <chris@etriptra der.com> wrote:[color=green]
          > > The struct var was being used inside of a foreach loop. Just for kicks[/color][/color]
          (a[color=blue][color=green]
          > > hunch) I changed the foreach to a for, looping through the array. This
          > > solved everything.[/color]
          >
          > Interesting - could you post some sample code? (Just enough to show
          > what's going on.)
          >[color=green]
          > > Simply using bla = new StructName() would not have worked in this case
          > > (because the values were being read from, not assigned to).[/color]
          >
          > I'm not sure I follow you, to be honest... if you're assuming that the
          > declaration gave you the default values, then assigning the default
          > value to it at the point of declaration should give you *exactly* the
          > same behaviour, but without the compile error.
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Jon Skeet

            #6
            Re: &quot;Use of possibly unassigned field&quot; compile error

            Chris LaJoie <chris@etriptra der.com> wrote:[color=blue]
            > as I said earlier, my problem has been solved, but just to clear some things
            > up I'll post some sample code. I have my struct:
            >
            > struct TestStruct {
            > public int a;
            > public int b;
            > }
            >
            > now I have a function that only assigns the values of the struct. In my
            > case, this function did not get any compile errors:
            >
            > public TestStruct[] Parse() {
            > TestStruct[] ret = new TestStruct[10];
            > for(int x = 0; x < ret.Length; x++) {
            > ret[x].a = 1;
            > ret[x].b = 2;
            > }
            > return ret;
            > }[/color]

            Right - just assigning will never have any problem.
            [color=blue]
            > as you can see, that function doesn't do any reading of the values, only
            > assigns them. I have another function, which reads the data and inserts it
            > in our database.
            >
            > public bool InsertInDB(Test Struct[] things) {
            > StringBuilder sql = new StringBuilder(" INSERT....");
            > foreach(TestStr uct t in things) {
            > sql.Append(sqli fy(t.a) + ", "); // <-- compile error here
            > sql.Append(sqli fy(t.b) + ")"); // <-- compile error here
            >
            > [...insert in db...etc...]
            > }
            > }[/color]

            That *is* strange. I don't get a compile-time error with this, for
            instance:

            using System;

            struct Foo
            {
            public int x;
            }

            class Test
            {
            static void Main(string[] args)
            {
            Foo[] f = new Foo[5];
            f[0].x=5;
            Bar (f);
            }

            static void Bar (Foo[] x)
            {
            foreach (Foo y in x)
            {
            Console.WriteLi ne (y.x);
            }
            }
            }

            <snip>
            [color=blue]
            > I appologize for using such silly examples, but I'm not allowed to post
            > company code.[/color]

            That's not a problem at all - but I'm still finding it hard to see why
            you're getting an error in the first place. Could you try compiling my
            code above? It should compile without warning. If it does, could you
            try constructing a similar complete test case which *doesn't* compile?

            Which version of Visual Studio (or csc) are you using?

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Chris LaJoie

              #7
              Re: &quot;Use of possibly unassigned field&quot; compile error

              I'm using VS.NET 2003. If I tried to create an example program that didn't
              compile, I wouldn't be able to. I honestly believe that this is a problem
              with the C# compiler. These problems crop up in code that HAS worked for
              months. Usually silly things like changing a foreach to a for will fix it.
              For example, I fixed one a couple weeks ago. Only one member of the struct
              was causing compile errors, so I changed the code around inside the struct.
              Here's how I did it.

              struct TestStruct {
              public int a; // <-- still works fine
              //public int b; // <-- broken
              private int m_b;
              public int b {
              get {return m_b;}
              set {m_b = value;}
              }
              }

              that's right, simply changing a public variable to a property fixed the
              problem.

              I've researched the error (Use of possibly unassigned field) thoroughly.
              Google only came up with documentation, not anyone else with problems.
              Something I haven't tried is to compile it on another computer when it does
              not compile on mine.

              I do appreciate your interest in this, but I'm afraid that in the end we
              won't be any closer to figuring it out.

              Chris LaJoie

              "Jon Skeet" <skeet@pobox.co m> wrote in message
              news:MPG.19d219 0bcd2c97d79896c 5@news.microsof t.com...[color=blue]
              > Chris LaJoie <chris@etriptra der.com> wrote:[color=green]
              > > as I said earlier, my problem has been solved, but just to clear some[/color][/color]
              things[color=blue][color=green]
              > > up I'll post some sample code. I have my struct:
              > >
              > > struct TestStruct {
              > > public int a;
              > > public int b;
              > > }
              > >
              > > now I have a function that only assigns the values of the struct. In my
              > > case, this function did not get any compile errors:
              > >
              > > public TestStruct[] Parse() {
              > > TestStruct[] ret = new TestStruct[10];
              > > for(int x = 0; x < ret.Length; x++) {
              > > ret[x].a = 1;
              > > ret[x].b = 2;
              > > }
              > > return ret;
              > > }[/color]
              >
              > Right - just assigning will never have any problem.
              >[color=green]
              > > as you can see, that function doesn't do any reading of the values, only
              > > assigns them. I have another function, which reads the data and inserts[/color][/color]
              it[color=blue][color=green]
              > > in our database.
              > >
              > > public bool InsertInDB(Test Struct[] things) {
              > > StringBuilder sql = new StringBuilder(" INSERT....");
              > > foreach(TestStr uct t in things) {
              > > sql.Append(sqli fy(t.a) + ", "); // <-- compile error here
              > > sql.Append(sqli fy(t.b) + ")"); // <-- compile error here
              > >
              > > [...insert in db...etc...]
              > > }
              > > }[/color]
              >
              > That *is* strange. I don't get a compile-time error with this, for
              > instance:
              >
              > using System;
              >
              > struct Foo
              > {
              > public int x;
              > }
              >
              > class Test
              > {
              > static void Main(string[] args)
              > {
              > Foo[] f = new Foo[5];
              > f[0].x=5;
              > Bar (f);
              > }
              >
              > static void Bar (Foo[] x)
              > {
              > foreach (Foo y in x)
              > {
              > Console.WriteLi ne (y.x);
              > }
              > }
              > }
              >
              > <snip>
              >[color=green]
              > > I appologize for using such silly examples, but I'm not allowed to post
              > > company code.[/color]
              >
              > That's not a problem at all - but I'm still finding it hard to see why
              > you're getting an error in the first place. Could you try compiling my
              > code above? It should compile without warning. If it does, could you
              > try constructing a similar complete test case which *doesn't* compile?
              >
              > Which version of Visual Studio (or csc) are you using?
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet

                #8
                Re: &quot;Use of possibly unassigned field&quot; compile error

                Chris LaJoie <chris@etriptra der.com> wrote:[color=blue]
                > I'm using VS.NET 2003. If I tried to create an example program that didn't
                > compile, I wouldn't be able to. I honestly believe that this is a problem
                > with the C# compiler.[/color]

                Yes, it sounds like it. Certainly the code you posted looked like it
                was fine.
                [color=blue]
                > These problems crop up in code that HAS worked for
                > months. Usually silly things like changing a foreach to a for will fix it.
                > For example, I fixed one a couple weeks ago. Only one member of the struct
                > was causing compile errors, so I changed the code around inside the struct.[/color]

                <snip>

                That sounds very strange.
                [color=blue]
                > Something I haven't tried is to compile it on another computer when it does
                > not compile on mine.[/color]

                That's definitely worth doing. I can't imagine how your VS.NET
                installation could be damaged in such a subtle way, but it's definitely
                worth a try.
                [color=blue]
                > I do appreciate your interest in this, but I'm afraid that in the end we
                > won't be any closer to figuring it out.[/color]

                You could well be right :(

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Grant Richins [MS]

                  #9
                  Re: &quot;Use of possibly unassigned field&quot; compile error

                  One question I have it if you're using incremental compilation.

                  --
                  --Grant
                  This posting is provided "AS IS" with no warranties, and confers no rights.


                  Comment

                  • Chris LaJoie

                    #10
                    Re: &quot;Use of possibly unassigned field&quot; compile error

                    No, none of my projects use incremental build.

                    Chris LaJoie

                    "Grant Richins [MS]" <grantri@online .microsoft.com> wrote in message
                    news:e2GyGCXfDH A.1200@TK2MSFTN GP09.phx.gbl...[color=blue]
                    > One question I have it if you're using incremental compilation.
                    >
                    > --
                    > --Grant
                    > This posting is provided "AS IS" with no warranties, and confers no[/color]
                    rights.[color=blue]
                    >
                    >[/color]


                    Comment

                    • Grant Richins [MS]

                      #11
                      Re: &quot;Use of possibly unassigned field&quot; compile error

                      Ah Hah!

                      As it turns out this is a known bug in the 7.0 and 7.1 C# compiler. It is
                      caused by having semi-large structs or just a large number of structs as
                      locals. There really is no good work-around other than to try and change
                      the method to decrease the number of structs, or other locals. Sometimes
                      even increasing them will also work. This has been fixed in current builds.
                      Those of you lucky enough to get bits from PDC can verify the fix if you
                      feel like it. I am working with the 'powers that be' to see if this fix can
                      also get pushed out as part of a future service pack or something, but no
                      promises!

                      --
                      --Grant
                      This posting is provided "AS IS" with no warranties, and confers no rights.


                      Comment

                      Working...