Sreenath Ganga
Sreenath Ganga

Reputation: 736

change the variable name inside a loop

I had a marquetool for my winform Application I obtained it from here

And I that control I can Add Textelement By using the below code

MarqueControl.Entity.TextElement textElement1 = 
    new MarqueControl.Entity.TextElement("TextElement 1");
MarqueControl.Entity.TextElement textElement2 = 
    new MarqueControl.Entity.TextElement("TextElement 2");
MarqueControl.Entity.TextElement textElement3 = 
    new MarqueControl.Entity.TextElement("TextElement 3");
superMarquee1.Elements.AddRange(new MarqueControl.Entity.TextElement[] 
               {
                   textElement1,
                   textElement2,
                   textElement3
               }

but the issue is I had to fill the textelement from datatable , I did it like this

   for(int i=0;i<dt.Rows.Count ;i++){
                String wholetext = dt.Rows[i][1].ToString() + "--" + dt.Rows[i][1].ToString();
            //    String textElement="textElement"+i.ToString();

                TextElement element = new TextElement(wholetext);

                MarqueControl.Entity.TextElement textElement1 = 
    new MarqueControl.Entity.TextElement("wholetext");
            }

The issue is every time the same text textelemt is getting updated . means It will be solved only when I can make different identifier for the text element. Can Anyone suggest how to change the varaibale name inside a loop

Upvotes: 0

Views: 180

Answers (2)

Michael
Michael

Reputation: 629

// creation
var marqueeList = new List<MarqueControl.Entity.TextElement>();

for (int i = 1; i<=3; i++)
{
  marqueeList.Add(new MarqueControl.Entity.TextElement("TextElement "+i));
}

// usage
for(int i=0;i<dt.Rows.Count ;i++)
{
  String wholetext = [here is your retrieving code];
  marqueeList[0] = new MarqueControl.Entity.TextElement(wholetext); // 0 = first item
  // OR:
  marqueeList[i] = new MarqueControl.Entity.TextElement(wholetext);
}

Upvotes: 1

Floc
Floc

Reputation: 698

I'm not sure it's that you try to do but can you try this ?

List<TextElement> list = new List<TextElement>();
for(int i = 0; i < dt.Rows.Count; i++)
{
    String wholetext = dt.Rows[i][1].ToString() + "--" + dt.Rows[i][1].ToString();

    list.Add(new TextElement(wholetext));
 }
 superMarquee1.Elements.AddRange(list.ToArray());

Upvotes: 0

Related Questions