- Kim Bach . Org - https://www.kimbach.org -

No _compiler_ detected errors – but shouldn’t I, at least, have received a warning?

One of the first compilers I worked with, VS-Pascal, had a very “HAL-like” confident behaviour, after it had finished a succesful compile, it wrote:

“No compiler detected errors”

I found that almost rude, but it was also something to keep in the back of your mind, and today I remembered this clearly.

For the last week or so I’ve had a mysterious ASP.NET/C# problem on the back-burner. I’m trying to populate a drop-down list on a webpage. The list is based on a list of documents. For that purpose I had an entity class with a constructor, in the Page_Load code of the webform, I’m initialising the drop-down with a desciption and an id from the document list.

The list was populated just fine, but when I did a post of the form, I was consistently told that the first item was selected, even if I selected item number 2, 3 or 4.

This was very frustrating, especially because I have two drop-downs, and the other was working just fine.

Today I couldn’t postpone the problem any longer, and I tried a number of things, including moving the drop-downs, and finally I started doing websearches for bug-reports, but it’s extremely unlikely that ASP.NET has such a fundamental bug, so I was clearly doing something wrong.

When you’re using frameworks like .NET, a lot is done behind the scenes, luckily I know HTML so I finally hit View/Source, and to my surprise all the option values in the select tag were set to 0, why was that, I knew that my list was initialised to values from 0-4, but it was consistent with the test-results.

I was clearly doing something wrong. Below is the C# source-code for the entity class, can you spot the problem?

public class ShredYearDocument
   {
       private int _shredYearId;

       public int ShredYearId
       {
           get { return _shredYearId; }
           set { _shredYearId = value; }
       }

       string _shredYearDescription;

       public string ShredYearDescription
       {
           get { return _shredYearDescription; }
           set { _shredYearDescription = value; }
       }

       public ShredYearDocument(int ShredYeadId, 
           string ShredYearDescription)
       {
           _shredYearId = ShredYearId;
           _shredYearDescription = ShredYearDescription;
       }
   }

I’ll give you a hint: there are no compiler detected errors, but I’d argue that the compiler should,at least, have generated a warning, because I have something that could be likened to unused, or uninitalised, local variables.

Another hint: Remember that I always had the id set to zero (0), no mater what.

But shouldn’t the compiler generate a warning here?