簡體   English   中英

每當我嘗試使用我的課程時,都會出現錯誤

[英]Whenever I try to use my class, I get an error

我正在嘗試編寫一個使用預制列表生成侮辱的程序。 當我嘗試從類mStart,mMid或mEnd更改某些值時,出現錯誤。 我究竟做錯了什么? 我是編碼新手,所以如果我犯了一個愚蠢的錯誤,請通知我。

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

namespace albertisnultsim
{
class Program
{
    public class startSent
    {
        public string[] listofstarters = new string[10];
    }

    public  class middle
    {
        public string[] listofmiddles = new string[10];
    }

    public class end
    {
        public string[] listofends = new string[10];
    }

    startSent mstart = new startSent();
    middle mMid = new middle();
    end mEnd = new end();

    static void Main(string[] args)
    {
    }
}
}

這就是我想要的:

namespace albertisnultsim
{
    class Program
    {
        public class startSent
        {
            public string[] listofstarters = new string[10];

        }

        public class middle
        {
            public string[] listofmiddles = new string[10];

        }
        public class end
        {
            public string[] listofends = new string[10];

        }


        static void Main(string[] args)
        {
            startSent mstart = new startSent();
            middle mMid = new middle();
            end mEnd = new end();

            mstart.listofstarters = new string[]{"setting1","1","setting2","apple"};
            Console.WriteLine(mstart.listofstarters[0]);
            Console.ReadKey();
        }
    }
}

這些類的實例必須在除名稱空間以外的特定范圍內創建。 由於這三個類都具有相同的有效屬性,因此您也可以使用該事實來大大簡化代碼:

class Program
{
    public class Section
    {
        public string[] items = new string[10];
    }

    static void Main(string[] args)
    {
        Section start = new Section();
        Section middle = new Section();
        Section end = new Section();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM