简体   繁体   中英

expose my class methods

I am making a simple app in wpf c#. I am new to object programming, been on a few course but trying to do my best. I expect this is very newbie mistake, so I apologise in advance!

I will explain in words then show my code.

I needed to work out the number of days from TODAY to the wage start day (we always start the wages on the same day eg Saturday), I will then use the number of days in a calculation to work out the wage start date.

To do this I have tried to write a class with a method. My expectation is that I should be able to call this method from the MainWindow.xaml.cs MainWindow. I was expecting to call the method with the format Class.Method . HOWEVER, in the MainWindow class when I type in my class name intellisense does not bring up my method as an option, I was expecting it to.

I expect I have not defined the class or the method properly, anyway here is the code:

The calling event handler

     private void cBChSite_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        string Location = cBChSite.SelectedItem.ToString();
        //Below is the call the the method DateCalcs.WageStart(Location); Which intellisense does not show
        int DaysAdjStart=DateCalcs.
    }

The class DateCalcs

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

    namespace Test_Read_CSV
    {
    public class  DateCalcs
    {
    DateCalcs()
    {
        //Constructor

    }

    public int WageStart(string Location)
    {
        //Returns the days difference from current to expect wage start 
        int DaysToStart;
        string CHCLocation = Location;
        DayOfWeek WageStartDay = WageWeekStart(Location);
        DayOfWeek Today = DateTime.Today.DayOfWeek;

        DaysToStart = WageStartDay - Today;

        // just to display result

       System.Windows.MessageBox.Show(DaysToStart.ToString(), "Information");
       return DaysToStart;


    }

    private DayOfWeek WageWeekStart(string Location)
    { 
        //Begin Switch
        switch (Location)
        {
            case "Leicester":
                {
                    return DayOfWeek.Wednesday;
                }
            default:
                return DayOfWeek.Saturday;
        }
    }
}
}

The method either has to be declared as static or you need an instance of the class. static methods are methods attributed to the class itself. non-static methods are typically referred to as "instance methods" meaning the instance itself owns the method and you have to have an instance in order to call it. So you can either;

Change the method definition to;

public static int WageStart(string Location)

and call it like;

int DaysAdjStart = DateCalcs.WageStart(Location);    

or instantiate the class with the new keyword like;

 DateCalcs dc = new DateCalcs();
 int DaysAdjStart = dc.WageStart(Location);

The basic problem is that you're trying to call an instance method like it's a static method. How you solve it is more of a design consideration and for your purposes likely doesn't matter. If this is a homework assignment, the language in your question makes it sound like you want a static method.

In general, utility methods should be static methods. Methods that operate on the data of an instance have to be instance methods. If the method doesn't care about any instance specific data then it is a candidate for being static .

In the calling event handler do this:

{
  string Location = cBChSite.SelectedItem.ToString();
  DateCalcs dc = new DateCalcs();

  int DaysAdjStart = dc. 

}

Intellisense will now show you the available methods.

In order to use methods on a class you need to instantiate the class into an object OR the methods / class need to be declared as static. In your case probably the best thing to do is simply instantiate it.

If you dont make the method static you have to instantiate the entire class. So try something like

public static int WageStart(string Location)

您的方法不是静态的,但是您正在尝试这样调用它们。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM