none
Winforms 3-tier. Select Year and month from comboxes to send datetime value to business layer? RRS feed

  • Question

  • Hi!

    have an issue trying to forward selected year as datetime to the business layer since i'm not quite sure how to declare it.

    This is what I've tried so far while experimenting:

    int id = int.parse(cbId.Text);
    
    //here's what i've tried
    
    DateTime.Now.Year year = cbYear.Text;
    
    DataTime.utcNow.year year= cbyear.Text;
    
    DateTime.Today.Year year = cbYear.Text;

    Never really done much with datetime this way so it's uncharted territory for me.

    This what I'll like to forward to the business layer:

    bl.InsuranceSoldPeriod(id, year, month);

    Anyone who might be able to enlighten me on how to go about this?

    Thanks in advance!

    Regards

    Gab


    Student at the University of Borås, Sweden

    Wednesday, October 31, 2018 12:55 PM

Answers

  • solved it by converting the values to int. thanks for the reply!

    Student at the University of Borås, Sweden

    • Marked as answer by Gabbelino Thursday, November 1, 2018 12:14 PM
    Thursday, November 1, 2018 12:14 PM

All replies

  • A datetime in .NET is simply the DateTime type. If you just care about the year then personally I'd probably just use an integral value (2000, 2018, etc). If you really want to stick with DateTime then set the month and day to 1 and ignore it in the business layer.

    Given your example code in the business layer it seems like you don't care about the day so just use a DateTime and ignore the day part.

    //Assuming you have a combo with the years given as (2000, 2001, etc)
    
    //Helper method for converting control values to ints - assuming here that valid values are > 0
    int GetInt32 ( Control control )
    {
       if (Int32.TryParse(control.Text, out var value))
          return value;
    
       return -1;
    }
    
    //Usage for a DateTime - defaulting to January 1
    var dt = new DateTime(GetInt32(cbYear), 1, 1);
    
    //Pass to business layer
    bl.InsuranceSoldPeriod(id, dt);
    
    
    //In business layer
    
    public class BusinessLayer
    {
       public void InsuranceSoldPeriod ( int id, DateTime date )
       {
           //Ignore the day if you don't care about it
       }
    }
    


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 31, 2018 2:07 PM
    Moderator
  • solved it by converting the values to int. thanks for the reply!

    Student at the University of Borås, Sweden

    • Marked as answer by Gabbelino Thursday, November 1, 2018 12:14 PM
    Thursday, November 1, 2018 12:14 PM