Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am a computer science student and i have wrote a program an i dont know the error ? help me!!

What I have tried:

private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };
private DateTime fromDate;
private DateTime toDate;
private int year;
private int month;
private int day;

public DateDifference(DateTime d1, DateTime d2)

	if (d1 >d2)
{
    this.fromDate = d2;
    this.toDate = d1;
}
else
{
    this.fromDate = d1;
    this.toDate = d2;
}

	increment = 0; 
if (this.fromDate.Day > this.toDate.Day)
{ 
    increment = this.monthDay[this.fromDate.Month - 1]; 
}

if (increment== -1)
{
    if (DateTime.IsLeapYear(this.fromDate.Year))
    {
        increment = 29;
    } 
    else
    {
        increment = 28;
    }
}

if (increment != 0)
{    
    day = (this.toDate.Day+ increment) - this.fromDate.Day;
    increment = 1; 
}
else
{       
    day = this.toDate.Day - this.fromDate.Day;
}

if ((this.fromDate.Month + increment) > this.toDate.Month)
{   
    this.month = (this.toDate.Month+ 12) - (this.fromDate.Month + increment);
       increment = 1;
}
else
{    
    this.month = (this.toDate.Month) - (this.fromDate.Month + increment);
    increment = 0;
}

this.year = this.toDate.Year - (this.fromDate.Year + increment);


public int year;
public int month;
public int day;

public override string ToString()
{    
    return this.year + "Year(s), " + this.month + " month(s), " + this.day + " day(s)";
}
Posted
Updated 28-Apr-17 14:23pm
Comments
[no name] 28-Apr-17 19:20pm    
"i dont know the error", then learn how to use the debugger and figure it out.

1 solution

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900