簡體   English   中英

可訪問性不一致:參數的可訪問性比方法* LINE 35 *少

[英]Inconsistent accessibility: parameter is less accessible than method *LINE 35*

不一致的可訪問性:參數無法比方法LINE 35更好地訪問我一生無法弄清楚出了什么問題,我覺得應該公開的東西是私有的,但是我找不到。

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

namespace Hangman
{
    public partial class Hangman : Form
    {
        public Hangman()
        {
            InitializeComponent();
        }

        private string word = string.Empty;
        List<Label> labels = new List<Label>();
        private int amount;

        enum BodyParts
        {
            Head,
            Body,
            Right_Arm,
            Left_Arm,
            Right_Leg,
            Left_Leg
        }

        void DrawBodyPart(BodyParts bp)
        {
            Graphics g = manPanel.CreateGraphics();
            Pen p = new Pen(Color.Blue, 2);

            if(bp == BodyParts.Head)
            {
                g.DrawEllipse(p, 40, 50, 40, 40);
            }
            else if (bp == BodyParts.Body)
            {
                g.DrawLine(p, new Point(60, 90), new Point(60, 170));
            }
            else if (bp == BodyParts.Right_Arm)
            {
                g.DrawLine(p, new Point(60, 100), new Point(90, 85));
            }
            else if (bp == BodyParts.Left_Arm)
            {
                g.DrawLine(p, new Point(60, 100), new Point(30, 85));
            }
            else if (bp == BodyParts.Right_Leg)
            {
                g.DrawLine(p, new Point(60, 170), new Point(90, 190));
            }
            else if (bp == BodyParts.Left_Leg)
            {
                g.DrawLine(p, new Point(60, 170), new Point(30, 190));
            }
        }

        void DrawHangPost()
        {
            Graphics g = manPanel.CreateGraphics();
            Pen p = new Pen(Color.Brown, 10);

            g.DrawLine(p, new Point(130, 218), new Point(130, 5));
            g.DrawLine(p, new Point(135, 5), new Point(65, 5));
            g.DrawLine(p, new Point(60, 0), new Point(60, 50));
        }

        void MakeLabels()
        {
            word = GetRandomWord();
            char[] chars = word.ToCharArray();
            int between = 330 / chars.Length - 1;

            for (int i = 0; i < chars.Length - 1; i++ )
            {
                labels.Add(new Label());
                labels[i].Location = new Point((i * between) + 10, 80);
                labels[i].Text = "_";
                labels[i].Parent = labelsGroupBox;
                labels[i].BringToFront();
                labels[i].CreateControl();
            }

            label1.Text += (chars.Length - 1).ToString();
        }

        string GetRandomWord()
        {
            WebClient wc = new WebClient();
            string wordlist = wc.DownloadString("http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt");
            string[] words = wordlist.Split('\n');
            Random rand = new Random();
            return words[rand.Next(0, words.Length - 1)];
        }

        void ResetGame()
        {
            Graphics g = manPanel.CreateGraphics();
            g.Clear(manPanel.BackColor);
            GetRandomWord();
            MakeLabels();
            DrawHangPost();
            missedLabel.Text = "";
            guessesLabel.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DrawHangPost();
            MakeLabels();

            Button[] buttons = new Button[] { buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, 
            buttonG, buttonH, buttonI, buttonJ, buttonK, buttonL, buttonM, buttonN, buttonO, buttonP, 
            buttonQ, buttonR, buttonS, buttonT, buttonU, buttonV, buttonW, buttonX, buttonY, buttonZ, };
            foreach (Button btn in buttons)
            {
                //common code for all the buttons:
                btn.Click += new EventHandler(buttons_Click);
            }
        }

        private void buttons_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            char letter = button.Text.ToLower().ToCharArray()[0];

          /*  if (!char.IsLetter(letter))
            {
                button.Enabled = true;
                MessageBox.Show("Only letters.");
                return;
            } */
            if (word.Contains(letter))
            {
                char[] letters = word.ToCharArray();

                for (int i = 0; i < letters.Length; i++)
                {
                    if (letters[i] == letter)
                    {
                        labels[i].Text = letter.ToString();
                    }
                }

                foreach(Label label1 in labels)
                {
                    if (label1.Text == "_") return;
                    MessageBox.Show("You Won!");
                    ResetGame();
                }
            }
            else
            {
                missedLabel.Text += letter.ToString() + ", ";
                DrawBodyPart((BodyParts)amount);
                amount++;
                if (amount == 6)
                {
                    MessageBox.Show("You lost. The word was: " + word);
                    ResetGame();
                }
            }
        }
    }
}

如果您未提供方法簽名默認值的訪問修飾符,它將是私有的,因此,如果您嘗試在類外部訪問,則會顯示可訪問性錯誤。.您需要將訪問修飾符從默認更改為公共。

注意:如果您試圖在類中調用此方法,那將不是問題,否則您需要更改訪問修飾符!

暫無
暫無

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

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