No clue about arrays, everything fails ...  
Author Message
Guido den Broeder





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

I want to fill an array in the constructor of my object, and then use it in
in another method. But no matter what I try, I can't get this right. It is too different from how arrays used to be. Either it does not compile, or the array remains null as in the example below. How should I do it

class
Calendar

{
private static int day,month;
private static int[] day****onth;

public Calendar()
{
day=16;
day****onth[1] = 31;
day****onth[2] = 28;
day****onth[3] = 31; // does not work, day****onth remains null
// ...
}

public static AddDay()
{
day++;
if (day>day****onth[month])
{month++; day=1;}
// ...
}

Regards,

  Guido



Visual C#14  
 
 
Daniel Rieck





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

You need to initialize the array, just like any other class:

private static int[] day****onth = new int[12];


Note that the array index will start at 0.

PS: There're ready to use Calendar classes in the framework (in the System.Globalization namespace), and the DateTime class has a static method to calculate the number of days in any given month (it also requires a year argument to take leap years into account).


 
 
Guido den Broeder





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

Thanks. Smile

I will be changing the number of months, days etc. since it is a different world, so I can't use any Earth-based calendars.

This also means that specifying the length of the array in the declaration is something I would rather not do, since we could have a year with many more months than 12. The specifics will eventually be read from a file.

Regards,

  Guido


 
 
CRitchie





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

If your arrays need to be that dynamic you might want to look at the ArrayList.

 
 
Guido den Broeder





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

Yes, that is what I was looking for, I think.
I didn't find it at first because it isn't called ArrayList but simply List.

Regards,

  Guido

 
 
CRitchie





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

ArrayList is in the System.Collections namespace.  "using" this namespace will allow you access to the ArrayList class.

 
 
BillWert - MSFT





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

"The specifics will eventually be read from a file."

You can initalize the array size with a variable.  IE:


 class Tmp
 class Tmp
 {
  public string[] m_str;
  public Tmp(string thesize)
  {
   int size = Int32.Parse(thesize);
   m_str = new string[size];
   for(int i = 0; i < size; i++)
   {
    m_str[ i ] = i.ToString();
   }
  }

 }
 




 
 
Muggins





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

re: m_str = new string [size]

doesn't this come up with a compilation error "A constant value is expected"


 
 
Matthew Watson





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

Not in C#. It would in C++.

 
 
Muggins





PostPosted: Visual C# Language, No clue about arrays, everything fails ... Top

thanks Matthew, I'd just had a mental block about how to define the array size this afternoon...bizarre.