Cistoran
Cistoran

Reputation: 1597

Populate String Array With For Loop and String Combintion in C#

I have a string array defined as String[] sEmails; that I am trying to populate with 10 different (but similar) strings (email addresses in this case).

Here is the code I'm trying to use to populate the array.

public void populateEmailArray()
    {
        for (int x = 0; x < 10; x++)
        {
            switch(x)
            {
                case 1:
                    sEmails[x] = sRepStuff + "1" + sGmail; 
                break;
                case 2:
                    sEmails[x] = sRepStuff + "2" + sGmail;
                break;
                case 3:
                    sEmails[x] = sRepStuff + "3" + sGmail;
                break;
                case 4:
                    sEmails[x] = sRepStuff + "4" + sGmail;
                break;
                case 5:
                    sEmails[x] = sRepStuff + "5" + sGmail;
                break;
                case 6:
                    sEmails[x] = sRepStuff + "6" + sGmail;
                break;
                case 7:
                    sEmails[x] = sRepStuff + "7" + sGmail;
                break;
                case 8:
                    sEmails[x] = sRepStuff + "8" + sGmail;
                break;
                case 9:
                    sEmails[x] = sRepStuff + "9" + sGmail;
                break;
            }
        }
    }

The end result I want to be something like this

sEmails['[email protected]','[email protected]','[email protected]'] and so and and so forth to [email protected]

But on the first try of trying to set an sEmails[x] it gives me an error of "NullReferenceException was unhandled. Object reference not set to an instance of an object."

I have no idea what I'm doing wrong here because the code seems sound in my mind. Any help on this would be greatly appreciated.

Upvotes: 0

Views: 18718

Answers (4)

Dummy01
Dummy01

Reputation: 1995

To the solution that you already accepted I would add some "spice" to make it even more dynamic. I would not set 10 hard-coded but I would use array's length instead.

public void populateEmailArray()
{
    int length = sEmails.Length;

    for (int x = 0; x < length; x++)
    {

        sEmails[x] = sRepStuff + x + sGmail; 
    }
}

I don't trust my memory when I have to return to a program some time afterwards and have to remember and check all the points that I have to change when for example your emails array must grow up to 20 elements.

Upvotes: 0

Oleksi
Oleksi

Reputation: 13097

Arrays start indexing at 0, not 1, and so you aren't giving sEmails[0] a value. Shift all your values down by 1. Then when you access sEmails[0], it will still be null. You should also make sure that your sEmails array has been instantiated:

sEmails = new String[10];

This should work:

public void populateEmailArray()
{
       sEmails = new String[10];
        for (int x = 0; x < 10; x++)
        {
            switch(x)
            {
                case 0:
                    sEmails[x] = sRepStuff + "1" + sGmail; 
                break;
                case 1:
                    sEmails[x] = sRepStuff + "2" + sGmail;
                break;
                case 2:
                    sEmails[x] = sRepStuff + "3" + sGmail;
                break;
                case 3:
                    sEmails[x] = sRepStuff + "4" + sGmail;
                break;
                case 4:
                    sEmails[x] = sRepStuff + "5" + sGmail;
                break;
                case 5:
                    sEmails[x] = sRepStuff + "6" + sGmail;
                break;
                case 6:
                    sEmails[x] = sRepStuff + "7" + sGmail;
                break;
                case 7:
                    sEmails[x] = sRepStuff + "8" + sGmail;
                break;
                case 8:
                    sEmails[x] = sRepStuff + "9" + sGmail;
                break;
                case 9:
                    sEmails[x] = sRepStuff + "10" + sGmail;
                break;
            }
        }
    }

A better, more concise version would be:

for(int i = 0; i < 10 ; i++)
{
   sEmails[i]="repstuff"+(i+1)+"@gmail.com";
}

Upvotes: 0

Stefan H
Stefan H

Reputation: 6683

Try instantiating your array with

String[] sEmails = new String[10];

You can also make that loop far more succinct:

public void populateEmailArray()
{
    for (int x = 0; x < 10; x++)
    {

        sEmails[x] = sRepStuff + x + sGmail; 
    }
}

Upvotes: 4

Kevin
Kevin

Reputation: 3509

Hope you are doing well.

I will help you clean up your code today!

rather than doing a switch-case you can do this

for(int i = 0; i < 10 ; i++)
{
   emails[i]="repstuff"+i+"@gmail.com";
}

That helps you clear out a your coding style. In addition, did you check if you have instantiated / created both your sEmails, repStuff and Gmail?

Upvotes: 0

Related Questions