calling upon classes in C#  | |
January 29th, 2006, 05:03 PM
|
#1 (permalink)
| | Mobile Member
Join Date: Apr 2005 Location: S. Central PA
Posts: 3,601
| calling upon classes in C#
im trying to get the exact context down for being able to call upon a class i made in a C# console app. Our teach wanted us to make a console app then add a class and call it date and then use the main part of the program to run the class "date," i copied the basic stuff i need into the class "date" then i need to call it from the other "window" where the program started.. then i can see my output then work on it form there? does anyone know the context on how to call on a class in C# (console app)
thanks
J.D.
__________________ Thinkpad T61 14.1" wide | WinXP Pro | C2D T8300 CPU | 3GB DDR2 | 160GB HDD | AGN & WWAN
Lenovo S10 10.2" LED display | 1.6Ghz Atom CPU | 1GB DDR2 | 1.3mp webcam | B/G WiFi | 160GB HDD |
| |
January 29th, 2006, 11:24 PM
|
#2 (permalink)
| | Mobile Member
Join Date: Apr 2005 Location: S. Central PA
Posts: 3,601
|
well to sorta elaborate on the first part of my post.... Im back in programming this semester and i have an assignment that i have to do, its working with multiple classes, well we make a class and call on it in the program, i guess class1, but im not sure exactly how to call on it, or really where to start on the program, if i get an inkling as to where to start im pretty sure i could do it from there, i understand what the code is saying and somewhat how to write it, its just that im confused as how to do "error checking" well heres the assignment that i was given: Modify the date class of (provided) to perform error checking on the initializer values for instance variables month, date, and year. Also provide a method NextDay to increment the day by one. The Date object should always remain in a consistent state. Write a program that tests the NextDay method in a loop that prints the date during each iteration of the loop to illustrate that the NextDay method works correctly. I have to be careful that I make sure my program increments into the next month and into the next year
here is the code that was given to me from the text... Code: using System;
namespace ex8_4
{
/// <summary>
/// Summary description for Date.
/// </summary>
public class Date
{
private int month; //1-12
private int day; //1-31 based on month
private int year; // any year
//constructor confirms proper value for month
//call method CheckDay to confirm proper value for day
public Date(int theMonth, int theDay, int theYear)
{
//validation of month
if ( theMonth > 0 && theMonth <= 12)
month = theMonth;
else
{
month = 1;
Console.WriteLine(
"Month {0} invalid. Set to month 1.", theMonth);
}
year = theYear; // year validation
day = CheckDay( theDay );
} //end of date constructor
//a utility method to confirm proper day value based on month and year
private int CheckDay( int testDay)
{
int[] daysPerMonth =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//a check to make sure its not a leap year
if (month == 2 && testDay == 29 &&
(year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0 )))
return testDay;
Console.WriteLine(
"Day {0} invalid. Set to day 1.", testDay);
return 1; //leaves object in consistent state
}
//returns date string as month/day/year
public string ToDateString()
{
return month + "/" + day + "/" + year;
}
//time to add some methods
}//end class Date
} so this is a nice foundation for me to start on, although im not sure what to do next, my main part of my console app looks like this:
using System;
namespace ex8_4
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
// what do i write in here???
Date date = new //????
}
}
}
soo yea any suggestions or help would be appreciated, any thoughts would also be great. Im still wondering how/where to write the methods to do the things it wants me to, i think in the date.cs but then how to i word it so that the program calls upon that class.. (getting back to my original ?? ) |
| |
January 31st, 2006, 02:39 PM
|
#3 (permalink)
| | Caveat Emptor
Join Date: Mar 2005 Location: Out of my mind
Posts: 3,241
|
All your code will belong in the class as functions (appropriately called "methods"). Looks like error checking is in there for month, day and year???
Your "main{}" should probably look like this:
main() {
int i;
Date myDate(12,25,2006);
for(i=0;i<10;i++)
{
myDate.NextDay(); // you write this to increment the day (and month? and year?)
myDate.PrintDate(); // you write this one too (might call existing method to format)
}
} |
| |
February 1st, 2006, 05:32 PM
|
#4 (permalink)
| | Mobile Member
Join Date: Apr 2005 Location: S. Central PA
Posts: 3,601
|
thanks roots,
well i have my stuff due today and i was already able to figure it out.. What i was having a problem with was understanding how to get the array to work correctly and then how to call from the main class, into the other class i made so that i can get it right... (pretty much formatting issues)
Console.WriteLine("\n"); //courtesy space
Console.WriteLine("Check increment day"); cls_Date Day_Obj=new cls_Date(12,22,1981);
Day_Obj.NextDay(); Console.WriteLine("12-22-1981-Next day={0} ",Day_Obj.ToString());
Console.WriteLine("\n");
thats how i was able to refer to the other class, i was all confused on how to get it to do it, until i asked the teach on monday to show us how we need to format it, he pretty much just sat down an coded it for us.. haha although i had some of it done, i still didnt know how to make em relate...  |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |