none
Inconsistent accessibility happened when I encapsulate the composited object in a bigger class RRS feed

  • 问题

  • When I encapsulate the composited object(a private member) in a bigger class,this error was informed of.

    It said:"Error 1 Inconsistent accessibility: property type 'DND_Tool_Bar.MainWindow.items' is less accessible than property 'DND_Tool_Bar.MainWindow.charactor.Myitem' C:\Users\FurukawaMu\Documents\WorkSpace\DND Tool Bar\DND Tool Bar\MainWindow.xaml.cs 94 26 DND Tool Bar

    "

    May be encapsulation of the object can't be so simple like that?

    Here is my code:

    the place where is seen as error is underlined.

    Thanks for any assistant!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace DND_Tool_Bar
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                
            }

            class items
            {
                int sword;

                public int Sword
                {
                    get { return sword; }
                    set { sword = value; }
                }

                int arrow;

                public int Arrow
                {
                    get { return arrow; }
                    set { arrow = value; }
                }

                int spear;

                public int Spear
                {
                    get { return spear; }
                    set { spear = value; }
                }

                int total;

                public int Total
                {
                    get
                    {
                        total = sword * 800 + arrow * 10 + spear * 600;
                        return total;
                    }
                    set { total = value; }
                }




            }

            public class charactor
            {
                public charactor()
                {
                    items myitem = new items();
                    myitem.Sword = 0;
                    myitem.Spear = 0;
                    myitem.Arrow = 0;
                    myitem.Total = 0;
                }

                //this is a test construction,this one is of no use when the program is actually running

                public charactor(int numofsword)
                {
                    myitem = new items();
                    myitem.Sword = numofsword;
                    myitem.Total = myitem.Sword * 800;
                }

                items myitem;

                public items Myitem
                {
                    get { return myitem; }
                }

                
            }
            charactor present = new charactor();
            

            private void button5_Click(object sender, RoutedEventArgs e)
            {
                present.Myitem.Sword++;
                TextBox1.Text = present.Myitem.Total.ToString();
            }


            
        }
    }

    2010年8月4日 19:59

答案

  • the accessible modifier of items class should be public(if not explicitly declaring, it's internal by default), because the charactor class is public and its public property, Myitem, references to the items class. that's say, all of the three should be consitent.

    Otherwise, you can change the accessible modifier of charactor to internal.


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net
    • 已标记为答案 FurukawaMu 2010年8月5日 4:08
    2010年8月5日 2:17