0% found this document useful (0 votes)
13 views

Lecture-08 Handling Higher Math

Uploaded by

kingofdeath1380
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture-08 Handling Higher Math

Uploaded by

kingofdeath1380
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Ghalib Private University

Visual Programming

By: M.Dawood Saddiqi


Handling Higher Math
• If we want to use the math methods in our project we need
to import the System.Math class.
• The built-in math class functions appear in coming slide.
Some of Math System classes

• There are many more if you want to explore.


Handling Dates and Times
• One of the biggest headaches a programmer can
have is working with dates. So Visual C# has a
number of date and time handling functions that
make the programmer work easy.
• The DateTime struct can store datetime information.
It makes handing time easier.
Some Examples:
• DateTime dt =DateTime.Now; MessageBox.Show(dt.ToString());

• DateTime dt =DateTime.Today; MessageBox.Show(dt.ToString());

• DateTime dt =DateTime.Now; MessageBox.Show(dt.ToLongDateString());

• DateTime dt =DateTime.Now; MessageBox.Show(dt.ToShortDateString());

• DateTime dt =DateTime.Now; MessageBox.Show(dt.TimeOfDay.ToString());

• DateTime dt =DateTime.Now; MessageBox.Show(dt.ToShortTimeString());


Constructor:
• Constructor. With new, we invoke the instance DateTime
constructor. The arguments must match a real date that
occurred. This is a validating constructor.
• DateTime value = new DateTime(2014, 1, 18);
• Console.WriteLine(value);
• Console.WriteLine(value == DateTime.Today);
• DateTime.Today is always set to the machine's local time, which depends on the current
system.
Get Yesterday
• Yesterday: We can subtract one day from the current day. We do this
by adding -1 to the current day. This is necessary because no
"Subtract Days" method is provided.
• DateTime y = GetYesterday();
• static DateTime GetYesterday()
• { // Add -1 to now
• return DateTime.Today.AddDays(-1);
•}
Get Tomorrow
• Tomorrow. To figure out tomorrow, we add one using the Add()
method. This is useful for using date queries in databases. We use
the AddDays method.
• DateTime d = GetTomorrow();
• static DateTime GetTomorrow()
•{
• return DateTime.Today.AddDays(1);
•}
First Day of Year
• DateTime d = FirstDayOfYear(DateTime.Today);

• static DateTime FirstDayOfYear(DateTime y)


• {
return new DateTime(y.Year, 1, 1);
•}
Get Last Day
• Last day. Here we find the last day in any year.
• DateTime d = new DateTime(1999, 6, 1);
• d= LastDayOfYear(d);
• /// Finds the last day of the year for the selected day's year.
• static DateTime LastDayOfYear(DateTime d)
• {
• // 1. Get first of next year.
• DateTime n = new DateTime(d.Year + 1, 1, 1);
• // 2. Subtract one from it.
• return n.AddDays(-1);
• }
Days In Month
• Many static methods are also available on the DateTime class. With
DaysInMonth we look up the number of days in a month based on
the year.
• int days = DateTime.DaysInMonth(2014, 9);
• // days will be 30
• days = DateTime.DaysInMonth(2014, 2);
• // days will be 28
Cont…
• TimeSpan: represents a length of time. We can create or manipulate
TimeSpan instances in a C# program. The TimeSpan type provides
many helper properties and methods. It is implemented as a struct
type.
• Add:The Add method (and Subtract) requires a TimeSpan
argument. We must first use the TimeSpan constructor.
• AddDays:Receives a double integer, which adds or subtracts days.
We can use AddHours, AddMinutes, AddSeconds and more.
Cont…
• AddTicks:One tick is considered one millisecond. This
method might be useful when used with
Environment.AddTicks.
• DateTime.Month: returns an integer. This value indicates
the month in the year, from January to December. With a
format string, we can get month names.
• DateTime now = DateTime.Now; Console.WriteLine(now.Month); // 4 Console.WriteLine(now.ToString("MMM"));
//April
Display Complete Months Names
• DateTime now = DateTime.Now;
• for (int i = 0; i < 12; i++) { Console.WriteLine(now.ToString("MMMM")); now =
now.AddMonths(1);
•}
• output
• April
• May ………………………………. March
Find the difference between dates in C#
• DateTime dt1 = new DateTime(2014, 12, 29);
• DateTime dt2 = new DateTime(2010, 08, 30);
• TimeSpan diff = dt1-dt2;
• DateTime age=DateTime.MinValue+diff;
• // Make adjustment due to MinValue equalling 1/1/0001

• int y = age.Year - 1;
• int m = age.Month - 1;
• int d = age.Day - 1;
• textBox1.Text = y.ToString() +" / " +m.ToString() +" / " + d.ToString();
Using format to display dates and time
• Dt.ToString(“M-d-yy")); // 1-1-15
• Dt.ToString(“MM-dd-yy")); // 05-31-15
• Dt.ToString(“M/d/yy")); // 1/1/15
• Dt.ToString(“ddd,MMMM d, yyy")); // sundy, January 1, 2015
• Dt.ToString(“hh:mm:ss MM/dd/yy")); // 12:20:50 05/07/15
• Dt.ToString(“hh:mm:ss tt MM-dd-yyy")); // 01:00:00 AM 11-18-2015
The timer Control
• The Timer control is essentially an invisible stopwatch that gives you access to
the system clock in your programs.
• The Timer control can be used like an egg timer to count down from a preset
time, to cause a delay in a program, or to repeat an action at prescribed
intervals.
• You set a timer’s interval by using the Interval property, and you activate a
timer by setting the timer’s Enabled property to True Once a timer is enabled, it
runs constantly until the user stops the program or the timer object is disabled
Creating a Digital Clock by Using a Timer
Control
• Create project, drags and set the properties for the object given

• Double-click the timer object in the component tray The Timer1_Tick event
procedure appears in the Code Editor This is the event procedure
• that runs each time that the timer clock ticks Type the following statement:
Label1.Text = DateTime.now.ToShortTimeString();
Using a Timer object to set a time limit
Sample Project
DateTime stoptime;
private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
TimeSpan duration = TimeSpan.Parse(textBox1.Text);
stoptime = DateTime.Now.Add(duration);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan remaining = stoptime.Subtract(DateTime.Now);
remaining = new TimeSpan(remaining.Hours, remaining.Minutes, remaining.Seconds);

if (remaining.TotalSeconds < 0)
{
remaining = TimeSpan.Zero;
this.WindowState = FormWindowState.Maximized;
this.TopMost = true;
timer1.Enabled = false;
}
label1.Text = remaining.ToString();
}
Inserting Code Snippets
• In the Code Editor, position the insertion point (I-beam) at the location where
you want to insert the snippet
• On the Edit menu, click IntelliSense, and then click Insert Snippet Browse to the
snippet that you want to use, and then double-click the snippet name.
3 Types of Error
• A syntax error (or compiler error) is a mistake (such as a misspelled property or keyword) that
violates the programming rules of Visual C# Visual C# will point out several types of syntax
errors in your programs while you enter program statements, and it won’t let you run a
program until you fix each syntax errors.

• A run-time error is a mistake that causes a program to stop unexpectedly during execution
Run-time errors occur when an outside event or an undiscovered run-time error forces a
program to stop while it’s running For instance, if you try to read a disk drive and it doesn’t
contain a CD or DVD, your code will generate a run-time error.

• A logic error is a human error—a mistake that causes the program code to produce the wrong
results Most debugging efforts are focused on tracking down logic errors introduced by the
programmer.
Using Debugging
• In debugging mode, you have an opportunity to see how the logic in your program is
evaluated.
• Tracking Variables by Using a Watch Window
• Visualizers: Debugging Tools That Display Data
• Using the Immediate (are used if immediately we want change in running program)
• Type >immed for immediate window

You might also like