chengdu.jack
chengdu.jack

Reputation: 29

c# to split Dynamic arrays

today I account a request like this.

I define five address parameters in one class ,but five address maybe empty or have value.

five address store one field in Database ,like this (email1;email2;....)

when to store DB ,i need get five textbox in page to popludate address add ":", this is ok.

then question is that ,when I get data from database and show value in five textbox in page.

if address have value ,then show page with five textbox. no data ,show empty.

So, how to parse the dynamic email list to five textbox.

string [] emailList = sourceEmail.split(‘;’);
    foreach(string email in emailList)
    {
    this.email0 =?
    this.email1=?
    this.email2=?
    .......
    }

I know this does not work, because we don't know the emailList have how many value.

I know one way can work ,but seems that it is not good.

string [] emailList =sourceEmail.Split(';');
if(emailList.length ==1)
{
this.email0 = emailList[0];
}
if(emailList.length==2)
{
this.email0 = emailList[0];
this.email1 = emailList[1];
}

if(emailList.length==3)
{
this.email0 = emailList[0];
this.email1 = emailList[1];
this.email2 = emailList[2];
}

I don't think this is a good way. can somebody help me with this?

Upvotes: 1

Views: 2160

Answers (4)

Abhitalks
Abhitalks

Reputation: 28387

You may want to make use of the ControlCollection to identify the textboxes.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection(v=vs.110).aspx

So, you can find the control of your choice (in your case the textboxes named emailx) by using the key. Append the index of the loop to complete the key name in the loop. And you are done. And you won't have to make any changes to the existing code.

string[] emailList = sourceEmail.split(';');

for (int i = 0; i < emailList.Length; i++) {
    this.Controls("email" + i).Text = emailList[i];
}

Upvotes: 1

Patrick
Patrick

Reputation: 681

Add the TextBoxes in a List on FormLoad or somewhere else.

    void OnFormLoad(Object sender, EventArgs e)
    {
        List<TextBox> texboxesWithEmail = new List<TextBox> { textBox1, textBox2, textBox3, textBox4, textBox5 };
        string[] emails = new string[] { "[email protected]", "...." };
        PutEmailsInTexboxes(emails, texboxesWithEmail);
    }
    void PutEmailsInTexboxes(string[] emails, List<TextBox> textBoxes)
    {
        if (emails.Count() != textBoxes.Count) return;
        for (var i = 0; i < emails.Count(); i++)
        {
            textBoxes[i].Text = emails[i];
        }
    }

Upvotes: 2

S_F
S_F

Reputation: 887

I believe the main problem is that you have those five addresses as separate fields/properites in your class. This way you cannot flexibly set e.g. only three of them without either lengthy if blocks like you posted or resorting to reflection which is a ridiculous idea for a simple case like yours.

I'd suggest storing those emails in a List instead. This way you can have a collection with a flexible length and can use for or while loops easily without resorting to switches. Change your email0, email, email2 etc. to a single List<string> emails. Then you can just do:

for (int i = 0; i < emailList.Length; i++)
{
    this.emails[i] = emailList[i];
}

If you can't or don't want to change your class, then AstralisSomnium's or Sriram Sakthivel's solution would be good.

Upvotes: 1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73452

How about just a loop?

string [] emailList = sourceEmail.Split(';');

for (int i = 0; i < emailList.Length; i++)
{
    switch (i)
    {
        case 0:
            this.email0 = emailList[i];
            break;
        case 1:
            this.email1 = emailList[i];
            break;
       ....//Do it for all variables
    }
}

You can do it better with reflection, but that comes with its own expense.

Upvotes: 1

Related Questions