开发者

C#: Possibly a polymorphism function? But not sure

开发者 https://www.devze.com 2023-02-07 13:05 出处:网络
I\'m currently working on a lab for class and we were told to make a method in the main form called \"DisplayInformation. that accepts a parameter of type Time and then displays the Time objects infor

I'm currently working on a lab for class and we were told to make a method in the main form called "DisplayInformation. that accepts a parameter of type Time and then displays the Time objects information" Which doesn't sound difficult to me but we're told to use 2 buttons to access this method. One just goes to the base class and gets the current time and returns it. The other is suppose to go to a derived class. And this is where my problem begins.

private void btnDisplayTime_Click(object sender, EventArgs e)
{
    DisplayInformation(time1);


} 

And this is the DisplayInformation function I'm not sure about:

private string DisplayInformation (Time zone)
        {
            time1 = zone;
            time1.displayTime();
           // extTime1.displayTime();
            return "okay";

        }//end of DisplayInformation

When i call DisplayInformation with the first button it'll go just fine. And if i make a call from the second button to this method it'll be fine as well. But i need to be able to pick and choose which class i go to. It's difficult to explain really. I just need to be able to call from each button to that method and get a different output depending on the button. I'm not sure if this would just be an if statement or what.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CIS247A_Week_4_Lab_BREWER
{
class Time
{
    private const int MIN_HOUR = 0;
    private const int MAX_HOUR = 24;
    internal int hour;
    internal int minute;

    public Time()
    {
        hour = 12;
        minute = 00;
    }//end of Time

    public Time(int Hour, int Minute)
    {
        hour = Hour;
        开发者_运维问答minute = Minute;
    }//end of Time

    public int incrementHour(int step)
    {
        if (step > 0 && hour < 24)
        {
            //step = step % hour;
            hour = (hour + step) % 24;

            return hour;
        }//end of if

        else
        {
            MessageBox.Show("Please enter a positive number.");

            return 0;
        }//end of else
    }//end of incrementHour

    public int incrementMinute(int step)
    {
        if (step > 0 && minute < 60)
        {
            minute = (minute + step) / 60;

            return 0;
        }//end of if

        else if (step < 0)
        {
            MessageBox.Show("Please enter a positive number.");
            minute = 0;
            return 0;
        }//end of else if
        else
        {
            MessageBox.Show("Unknown error.");
            return 0;
        }
    }//end of incrementMinute
    public virtual string displayTime()
    {
        DateTime time = DateTime.Now; // Use current time
        string format = "MMM ddd d HH:mm yyyy"; // Use this format
        MessageBox.Show(time.ToString(format)); // Write to console

        return time.ToString(format);
    }//end of displayTime

    public int Hour
    {
        get { return hour; }


        set
        {
            if (value < MIN_HOUR)
            {
                hour = 0;
                MessageBox.Show("Hour value " + value.ToString() + " cannot be negative. Reset to " + MIN_HOUR.ToString(),
                "Invalid Hour", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                //take the modulus to ensure always less than 24 hours
                //works even if the value is already within range, or value equal to 24
                hour = value % MAX_HOUR;
            }
        }
    }
    public int Minute
    {
        get { return minute; }
        set
        {
            if (value < 0)
            {
                minute = 0;
                //MessageBox.Show("cannot be negitive" , MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                minute = value % 60;
            }


        }
    }
}//end of Time Class
}//end of Namespace

And the extendedtime class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CIS247A_Week_4_Lab_BREWER
{

class ExtendedTime : Time
{

    private string timeZone{get; set;}

    public ExtendedTime() : base()
    {
        timeZone = "CDT";

    }//end of ExtendedTime

    public ExtendedTime(int Hour, int Minute, String TimeZone) :base(Hour, Minute)
    {

        timeZone = TimeZone;
    }//end of ExtendedTime

    public override string displayTime()
    {
        //return base.displayTime();
        MessageBox.Show(base.displayTime() + timeZone);
        return base.displayTime() + timeZone;
    }//end of DisplayTime

}//end of ExtendedTime class

}//end of namespace

And just for time sake the section of the form i'm working on:

public partial class frmTime : Form
    {
        Time time1;
        ExtendedTime extTime1;


    public frmTime()
    {
        //DateTime Ctime = DateTime.Now; // Use current time
       // Ctime = new DateTime();
       // label1.Text = Ctime.ToString();
        InitializeComponent();
        time1 = new Time();
        extTime1 = new ExtendedTime();
    }


    private void DisplayInformation (Time zone)
    {
        time1 = zone;
       // time1.displayTime();
        extTime1.displayTime();
        //return "okay";

    }//end of DisplayInformation
    //exit Button (btnExit)
    private void btnExit_Click(object sender, EventArgs e)
    {
        Application.Exit();//closes the program

    }

    private void btnDisplayTime_Click(object sender, EventArgs e)
    {
        DisplayInformation(time1);


    }


In your DisplayInformation class if you are passing in a Time object from one click handler and an extTime object from the other handler then you should be able to just call the displayTime method.

private void DisplayInformation (Time zone)
{
    zone.displayTime();
}

on the Time one it will obviously call the base method. On the extTime one despite the fact you are treating it as a Time object it will call the overridden method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号