PDA

View Full Version : سوال: مشکل در ساخت کلاس



amir-aa
چهارشنبه 26 مرداد 1390, 17:10 عصر
سلام دوستان من اولین بارمه که با کلاس ها تو سی شارپ کار می کنم
من این کد رو نوشتم ولی روی _items ارور می ده.

namespace WebApplication1
{
[Serializable]
public class cartitem
{
private string _name;
private string _type;
private int _price;
private string _aks;
private int _id;
private int _total;
private int _tedad;
public void New()
{
}
public void New(string name, string type, int price, string aks, int id, int total, int tedad)
{
_id = id;
_name = name;
_price = price;
_type = type;
_aks = aks;
_total = total;
_tedad = tedad;
}
public int id
{
get
{
return _id;
}
set
{
_id = value;
}
}
//*************
public string name
{
get
{
return _name;
}
set
{
_name = value;
}
}
//***********
public int price
{
get
{
return _price;
}
set
{
_price = value;
}
}
//***********
public string type
{
get
{
return _type;
}
set
{
_type = value;
}
 
}
//**********
public int tedad
{
get
{
return _tedad;
}
set
{
_tedad = value;
}
}
//*********
public int total
{
get
{
return _tedad * _price;
}
 
}
}
 
//**************
[Serializable]
public class shoppingcard
{
private List<cartitem> _items;
public shoppingcard
{
if(this._items==null)
{
this._items=new List<cartitem>();
}
}

}
}

TeacherMath
چهارشنبه 26 مرداد 1390, 22:23 عصر
از new استفاده کن.

nasinas
پنج شنبه 27 مرداد 1390, 00:41 صبح
دوست عزیز Error رو بنویسی مشکلت زودتر حل میشه
لطفا Error رو اینجا بنویس

amir-aa
پنج شنبه 27 مرداد 1390, 09:39 صبح
دوست عزیز Error رو بنویسی مشکلت زودتر حل میشه
لطفا Error رو اینجا بنویس

Error 7 'WebApplication1.shoppingcard._items' is a 'field' but is used like a 'type' C:\Users\ahmadabadi\documents\visual studio 2010\projects\WebApplication1\WebApplication1\Clas s1.cs 124 17 WebApplication1

این ارور رو داره. ومن نمی دونم چطور از new() باید استفاده کنم میشه یه توضیح بدید لطفا.

Saman Hashemi
پنج شنبه 27 مرداد 1390, 12:25 عصر
WebApplication1 myWebApp=new WebApplication1();
شما اشتباه از کلاس استفاده میکنی کدی که برای استفاده از کلاس نوشتی بذار...!

amir-aa
پنج شنبه 27 مرداد 1390, 12:33 عصر
ببین دوست من من فعلا نمی خوام از کلاسم استفاده کنم اما در زمانی که دارم کد می نویسم این _items رو نمیشناسه و روش ارورر می ده

m.soleimani
پنج شنبه 27 مرداد 1390, 13:04 عصر
اسم کلاسی که استفاده کردی با اسم سازنده کلاس متفاوت هست یا من اشتباه متوجه شدم ؟
واسه گذاشتن کد اون گزینه که کنار فونت هست و روی حروف رنگی خط زده رو بعد از انتخاب کل کد بزن تا استایل قبلی رو به سایت نفرسته بقیه رو دچار مشکل کنه.



namespace WebApplication1
{
[Serializable]
publicclasscartitem
{
privatestring _name;
privatestring _type;
privateint _price;
privatestring _aks;
privateint _id;
privateint _total;
privateint _tedad;
publicvoid New()
{
}
publicvoid New(string name, string type, int price, string aks, int id, int total, int tedad)
{
_id = id;
_name = name;
_price = price;
_type = type;
_aks = aks;
_total = total;
_tedad = tedad;
}
publicint id
{
get
{
return _id;
}
set
{
_id = value;
}
}
//*************
publicstring name
{
get
{
return _name;
}
set
{
_name = value;
}
}
//***********
publicint price
{
get
{
return _price;
}
set
{
_price = value;
}
}
//***********
publicstring type
{
get
{
return _type;
}
set
{
_type = value;
}
 
}
//**********
publicint tedad
{
get
{
return _tedad;
}
set
{
_tedad = value;
}
}
//*********
publicint total
{
get
{
return _tedad * _price;
}
 
}
}
 
//**************
[Serializable]
publicclassshoppingcard
{
privateList<cartitem> _items;
publicshoppingcard
{
if(this._items==null)
{
this._items=newList<cartitem>();
}
}

}
}





./

amir-aa
پنج شنبه 27 مرداد 1390, 14:50 عصر
بچه ها ممنون حل شد باید بعد از
public shoppingcard
پرانتز باز و بسته می کردم.