Reputation:
I have this small piece of code:
public DisabledStudent(int id, int age, bool requiressupport)
{
this.requiressupport = requiressupport;
}
The array is similar to this below:
public partial class Form1 : Form
{
const int MaxStudents = 4;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Student[] studentList;
studentList = new Student[4];
studentList[0] = new Student(51584, 17);
studentList[1] = new Student(51585, 19);
studentList[2] = new Student(51586, 15);
studentList[3] = new Student(51587, 20);
for (int i = 0; i < MaxStudents; i++)
{
lstStudents.Items.AddRange(studentList);
}
}
What I want to do is output a string from an array of students and basically display a different part of text depending on whether the requiressupport
boolean is true
or false
:
public override string ToString()
{
return string.Format("Disabled Student - ID: {0} (Age {1})", this.Number, this.Age);
}
I want that statement above to basically say Disabled Student - ID: 45132 (Age 19) with support
if the requiressupport
boolean is true
and Disabled Student - ID: 45132 (Age 19) without support
if the requiressupport
boolean is false
, but I'm not sure how I'd go about this?
Upvotes: 3
Views: 511
Reputation: 11252
One of your many options is to use the ?: conditional operator
public override string ToString()
{
return string.Format("Disabled Student - ID: {0} (Age {1}) {2}", this.Number, this.Age, this.requiressupport ? "with support" : "without support");
}
Upvotes: 2