views:

94

answers:

6

OK, I've been messing around with this for ages and I'm not getting any closer. My current version is as below. The comments are what I think I'm doing. The semantic is basically an index number (like a house number) and a list of attributes in an array. Then create an array 'street'. I want to be able to update the values of all elements in the current scope. The class is defined as high as possible so as to make the scope global. My ossified 'C' brain doesn't really understand things like lists and IEnumerable so I haven't tried to go that route. The code parser in the editor makes a bit of a mess of this - sorry.

public class house 
{

      // Ok, looking at this from the world of 'C' and thinking 'struct' like,
      // I put my variables here.

      public int my_id;
      public long [] pl_id;

      public house()
      {
         // I try to initialise the starting values, so I can carry out some tests later.
         my_id = 0;
         pl_id = new long[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
      }
  }

// I attempt to inform the compiler that I will be wanting an array of the house objects
// and call that array 'new_house'. Again, the code editor isn't keen.

  house [] new_house;

    private void button1_Click(object sender, EventArgs e)
    {
      // In the programs main routine (not *main*!), I then try to get the
      // array 'new_house' populated with initialised 'house' objects 
      for (int idx = 0; idx < 10; idx++)
       {
          new_house[idx] = new house();
       }

       // And at some point in the future I wish to set or update the values arbitrarily. eg:
        new_house[7].my_id = 123;
        new_house[7].pl_id = 345678;
        // any combination of attributes and id numbers is possible, so I use zero simply to see if they have been set, with -1 indicating failure / an absence of data-
    }
}

Right. As I say, I've tried a lot of different ways to do this, and the main problem I am getting is that I never seem to correctly initialise the array 'new_house' and get null exceptions thrown when I try to assign anything. I can't believe something that seems so intuitively simple can be so hard to code, so where have I got it wrong (and I fully accept that there could be more than one conceptual or coding error in the above).

Comments on appropriateness of approach, and help with coding alike, gratefully accepted.

Colville

+1  A: 

The first obvious problem with your code is that your constructor doesn't have the same name as the class. It should be this:

public house()
{
   // ...
}

A second point is you don't need the constructor at all here:

public int my_id = 0;   // The "= 0" is actually not needed here either.
public long[] pl_id = new long[10];

I would also suggest that you don't use arrays for things like houses on a street because house numbers won't necessarily be sequential. You can have gaps and even multiple houses with the "numbers" 5A and 5B. A dictionary might be a better choice.

IDictionary<string, house> houses = new Dictionary<string, house>();

If you really want to have sequential numbering you might want to consider a List<house> instead of an array so that it can be easily extended if new houses are built.

Finally I'd advise using PascalCase for classes. It will make your code much easier to read if you use the same standards as the rest of the .NET framework.

Mark Byers
A: 

Change public game()

to public house()

Your constructor has to have the same name as the class.

Marcin
As above. Thanks
+1  A: 

You need to instantiate the array before initializing items of it:

house[] new_house = new house[10];

Replace 10 with desired number of items.

In case you don't know the number, use List:

List<house> new_house = new List<house>()

Then you can dynamically add items using new_house.Add(item) and access them in foreach loop or through index new_house[i]

František Žiačik
You guys are fast! I'm still editing it.
I tried that approach and was OK until I tried the actual assignment when I got a null exception from that. I like 'list' approach but haven't really understood it - my alternative being some kind of redimensioning of the array which just feels ugly.
Well, redimensioning is not only ugly, it's also bad because it would need to reallocate memory heavily. I'd recommend you to take a look at the List approach, it's not that hard, in fact it is pretty easy.
František Žiačik
A: 

A couple things:

  1. new_house is never initialized. You can't use it until you've initialized it.
  2. pl_id is an array, but you attempt to store a long in it (345678) -- you could change it to new int[] { 345678}.
  3. You've got a method, game(), in the class house which looks and acts like a constructor. You would have to name it house() if it is meant to be a constructor.
expedient
Thanks. Covered that in the last edit pass - I was trying to simplyfy the semantics out from the actual code.The array needs to be an array of longs. The call to the remore API returns longs and there is an array of them (actually 17) for each ID value I send. The sent value is an int.
A: 

not public game()

right: public house()

Always the constructor has to have the same name as the class.

torun
A: 

Use List<T> for those collections. Try not to say you don't understand something because you are 'c' addicted. Try to say yourself you want to try something new and search for a good solution

namespace Myprog
{
// I attempt to inform the compiler that I will be wanting an array of the house objects
// and call that array 'new_house'

  List<house> houselist = new List<house>();

    private void button1_Click(object sender, EventArgs e)
    {
      // In the programs main routine (not *main*!), I then try to get the
      // array 'new_house' populated with initialised 'house' objects 
      for (int idx = 0; idx < 10; idx++)
       {
          houselist.add(new house());
       }

       // And at some point in the future I wish to set or update the values arbitrarily. eg:
        houselist[7].my_id = 123;
        // any combination of attributes and id numbers is possible, so I use zero simply to see if they have been set, with -1 indicating failure / an absence of data-
    }
  }
}
KroaX