I have a Get Status
button on my form and the code for that currently looks like this:
private void btnGetStatus_Click(object sender, EventArgs e)
{
// Check if a runner has been selected
if (lstRunners.SelectedIndex > -1)
{
// Obtain selected runner
Runner selectedRunner = (Runner)lstRunners.SelectedItem;
// Call the method in Runner class to get the runner's status
selectedRunner.GetStatus(selectedRunner);
}
}
}
}
Now in the Runner
class I have:
public void GetStatus(Runner selectedRunner)
{
if (selectedRunner.HasFinished == true)
{
lblRunnerInfo.Text = "Runner has already finished!";
}
}
What I'm basically trying to do is make the btnGetStatus
call the GetStatus
method in the Runner
class and what I want that method to do is then basically check the boolean HasFinished
to see if the runner has finished and if they have finished, the lblRunnerInfo.Text
has a message to reflect this and if the boolean is false, then basically output a message saying "Runner has not yet finished / did not finish"
I'm not quite sure if it's proper practice to access form controls from a class or if it even can be done, but I am not sure of how to do it the way I want (Getting the GetStatus
method to check the status of the runner rather than getting the btnGetStatus
to fire the code.)
I think what you're looking for is this:
lblRunnerInfo.Text = selectedRunner.GetStatus();
and then in the runner class:
public string GetStatus()
{
if (this.HasFinished == true)
{
return "Runner has already finished!";
}
return "Finished";
}
You can change the btnGetStatus_Click as follows:
private void btnGetStatus_Click(object sender, EventArgs e)
{
// Check if a runner has been selected
if (lstRunners.SelectedIndex > -1)
{
// Obtain selected runner
Runner selectedRunner = (Runner)lstRunners.SelectedItem;
// Call the method in Runner class to get the runner's Status
// CHANGED
if (selectedRunner.HasFinished)
lblRunnerInfo.Text = "Runner has already finished";
}
}
This way, the form handles the output and the Runner class is responsible for "running". If you have a more complex Status Situation later on, then you still can add an enumeration for the states and retrieve the Status instead of just checking for HasFinished. To retrieve the text, you need to add a mapping from the Status enumeration to the appropriate text.
Why not just access your HasFinished() property directly?
private void btnGetStatus_Click(object sender, EventArgs e)
{
// Check if a runner has been selected
if (lstRunners.SelectedIndex > -1)
{
// Obtain selected runner
Runner selectedRunner = (Runner)lstRunners.SelectedItem;
// Call the method in Runner class to get the runner's status
if (selectedRunner.HasFinished)
{
lblRunnerInfo.Text = "Runner has already finished!";
}
else
{
lblRunnerInfo.Text = "Runner has NOT finished yet!";
}
}
}
how about something like this
private void btnGetStatus_Click(object sender, EventArgs e)
{
// Check if a runner has been selected
if (lstRunners.SelectedIndex > -1)
{
// Obtain selected runner
Runner selectedRunner = (Runner)lstRunners.SelectedItem;
// Call the method in Runner class to get the runner's status
lblRunnerInfo.Text = selectedRunner.GetStatus(selectedRunner);
}
}
then set your GetStatus as string
public string GetStatus(Runner selectedRunner)
{
if (selectedRunner.HasFinished == true)
{
return "Runner has already finished!";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.