无法通过类分配变量 [英] Could not assigne varible through a class

查看:86
本文介绍了无法通过类分配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Form1.cs中,我声明了类 MyRandomRW 的实例的2个数组,它们分别是Record和RecordTmp
该项目还有另一个Form是InsertForm.cs,我想分配一个字符串"123456789012345"
到变量Form1.RecordTmp [0] .NO,然后在Form1.cs中我要分配
Record [0] .NO = RecordTmp [0] .NO,我不能这样做
编译器写了
错误1可访问性不一致:字段类型"Wared.MyRandomRW []"难以访问
比字段"Wared.Form1.RecordTmp" G:\ Wared \ Wared \ Form1.cs 16 36
警告

In Form1.cs i declared 2 array of instance of class MyRandomRW those are Record and RecordTmp
The project has another Form which is InsertForm.cs ,i want to assigne a string "123456789012345"
to variable Form1.RecordTmp[0].NO, then in Form1.cs i want to assigne
Record[0].NO=RecordTmp[0].NO , i could not do that
the compiler wrote
Error 1 Inconsistent accessibility: field type ''Wared.MyRandomRW[]'' is less accessible
than field ''Wared.Form1.RecordTmp'' G:\Wared\Wared\Form1.cs 16 36
Wared

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.IO;

namespace Wared
{
    public partial class Form1 : Form
    {
        MyRandomRW[] Record = new MyRandomRW[8000];
        public static MyRandomRW[] RecordTmp=new MyRandomRW[100];

        public Form1()
        {
            InitializeComponent();
            MyInsert();
        }
        
         private void Form1Load(object sender, EventArgs e)//<--------Line C
        {
            int i;
            for (i = 0; i < 8000; i++) Record[i].NO = "0";
            for (i = 0; i < 100; i++) RecordTmp[i].NO = "0";
        }
        private void MyInsert()
        {   
            InsertForm InsertFrm = new InsertForm();
            InsertFrm.ShowDialog();
            Record[0].NO             =  RecordTmp[0].NO;//<---- Error
        }//Insert_Click

    }
}


//--------


//-------

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;

namespace Wared
{
    public partial class InsertForm : Form //<---line A
    {
        public InsertForm()
        {
            InitializeComponent();
        }

        public void InsertFormLoad(object sender, EventArgs e)
        //private void InsertFormLoad(object sender, EventArgs e)
        {
            Form1.RecordTmp[0].NO = "123456789012345";//<---- line 21
        }


    }
}


//------------班级


//------------ the class

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

namespace Wared
{
    //class MyRandomRW      //<----line B
    public class MyRandomRW  //<--- Line B2
    {
        private const int CHAR_ARRAY_LENGTH = 15;
        private const int SIZE_OF_CHAR = 2;
        public const int SIZE = (SIZE_OF_CHAR * CHAR_ARRAY_LENGTH);
        public char [] No=new char[CHAR_ARRAY_LENGTH];

         public MyRandomRW()   : this("")
        {
        }

        public  MyRandomRW(string No)
        {
            NO=No;
        }

        public string NO
        {   get{return new string (No);}
            set
            {
                int stringSize = value.Length;
                string NameString = value;

                if(CHAR_ARRAY_LENGTH >= stringSize)
                {
                    NameString = value +
                    new string(' ', CHAR_ARRAY_LENGTH - stringSize);
                }
                else
                {
                    NameString =
                    value.Substring(0, CHAR_ARRAY_LENGTH);
                }
                No = NameString.ToCharArray();
            } // end set
        }

    }
}

推荐答案

那是因为您有一个公开私有类型的公共字段.
将您的MyRandomRW类设为公开或将您的字段设为私有


您将要遇到的大多数错误都可以很轻松地在Google上取得成功,并且应该大多会带来良好的结果.
字段类型"比字段更难访问" [ ^ ]
that''s because you have a public field that exposes a private type.
make your MyRandomRW class public or your field private


Most errors you''re going to encounter can be google with success quite easely and should mostly give good results.
"field type" "less accessible than field"[^]


这篇关于无法通过类分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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