C# - עזרה בתרגיל עם מתודות - עמוד 2 - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

C# - עזרה בתרגיל עם מתודות


gshhar

Recommended Posts

אני די מבולבל עם ה-THIS הזה ומה זה אומר, אשמח להבהרות.

בנוסף במתודה toString יש לי שגיאה שאומרת:

Error 1 'int' does not contain a definition for 'toString' and no extension method 'toString' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

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


namespace T270
{
public class Date
{
public int dd;
public int mm;
public int yy;


//method
public void setDate(int dd, int mm, int yy)
{
this.dd = dd;
this.mm = mm;
this.yy = yy;
}


//convert date to string metod
public string toString()
{


return dd.toString() + "/" + mm.toString() + "/" + yy.toString();


}


//returns the date of the next day method
//public Date nextDay()
//{
// Date nextDate = new Date();
// nextDate.Day = this.Day;
// nextDate.Month = this.Month;
// nextDate.Year = this.Year;


// return nextDay;
//}
}


class Program
{


static void Main(string[] args)
{
Console.WriteLine("Enter The First Date: ");
Console.Write("Enter Day: ");
string dd1 = Console.ReadLine();


Console.Write("Enter Month: ");
string mm1 = Console.ReadLine();


Console.Write("Enter Year: ");
string yy1 = Console.ReadLine();


Console.WriteLine("Enter The Second Date: ");
Console.Write("Enter Day: ");
string dd2 = Console.ReadLine();


Console.Write("Enter Month: ");
string mm2 = Console.ReadLine();


Console.Write("Enter Year: ");
string yy2 = Console.ReadLine();


}
}
}

קישור לתוכן
שתף באתרים אחרים

ToString, לא toString. אם אתה לא יודע את זה, ולא יודע מה זה this, אז זה אומר אחד משני דברים: או שאתה צריך לחזור אחורה בספר ולקרוא מחדש חלקים מסויימים, או שאתה צריך לזרוק אותו לפח וללמוד מספר אחר.

קישור לתוכן
שתף באתרים אחרים

אם כתבתי לדוגמא מתודה (אני עדיין באותו התרגיל) שנקראת nextDay ולמעשה מציגה לי את התאריך של יום אחרי התאריך שנתתי בהתחלה.

המתודה מחזירה nextDay.

איך אני פונה אליה ומציג את התאריך החדש ?

    //returns the date of the next day method
public Date nextDay()
{
Date nextDay = new Date();
nextDay.dd = this.dd;
nextDay.mm = this.mm;
nextDay.yy = this.yy;


//the day is smaal than 30
if (nextDay.dd < 30)
{
nextDay.dd++;
}


return nextDay;

קישור לתוכן
שתף באתרים אחרים

זה הקוד שלי, בסופו אני מדפיס את התאריך רוצה בשורה אחריו את ה-NEXT DAY אבל אני לא יודע איך להפעיל את המתודה, איך בדיוק לרשום, זה מה שאני שואל, אני פשוט רושם מתודה מתודה ובודק אם זה פועל.

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


namespace T270
{
public class Date
{
public int dd;
public int mm;
public int yy;


//method
public void setDate(int dd, int mm, int yy)
{
this.dd = dd;
this.mm = mm;
this.yy = yy;
}


//convert date to string metod
public string ToString()
{


return dd.ToString() + "/" + mm.ToString() + "/" + yy.ToString();


}


//returns the date of the next day method
public Date nextDay()
{
Date nextDay = new Date();
nextDay.dd = this.dd;
nextDay.mm = this.mm;
nextDay.yy = this.yy;


//the day is smaal than 30
if (nextDay.dd < 30)
{
nextDay.dd++;
}


return nextDay;
}
}


class Program
{


static void Main(string[] args)
{
Date D1 = new Date();


D1.dd = 10;
D1.mm = 11;
D1.yy = 1995;


Console.WriteLine(D1.dd + "/" + D1.mm + "/" + D1.yy);

Console.ReadLine();


}
}
}

קישור לתוכן
שתף באתרים אחרים

אתה לא יודע איך לקרוא למתודה של אובייקט? פתח את הספר שלך וקרא אותו מההתחלה. לא ייתכן שאתה כותב מחלקות בלי לדעת איך קוראים למתודה של מחלקה.

שים לב שלמרות שהתרגיל דרש שהשדות של המחלקה יהיו פרטיים, עדיין עשית אותם פומביים.

קישור לתוכן
שתף באתרים אחרים

"יש לי בעיה"? איזו בעיה יש לך?

וכיוון שהשדות הם public, אתה משתמש במחלקה לא נכון - הכנסת הנתונים לתוך המחלקה צריכה להשתמש במתודה setDate, וההדפסה שלהם צריכה להשתמש במתודה toString, ולא באמצעות גישה ישירה לשדות. אם השדות היו private אז לא היית יכול לגשת אליהם ישירות, והיית חייב להשתמש במתודות האלו.

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...