Problem with Day/Month/Year program in C#

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 6810 Views - Last Post: 20 January 2011 - 08:22 PM Rate Topic: -----

#1 Nitewalkr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 147
  • Joined: 17-November 10

Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 01:55 PM

Greetings;

This is my first attempt in a very long time and I am in the need of help "with no reference ofcourse"

the UML that I've created was:

MyDate
- day : Integer
- month : Integer
- year : Integer
+ «constructor» MyDate()
+ «constructor» MyDate (y : int, m : int, d : int)
+ Add(d : int)
+ Add(date : MyDate)
+ ToString() : String

The program suppose to display the date and I have forgotten half of c# programming.

Tagged to this thread are 2 set of .cs files one program which is main and one class MyDate.

MyDate:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment1_ClassExercise
{
    class MyDate
    {
        int day, month ,year;

        MyDate()
        {
            day = 1;
            month = 1;
            year = 1900;
        }
        MyDate(int d, int m, int y)
        {
            day = d;
            month = m;
            year = y;
        }
        int Add(int d)
        {
            return d++;
        }
        int Add(MyDate date)
        {
            if(day=30)
            {
                month++;
                day = 1;
            }
            if (month=12)
            {
                year++;
                month = 1;
            }
        }

        public override string ToString()
        {
            return MyDate();
        }

    }
}



Main
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment1_ClassExercise
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDate day, month, year;
            int init = 1;
            Console.WriteLine"{0},{1},{3}".ToString();

        }
    }
}



I know the logic is sound but it has few errors. I know that there is suppose to be one default constructor which suppose to initialize the day month and year and the constructor which has params in it suppose to initialize day to d month to m and year to y

there is one add method which suppose to increase the day only.
there is the second add method which suppose to increase the month if day is 30 and initiate day back to 1 and same goes for the year and months.

Now, as I said before that I havent done c# programming in a long time and I have forgotten half of the coding methods there fore there are syntax errors in this one. I need help fixing them.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Day/Month/Year program in C#

#2 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:13 PM

Well, your constructors are right, but that's about it.

This is going to seem mean, but please take it all as constructive criticism. There's a lot wrong with this, but the structure is there, and it should be easy enough to fix.

All that your Add(int d) function is going to do is return whatever you passed it (and then afterwards add one to it, but it's already out of scope at that point).

Your Add(MyDate date) will do absolutely nothing unless day = 30 or month = 12. You haven't handled any other case.

An issue that both have in common is that you're passing external values to them to operate on. You're passing some int to Add(int d). That's an external value, not the actual "day" that's associated with your MyDate object.

Same goes for Add(MyDate date). Are you actually supposed to pass a second date to it to add? Or a number of days or months or what?

Your ToString method actually shouldn't compile. You need to create a string to return, not just try to call a method that doesn't exist.

On to your main method:

Your instantiation of the date is incorrect. What you've actually done is create three MyDates, called day, month and year respectively. Remember to use the "new" keyword.

You've created a variable called "init" and done nothing with it.

You've also tried to write three tokens to the console, but didn't provide the tokens to actually write.

Attempt to fix these mistakes, show us what you've done, and we'll work on it some more.
Was This Post Helpful? 1
  • +
  • -

#3 Nitewalkr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 147
  • Joined: 17-November 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:19 PM

ya I kinda got lost in init and the To string method doesnt that way I know that. I got the structure and idea of how to achieve it but I know that there are many things wrong with it since I havent written a program in c# in like long time?

Also please do not hold back on your crits and call them mean. Crits are suppose to give you the honest opinion of one's work and suggest him/her things he/she can do in terms of improving/correcting that work. That forced me to post this here.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:22 PM

Ok, well go ahead and work on fixing the issues I listed. Once you do, you can re-post your code and we can see what else is wrong.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:35 PM

I'm just curious why you are re-inventing the wheel.
There is already a DateTime object class in the .NET framework loaded with properties and methods you can use, including .AddSeconds(int) multiple overloads of ToString() and so on.
Why are you trying to create your own Date object? You might as well try to re-invent the internal combustion engine the next time you go buy a car.
Was This Post Helpful? 0
  • +
  • -

#6 Nitewalkr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 147
  • Joined: 17-November 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:44 PM

@tlhIn'toq: It was an assignment since last night but I want to understand this entire thing before moving on so I'll have less issues in programming c# any further. I know there is a system library of it.

@InsertAlias: Ok so I've done modifications in this program. Got 3 errors now. 2 related to bool which is an if else and one related to ToString display method.

MyDate:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment1_ClassExercise
{
    class MyDate
    {
        int day, month ,year;

        MyDate()
        {
            day = 1;
            month = 1;
            year = 1900;
        }
        MyDate(int d, int m, int y)
        {
            day = d;
            month = m;
            year = y;
        }
        //int Add(int d)
        //{
        //    return day;
        //}
        int Add(MyDate date)
        {
            day++;
            if(day=30)
            {
                month++;
                day = 1;
            }
            if (month=12)
            {
                year++;
                month = 1;
            }
        }

        public override string ToString()
        {
            return string.Format("year: {0} month: {1} day: {2}");
        }

    }
}



Main:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment1_ClassExercise
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDate day, month, year;
            Console.WriteLine(MyDate.ToString());
        }
    }
}



Errors:

-C:\..Program.cs(13,31): error CS0120: An object reference is required for the non-static field, method, or property 'object.ToString()'
-C:..mscorlib.dll: (Related file)
-C:..MyDate.cs(31,16): error CS0029: Cannot implicitly convert type 'int' to 'bool'
-C:..MyDate.cs(36,17): error CS0029: Cannot implicitly convert type 'int' to 'bool'


The 2 string method is a syntex error I know this because there is either system.console.writeline(Mydate.ToString()) would work or the one without writing system in front of it would work.

and I'll see what happened in the bool erros. Now the errors are in the condition and one disply.

Sorry to say one thing 3 times in 3 different ways. lol
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:50 PM

Quote

C:\..Program.cs(13,31): error CS0120: An object reference is required for the non-static field, method, or property 'object.ToString()'
-C:..mscorlib.dll: (Related file)
-C:..MyDate.cs(31,16): error CS0029: Cannot implicitly convert type 'int' to 'bool'
-C:..MyDate.cs(36,17): error CS0029: Cannot implicitly convert type 'int' to 'bool'


This is debugging 101. You're asking for a int and sending a bool. Any object you reference you actually have to make before you can use.

Debugging tutorial


Seeing as this is a course assignment you might consider asking your professor what these mean. It will show him that he is not doing a good job of teaching his class.
Was This Post Helpful? 0
  • +
  • -

#8 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:56 PM

Regarding your syntax errors, this is caused by the '=' sign. This means that you assign a value to a parameter (at least in your case).

if(day=30)
{
//do things


means that day has a integer value of 30. You do not want to assign that, rather, you want to verify if the integer value for day is equal to 30. (if it is true, add month+1, if false, do nothing). Hence, you should use '==' instead of '='.

Hope this helped you out a bit. ^^

This post has been edited by karabasf: 19 January 2011 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 02:57 PM

Ok, the problems you're seeing there about converting ints to bools is because you're using "=" which is an assignment operator. You need to use "==" for comparison.

Your ToString is closer, but you also have to provide the actual tokens you want to print.

Here's an example:

int x = 5, y = 6;
string toPrint = string.Format("x = {0}, y = {1}", x, y);


So apply the same principle there.

You're passing a MyDate to your add function. Why? If all it does is add a day, then it shouldn't take any parameters. It already is attached to a date by virtue of being defined in the class.

And your declaration in the main method is still wrong. Here's an example of declaring and instantiating objects:

SomeType myObject = new SomeType(param1, param2);


So follow that pattern.

And you should be close to done.
Was This Post Helpful? 1
  • +
  • -

#10 Arrangemonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 11

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 03:54 PM

i hope you dont mind me posting the assignment you're doing
Spoiler


while doing as your professor whishes is possible and easy,
the date class he wants you to write is simply crap.

1. you cannot optain the current date without accesing the actual system.datetime class, or external unmanaged assemblies which have access to the bios clock.
2. adding 2 dates and normalizing afterwards creates huge errors, because months are not always 30, but 31,28,31,30,31,30,31,31,30,31,30,31, and february has 29 days if the year is a leapyear, so adding 2 dates that way gives wrong results most of the time. same goes for adding only days, but its not that complicated.
3. the purpose of using managed languages is to not think about such things but focus on development.
4. doing such a simple class for dates makes the students believe writing a correct date class is easy ans straight forward, but one has to think alot about to make it right.

if i were you, i would suggest your professor something different for your first class
maybe a color or vertex class, cause these are easy to implement
Spoiler

just for example
(but no current calculator, its as complicated as the date class)

This post has been edited by insertAlias: 19 January 2011 - 04:18 PM
Reason for edit:: added spoiler tags

Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 04:20 PM

I'm sure you had the best intentions, but people don't need to hear why their homework is crap. They need help without being fed answers.

And I would very much suggest against giving your professor any advice on assignments. It's a good way to ruffle feathers; you're basically telling a professional who's paid to teach you that he sucks and you could do it better. Very bad idea.

I added the spoiler tags to hide the information that's less than relevant to this thread.

This post has been edited by insertAlias: 19 January 2011 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 Nitewalkr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 147
  • Joined: 17-November 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 07:35 PM

Damn you Arrangemonk told you 2ice that I need help not the assignment. But thanks. So much from digging info that I already know.

One more thing as someone indicated. I didnt make a new object for display.

Quote

if i were you, i would suggest your professor something different for your first class
maybe a color or vertex class, cause these are easy to implement


If I were you, I would participate and actually resolve the issue rather then being an ars ^^. This will help me understand how it used to work, since the assignment is no longer acceptable by the professor I chose to post here and learn it so I will have possible better chance to do the next one.

Oh by the way hez doing that color or vertex programming in game programming with xna ^^.

General reply:

Why the hell is it not taking the argument if the constructor is taking 3 arguments? Do I take off the default constructor?

This post has been edited by Nitewalkr: 19 January 2011 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4431
  • View blog
  • Posts: 7,702
  • Joined: 08-June 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 08:41 PM

Quote

Why the hell is it not taking the argument if the constructor is taking 3 arguments? Do I take off the default constructor?


Because you're not doing it right. Look at my example. I show you how you would instantiate some object. Follow that pattern for your object.
Was This Post Helpful? 0
  • +
  • -

#14 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Problem with Day/Month/Year program in C#

Posted 19 January 2011 - 09:48 PM

Constructor Tips 101™:

public class Person
{
    public string Name { get; set; }

    //Create an anonymous person.
    public Person()
    {
    }

    //Create a person with a name.
    public Person(string name)
    {
        Name = name;
    }
}



Basically, you can now create a person either way!

Person sergio = new Person("Sergio");


Or

Person anon = new Person();

Was This Post Helpful? 0
  • +
  • -

#15 Arrangemonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 11

Re: Problem with Day/Month/Year program in C#

Posted 20 January 2011 - 05:55 AM

in the add method its not really nececary to return a new object

if you make it this way
public void Add(Mydate date)
        {
            this.day += date.day;
            this.month += date.month;
            this.year += date.year;
            normalize();
        }


the result is stored in the current object
which is a bit against the immutable pattern but meh
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2