Wątek przeniesiony 2015-02-23 09:55 z C# i .NET przez somekind.

Błąd przy listach w Visual C#

0

Witam, mam problem z listami w Visual c#. Pojawia się kilka błędów, a mianowicie:

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;


namespace Program
{
    public class Strona
    {
        public int Id { get; set; }
        public string Adres { get; set; }

        public Strona (int nId, string nAdres) 
        {
            this.Id = nId;
            this.Adres = nAdres;
        }
    }


    public partial class program : Form
    {
        public program()
        {
            InitializeComponent();
        }

        List<Strona> SiteList = new List<Strona>();
        Strona nowy = new Strona(1, "qqq");
        SiteList.Add(nowy); //1. Invalid token '(' in class, struct, or interface member declaration; 2. Invalid token ')' in class, struct, or interface member declaration; 3. 'Program.program.SiteList' is a 'field' but is used like a 'type'; 4. The type or namespace name 'now' could not be found (are you missing a using directive or an assembly reference?)	

    }
}

Wszystko robiłem wg. poradnika, nie mam pojęcia z czym jest problem.

1

Umieść ten kod w jakiejś metodzie a nie bezpośrednio w klasie np:

     public partial class program : Form
    {
        public program()
        {
            InitializeComponent();
            NowaStrona();
        }
 
        List<Strona> SiteList = new List<Strona>();
        
        void NowaStrona()
        {
            Strona nowy = new Strona(1, "qqq");
            SiteList.Add(nowy);
        }
    }
1

albo tak:

List<Strona> SiteList = new List<Strona>
{
    new Strona(1, "qqq"),
    new Strona(2, "aaa")
};

albo nawet tak:

public class StronaList : List<Strona>
{
    public void Add(int nId, string nAdres)
    {
        Add(new Strona(nId, nAdres));
    }
}

List<Strona> SiteList = new StronaList
{
    {1, "qqq"},
    {2, "aaa"}
};

1 użytkowników online, w tym zalogowanych: 0, gości: 1