3
//program.cs
foreach (Mutant mutant in mutants)
{
    label12.Text=mutant.displayInfo();
}

//Mutant.cs 
public abstract int dangerQuotient();
public String displayInfo()
{
    return(codename + " " + dangerQuotient().ToString());
}

//physicalMutant.cs 
public override int dangerQuotient()
{
    return level * IQ * strength;
}

//elementMutant.cs
public override int dangerQuotient()
{
    return level * region;

}

Mutant is my base class and I have two derived classes physicalMutant and elementMutant. At final I'm getting the output for elementMutant class alone in label12. How to get output of physicalMutant too in the same label. By using message box control inside foreach loop i can get values from the displayQuotient() in both derived classes but by using label i can able to display only the last iteration value.How to overcome this problem?

Rama Priya
  • 2,387
  • 7
  • 28
  • 39

1 Answers1

0

I am really not sure what you are wanting, but if it is to add all of your Mutants dangerQuotient to your label you will need to change your foreach statement to something like this. You are currently replacing the label's contents every iteration of your foreach loop.

label12.Text = ""; // or just delete the text in the designer
foreach(Mutant mutant in mutants)
{
    label12.Text += mutant.displayInfo() + "\n"; //  This will create a seperate line for each displayInfo
   // label12.Text += mutant.displayInfo();  //This will add them on the same line
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • Its working but i am getting output along with the label12 text. – Rama Priya Dec 27 '12 at 07:06
  • Output:label12John 18 Allen 32.From this i need to remove the text "label12" – Rama Priya Dec 27 '12 at 07:11
  • @RamaPriya what output are you getting? If it is the starting Text of label You can either delete it, check add a "\n" to it right before you enter your foreach loop. The first way will remove it entirely the second will leave it as a heading. – Mark Hall Dec 27 '12 at 07:16
  • @RamaPriya I added an edit to my answer it will set the Contents of the Label to nothing before you enter your foreach loop – Mark Hall Dec 27 '12 at 07:19
  • @RamaPriya You are welcome. If this or any other answer to any of your questions answer your question, you should click the Checkmark at the right of the answer so the question is marked as closed. It will give you some rep and the person who helped some also. – Mark Hall Dec 27 '12 at 07:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21746/discussion-between-rama-priya-and-mark-hall) – Rama Priya Dec 27 '12 at 07:37