مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

mehdi1359a

عضویت از 1394/02/03

خطاي استفاده از كلاس

  • پنجشنبه 10 دی 1394
  • 11:23
تشکر میکنم

سلام دوستان گرامي كد زير چرا خطا ميده:

public class ItemsOfQuestions
    {
        public string item_title { get; set; }
        public Byte item_type { get; set; }
        public string item_image { get; set; }
        public Int16 item_grade { get; set;}
    }
    public class Questins_With_Items
    {
         
        public string question_title { get; set; }
        public List<ItemsOfQuestions> list_items { get; set; }
   
    }
 
بصورت زیر استفاده میکنم :
 
 
            List<ItemsOfQuestions> itemlist = new List<ItemsOfQuestions>();
            Questins_With_Items qwi = new Questins_With_Items();
            for (int i = 0; i < 5; i++)
            {
                ItemsOfQuestions ioq = new ItemsOfQuestions();
                ioq.item_title = "item"+i.ToString();
                ioq.item_type = 0;
                ioq.item_image = "";
                ioq.item_grade = (Int16)i;
                itemlist.Add(ioq);
        
            }           
            qwi.question_title = "سوال اول";
            qwi.list_items.Add(itemlist);

خطا در خطر آخره

پاسخ های این پرسش

تعداد پاسخ ها : 3 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • پنجشنبه 10 دی 1394
  • 11:28

به شکل زیر استفاده کنید برای کلاس Questins_With_Items باید سازنده بزاید

 

public class ItemsOfQuestions
    {
        public string item_title { get; set; }
        public Byte item_type { get; set; }
        public string item_image { get; set; }
        public Int16 item_grade { get; set;}
    }
    public class Questins_With_Items
    {
          
        public string question_title { get; set; }
        public List<ItemsOfQuestions> list_items { get; set; }


    public Questins_With_Items()
{
list_items =new List<ItemsOfQuestions>();
}
    }
  
  
            List<ItemsOfQuestions> itemlist = new List<ItemsOfQuestions>();
            Questins_With_Items qwi = new Questins_With_Items();
            for (int i = 0; i < 5; i++)
            {
                ItemsOfQuestions ioq = new ItemsOfQuestions();
                ioq.item_title = "item"+i.ToString();
                ioq.item_type = 0;
                ioq.item_image = "";
                ioq.item_grade = (Int16)i;
                itemlist.Add(ioq);
         
            }           
            qwi.question_title = "سوال اول";
            qwi.list_items.Add(itemlist);

 

کاربر سایت

mehdi1359a

عضویت از 1394/02/03

  • جمعه 11 دی 1394
  • 17:23

ممنون آقای مدائنی چه جوری در ویو ازش اسفاده کنیم در حلقه foreach خطا میده میگه اطلاعات مدل باید IEnumerable باشه

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • شنبه 12 دی 1394
  • 09:06

مدل صفحه را چگونه تعریف کردید

کدش را قرار دهید

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)