Click here to Skip to main content
14,422,059 members
Rate this:
Please Sign up or sign in to vote.
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 15:23pm
Comments
NotPolitcallyCorrect 28-Apr-17 19:20pm
   
"i dont know the error", then learn how to use the debugger and figure it out.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100