Solved

Pass calendar control property value as argument into method

Posted on 2014-11-03
3
239 Views
Last Modified: 2014-11-04
Hi, how can I set a variable for a control property?  Ie, for a Calendar control VisibleDate property I would like to pass the value into the  GetFirstDayOfNextMonth() method shown below.  So if I have multiple calendar controls I would like to pass say (pseudo)
if
(Calendar_Jan.VisibleDate.Month = 12 / Calendar_Feb.VisibleDate.Month = 12 / Calendar_Month.VisibleDate.Month = 12) == 12
{

}

so I want to pass Calendar_XXX.VisibleDate.Month as an argument into GetFirstDayOfNextMonth()

protected DataSet dsAgent;

protected DateTime GetFirstDayOfNextMonth()
        {
            int monthNumber, yearNumber;
            if (Calendar_Jan.VisibleDate.Month == 12)
            {
                monthNumber = 1;
                yearNumber = Calendar_Jan.VisibleDate.Year + 1;
            }
            else
            {
                monthNumber = Calendar_Jan.VisibleDate.Month + 1;
                yearNumber = Calendar_Jan.VisibleDate.Year;
            }
            DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
            return lastDate;
        }

        protected void Fill_ds()
        {
            DateTime firstDate = new DateTime(Calendar_Jan.VisibleDate.Year, cld.VisibleDate.Month, 1);
            DateTime lastDate = GetFirstDayOfNextMonth();           
            Int32 empNum = 6022022;
            dsAgent = GetAgent(firstDate, lastDate,empNum);

        }

        protected DataSet GetAgent(DateTime startDate, DateTime endDate, Int32 empNum)
        {
            String qryAgent = "select * from dbo.tbl_Table where c_id = @empNum and DTE between @startDate and @endDate";
            DataSet dsGetAgent = new DataSet();

            using (SqlConnection conn = new SqlConnection(strConn))
            {
                using (SqlCommand cmd = new SqlCommand(qryAgent, conn))
                {
                    cmd.Parameters.Add(new SqlParameter("@startDate", @startDate));
                    cmd.Parameters.Add(new SqlParameter("@endDate", @endDate));
                    cmd.Parameters.Add(new SqlParameter("@empPein", empNum));
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    try
                    {
                        da.Fill(dsGetAgent);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    return dsGetAgent;
                }

            }
        }

Open in new window

0
Comment
Question by:Scarlett72
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40420726
I dont know what Type your calendar control is, but this is one way to do it:
protected DataSet dsAgent;

protected DateTime GetFirstDayOfNextMonth(Calendar calendar)
{
	int monthNumber, yearNumber;
	if (calendar.VisibleDate.Month == 12)
	{
		monthNumber = 1;
		yearNumber = calendar.VisibleDate.Year + 1;
	}
	else
	{
		monthNumber = calendar.VisibleDate.Month + 1;
		yearNumber = calendar.VisibleDate.Year;
	}
	DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
	return lastDate;
}

protected void Fill_ds()
{
	DateTime firstDate = new DateTime(Calendar_Jan.VisibleDate.Year, cld.VisibleDate.Month, 1);
	DateTime lastDate = GetFirstDayOfNextMonth();           
	Int32 empNum = 6022022;
	dsAgent = GetAgent(firstDate, lastDate,empNum);
}

protected DataSet GetAgent(DateTime startDate, DateTime endDate, Int32 empNum)
{
	String qryAgent = "select * from dbo.tbl_Table where c_id = @empNum and DTE between @startDate and @endDate";
	DataSet dsGetAgent = new DataSet();

	using (SqlConnection conn = new SqlConnection(strConn))
	{
		using (SqlCommand cmd = new SqlCommand(qryAgent, conn))
		{
			cmd.Parameters.Add(new SqlParameter("@startDate", @startDate));
			cmd.Parameters.Add(new SqlParameter("@endDate", @endDate));
			cmd.Parameters.Add(new SqlParameter("@empPein", empNum));
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			try
			{
				da.Fill(dsGetAgent);
			}
			catch (Exception)
			{
				throw;
			}
			return dsGetAgent;
		}
	}
}

Open in new window


Ofcouse, I would prefer to use an extension method:
public static class Extensions
{
	protected static DateTime GetFirstDayOfNextMonth<T>(this T calendar) where T : Calendar
	{
		int monthNumber, yearNumber;
		if (calendar.VisibleDate.Month == 12)
		{
			monthNumber = 1;
			yearNumber = calendar.VisibleDate.Year + 1;
		}
		else
		{
			monthNumber = calendar.VisibleDate.Month + 1;
			yearNumber = calendar.VisibleDate.Year;
		}
		return new DateTime(yearNumber, monthNumber, 1);
	}
}

Open in new window


Using the extension you would simply have to call it from your control itself:
DateTime result = Calendar_Jan.GetFirstDayOfNextMonth();
// or you could call it like this as well
DateTime result2 = Extensions.GetFirstDayOfNextMonth(Calendar_Jan);

Open in new window


-saige-
0
 

Author Closing Comment

by:Scarlett72
ID: 40421886
Thank you -saige- this answered my question nicely, and has introduced the concept of 'Extension methods' to me; which I have to be honest is advanced for my skill level, never the less has taken me down a new rabbit hole  : )
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40421917
You are very welcome.

-saige-
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question