繁体   English   中英

从静态类调用方法到部分类中以随机播放列表

[英]Calling a method from Static class into a partial class to shuffle a list

使用C#,我可以从完成程序的主要功能中获得很多代码,但是遇到了麻烦,并且搜索了数小时后该怎么做。

我的程序是用局部类编写的。 我想改组一个列表,一个图像列表,但是我似乎无法从我的局部类中调用静态方法? 我从“ 将List <T>随机化”中挖出了方法,这正是我想要做的。 您可能会注意到我有一个shuffle方法,但是该方法可以对数组而不是列表进行随机排序。 我已添加所有我的代码,并且只扩展了感兴趣的方法。 我尝试在内部使用shuffle方法创建一个新类,但是如果在我的局部类中没有运气,我希望这不是不必要的代码。

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;
using System.Threading;

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

        //generate random number
        Random r = new Random();
        //int for matching panels loop in matching method
        int v = 0;
        //Counts how many times the music stop/start buttin has been clicked
        int musicCount = 0;
        //timer counter for starting and stopping timer
        int time = 1;
        //First picture to be compared against next clicked
        int myPBox1;
        //Second picture to be compared against first
        int myPBox2;
        //counts the number of clicks events occured during play
        int mouseClicks = 0;
        //create image list for easy
        List<Image> imagesEasy;
        //create image list for hard1
        List<Image> imagesHard1;
        //create image list for hard2
        List<Image> imagesHard2;
        //List for final images picked to be used in the pictureBoxes on easy mode
        List<Image> imagesFinalEasy;
        //List for final images picked to be used in the pictureBoxes hard mode
        List<Image> imagesFinalHard;
        //create array to pick numbers 1-54 for the images to be chosen for the game
        int[] picSlotArray = new int[8];
        //Array to hold what pictures the user has mached
        int[] picsMatched = new int[16];
        //an array for the eight pictures already picked to be stored but now shuffeled in an array
        int[] randArrayEightEasy = new int[8];
        //an array for the eight pictures already picked to be stored but now shuffeled in an array
        int[] randArrayEightHard = new int[8];
        //array containing all panels that match
        int[] matchedPanels = new int[16];
        //the pics picked for easy doubled up twice into one array
        int[] randArraySixteenEasy = new int[16];
        //the pics picked for hard doubles up twice into one array
        int[] randArraySixteenHard = new int[16];
        //random array of 16 from 0-15 including 0 & 15
        int[] randArray1616 = new int[16];
        //Array for pictureBoxes
        PictureBox[] myPics = new PictureBox[16];
        //Array for panels
        Panel[] myPanels = new Panel[16];

        private void Form2_Load(object sender, EventArgs e)...

        private void InitialiseMyPics()...

        private void InitialisePanels()...

        private void Theme()...

        private void ImageListEasy()...

        private void ImageListHard1()...

        private void ImageListHard2()...

        private void ImageListEasyFinal()...

        private void ImageListHardFinal()
        {
            imagesFinalHard = new List<Image>();
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[0])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[1])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[2])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[3])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[4])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[5])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[6])]);
            imagesFinalHard.Add(imagesHard1[(randArraySixteenHard[7])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[0])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[1])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[2])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[3])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[4])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[5])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[6])]);
            imagesFinalHard.Add(imagesHard2[(randArraySixteenHard[7])]);
        }

        private void Randoms()...

        private void ShuffleEasy()...

        //need to some how shuffle the hardFinalList
        private void ShuffleHard()...

        private void SetImagesEasy()...

        private void SetImagesHard()...

        private void ArrayEightTwiceEasy()...

        private void ArrayEightTwiceHard()...

        private void Tags()...

        private void TagsHard1()...

        private void TagsHard2()...

        private void Names1()...

        private void Names2()...

        private void Compare()...

        private void MatchedPics()...

        private void CheckForFinished()...

        private void HidePanels()...

        private void ShowPanels()...

        private void DisableAllPanles()...

        private void EnablePanels()...
        {
        private void HidePictureBoxes()...

        private void RevealPictureBoxes()...

        private void ShowPics()...

        private void panel2_Click_1(object sender, EventArgs e)...

        private void button6_Click(object sender, EventArgs e)...

        private void button7_Click(object sender, EventArgs e)...

        private void New_Game_Click(object sender, EventArgs e)...

        private void timer1_Tick_1(object sender, EventArgs e)...

        private void Music_Stop_Click(object sender, EventArgs e)...

        private void button1_Click(object sender, EventArgs e)
        {
            ImageListHard1();
            ImageListHard2();
            Randoms();
            ArrayEightTwiceHard();
            ShuffleHard();
            TagsHard1();
            ImageListHardFinal();
            //Shuffle Needs to be called here when button is clicked
            SetImagesHard();
            HidePictureBoxes();

        }
    }
}

没有理由您无法从partial类中调用static方法。 这里有一些要检查的东西:

  • 静态方法是在您的类中声明的吗? 如果不是,则必须指定其所在类的名称。 例如, Shuffler.Shuffle()
  • 如果该方法在另一个类中,请确保该类和方法是可访问的-如果无法访问,则将类和方法设置为public
  • 确保以正确的顺序传递正确的参数类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM