"不能在结构&QUOT实例字段初始化;问题 [英] "cannot have instance field initializers in structs" problem

查看:140
本文介绍了"不能在结构&QUOT实例字段初始化;问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要结构的建议。

我的代码2部分。第一部分是如下

I have 2 section of code. The first section is as below

namespace Project.GlobalVariables
{
    class IOCard
    {
        struct InputCard
        {
            public string CardNo;
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        static InputCard[] InputCards = new InputCard[5];

        public static string ACardNo = InputCards[1].CardNo;
        public static string BCardNo = InputCards[2].CardNo;

    }
}



的第二部分是如下:

The second portion is as below:

private void Form1_Load(object sender, EventArgs e)
    {
        IOCard.ACardNo = "Card A";
        IOCard.BCardNo = "Card B";

        MessageBox.Show(IOCard.ACardNo);
        MessageBox.Show(IOCard.BCardNo);
    }



我的计划是能够分配和使用IOCard作为检索InputCards成分在Form1_Load的显示。

My plan is to be able to assign and retrieve InputCards component by using IOCard as shown in Form1_Load.

然而,当我编译代码时,我得到了下面的错误。

However, when I compile the code, I get the following error.

"Error 1 'Project.GlobalVariables.IOCard.InputCard.WriteBitNo': cannot have instance field initializers in structs E:\Programming\New platform\StandardPlatform\StandardPlatform\Project\GlobalVariables.cs 16 26 StandardPlatform
"

我能知道如何解决这个错误?
请指点。谢谢你。

Can I know how to solve the error? Please advice. Thanks.

下面是我试图创建以及试图利用他们,但失败的类。

Here are the classes that i've attempt to create as well as tried to use them but failed.

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

namespace Project.GlobalVariables
{
    static class IOCard
    {
        public const int TotalInputCard = 10;
        public const int TotalOutputCard = 10;

        public  class InputCard
        {
            public string CardNo = "1";
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        public class OutputCard
        {
            public string CardNo;
            public int BaseAddress;
            public int LowerAddress;
            public int UpperAddress;
            public int[] WriteBitNo = new int[16];
            public int[] ReadBitNo = new int[16];
        }

        public static InputCard[] InputCards = new InputCard[TotalInputCard];
        public static OutputCard[] OutputCards = new OutputCard[TotalOutputCard];

        public static int X100 = InputCards[0].WriteBitNo[0];
        public static int Y100 = OutputCards[0].WriteBitNo[0];
    }
}

和我试图用它在Form_Load像这样

And I tried to use it in the Form_Load like so

private void Form1_Load(object sender, EventArgs e)
{
    IOCard.X100 = 1;
    IOCard.Y100 = 1;
} 



不管我怎样试图在网上搜索答案,我得到没有在那里。
请指点亚..
感谢。

No matter how much i tried to search on the net for answers, i get no where. Please advice ya.. Thanks.

推荐答案

什么是它想说的是,当你有 InputCards =新InputCard [5]; 将分配内存5倍 InputCard 结构的大小块,它的所有字节设置为0,没有机会执行 INT [] WriteBitNo =新的INT [16]; 键,这样的任务,所以你不能让他们

What's it's trying to say is that when you have InputCards = new InputCard[5]; it will allocate a block of memory 5 times the size of an InputCard structure and set all of its bytes to 0. There is no opportunity to execute the int[] WriteBitNo = new int[16]; and such assignments, so you cannot have them.

您的选择是要么手动调用一个初始化为你的结构或使其类和手动初始化 InputCards 阵列与 InputCard 的5个新的实例。

Your options are to either manually call an initializer for your structs or make it a class and manually initialize the InputCards array with 5 new instances of InputCard.

这篇关于"不能在结构&QUOT实例字段初始化;问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆