By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,992 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,992 IT Pros & Developers. It's quick & easy.

Programmatically filling in an array

P: n/a
I am new to C#. I am trying to use a for loop of integers to fill up a String array. Actually, I am trying to get a dropdown list of all the days in the current month.

This is what I have been trying so far:

int i;
String[] day;
for (i = 1; i <= DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month); i++)
{
day = i;
}

Can someone please help me?

TIA
--------------= Posted using GrabIt =----------------
------= Binary Usenet downloading made easy =---------
-= Get GrabIt for free from http://www.shemes.com/ =-

May 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Two things.

First, you probably want to store the days in the current month in a
variable. I don't know whether the compiler is smart enough to extract
that operation from the loop and not calculate it every time. So, you
could say:

int daysThisMonth = DateTime.DaysInMonth(DateTime.Today.Year,
DateTime.Today.Month);

Second, you declared

string[] day;

but you didn't actually make an array of strings. Let me explain: the
line above creates a variable called "day" that sometime later may
refer to an array of strings. However, it doesn't actually contain
anything yet: it just contains a null reference. You have to make an
array of strings and then have "day" refer to that array. Usually that
is done like this:

string[] day = new string[daysThisMonth];

Now "day" will contain a reference to an array of (maybe 31) strings.
There's nothing in each element of the array yet, though: all of the
entries contain null references.

Inside your loop, you said:

day = i;

what this sort-of says is that you want to throw away your reference to
the 31 string array and instead point "day" to an integer held in "i".
Since integers and arrays aren't interchangeable, the compiler will
complain about this.

You want something more like this:

day[i - 1] = i;

which says that you want to set the "i - 1"th element in the array to
the integer i. Now, this still won't work, because integers and strings
aren't interchangeable in C#, but at least you're setting an element in
the array and not trying to clobber the entire array. Finally, to make
it compile, you need to replace the above line with this:

day[i - 1] = i.ToString();

which says that you want to convert the integer value in "i" into a
string, and then put a reference to that string in the "i - 1"th entry
in the array.

(Note that it's i -1 because in C# arrays start at index 0, not at
index 1 as they normally do in Visual Basic.)

May 17 '06 #2

P: n/a
gpg
Just a suggestion -

Instead of using day[i - 1] to refer to your array element, use the
following:

for (i = 0; i < daysThisMonth; ++i)
{
day[i] = i.ToString();
}

C / C++ / C# iterations are normally written this way since these
languages use 0 based arrays. Since this is a common construction, you
should be use to seeing / using it.

GPG

May 17 '06 #3

P: n/a
Sorry... I don't understand what you're proposing here. This will
result in an array with "0" stored in element 0, "1" in element 1, etc.
Since "0" isn't a valid day of the month, the 0 entry will therefore be
useless. Since you are also iterating just short of daysThisMonth, the
highest element in the array will be one day short of the end of the
month. This is NOT common practice in any of the C-based languages.

Did you mean to say that the loop should iterate from 0 to
daysThisMonth - 1 (as you wrote it)... that this is common practice?
Well, yes, I suppose that it is, but then the line inside your loop
would have to say:

day[i] = (i + 1).ToString();

May 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.