user2929108
user2929108

Reputation:

Increment an integer in a form

In my Form1 I have the following integer:

public static int completed;

I have multiple classes in my program; Runner is the superclass if you will then there is Expert : Runner, Beginner : Runner and Disabled : Expert

I basically have three numericUpDown boxes on my form for the user to input number to represent a time (hours, minutes and seconds). There is also a listbox called lstRunners that is populated using an array of objects, like so:

Runner[] runnerList;
runnerList = new Runner[10];

I have a button called btnFinish. What I am trying to do is add a method in the Expert class called CountFinished24() that I want to basically increment the completed integer by 1 if the selected Expert or Disabled runner has completed the run in 24:00:00 or less. This has to be recorded only for Expert and Disabled runners. I'm not sure how I'd go about doing this?

Upvotes: 0

Views: 127

Answers (2)

sab669
sab669

Reputation: 4104

Just elaborating on Weyland's answer, you might have something like this in your Runner class:

 protected virtual int OnFinish(int h, int m, int s) 
 { 
      return 0;
 }

Then in ExpertRunner / DisabledRunner, you override OnFinish by simply adding the ovverride key word:

 protected override int OnFinish(int h, int m, int s)
 { 
      //IF the total time is less than 24:00:00
           return 1;
      else
           return 0;
 }

Then over on your form...

 private void btnFinish_Click(object sender, EventArgs e)
 {
      completed += myExpertRunner.OnFinish(numericHour, numericMinute, numericSecond);
 }

You might want to use some sort of custom object (or DateTime) to store H:M:S in a more easily consumable fashion. But anyways...

In essence, override lets you use a method that takes the same arguments and uses the same name but behaves differently.

A super common example is the ToString() method that belongs to all objects. When you call this method, it might simply return the object type as a string. Other objects override this ToString() method and return something different. For example, the StringBuilder class' ToString method returns a string with each "element" in it.

Read up on the override keyword on MSDN. It also has examples. In their first example they use an abstract class, but in the second example they do something more similar to what I've written here.

Upvotes: 0

Weyland Yutani
Weyland Yutani

Reputation: 4960

Add a virtual OnFinished() method to Runner which gets called by btnFinish. Override this method for Expert and Disabled and increment the completed integer if they completed the race within 24h

Upvotes: 2

Related Questions