C#: comment avez-vous reportez-vous au contenu du Programme.cs dans un formulaire.cs?

Je suis en train de placer une valeur (d'un objet) dans une variable et de le placer dans une zone de texte dans un formulaire.

Voici le code du formulaire:

public Form1(Deck mainDeck)
    {
        InitializeComponent();
        int Val = mainDeck.ReturnCard(10);
        textBox1.Text = Val.ToString();
    }

mainDeck est un objet dans mon Programme.cs fichier

Le problème est le suivant: int Val = mainDeck.ReturnCard(10);

Oups, erreur incorrect.
C'est le réel:

Error 1 The name 'mainDeck' does not exist in the current context C:\Users\Chris\Documents\Visual Studio 2008\Projects\Pcard\Pcard\Form1.cs 17 23 Pcard

Voici mon Programme.cs fichier:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Pcard
{
class Deck
{
public Deck()
{
//Assign standard deck to new deck object
//  int j;
for (int i = 0; i != currentDeck.Length; i++)
{
currentDeck[i] = originalCards[i];
}
//Fisher-Yates Shuffling Algorithim    ---   Do initial shuffle
Random rnd = new Random();
for (int k = currentDeck.Length - 1; k >= 0; k--)
{
int r = rnd.Next(0, k + 1);
int tmp = currentDeck[k];
currentDeck[k] = currentDeck[r];
currentDeck[r] = tmp;
}
}
public void Shuffle()
{
Random rnd = new Random();
for (int k = currentDeck.Length - 1; k >= 0; k--)
{
int r = rnd.Next(0, k + 1);
int tmp = currentDeck[k];
currentDeck[k] = currentDeck[r];
currentDeck[r] = tmp;
}
}
public int[] ReturnDeck()
{
return currentDeck;
}
public int ReturnCard(int num)
{
return currentDeck[num];
}
public int[] originalCards = new int[54]
{
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D,
0x50, 0x51
};
private int[] currentDeck = new int[54];
}
public class Program
{
static void Main(string[] args)
{
//Create a Deck object
Deck mainDeck = new Deck();
Console.WriteLine("Here is the card array:");
for (int index = 0; index != 54; index++)
{
string card = mainDeck.ReturnCard(index).ToString("x");
Console.WriteLine("0x" + card);
}
//Return 10th Card
int PickCard = mainDeck.ReturnCard(10);
Console.WriteLine("Here is the 10th Card");
Console.WriteLine("0x" + PickCard);
//Shuffle
mainDeck.Shuffle();
Console.WriteLine("Shuffling..");
PickCard = mainDeck.ReturnCard(10);
Console.WriteLine("Here is the 10th card now: 0x" + PickCard);
Application.Run(new Form1(maindeck));
}
}
}

Modifier: Ok, je suis de passage mainDeck pour la Forme, mais je suis maintenant une autre erreur:
Error 1 Inconsistent accessibility: parameter type 'Pcard.Deck' is less accessible than method 'Pcard.Form1.Form1(Pcard.Deck)' C:\Users\Chris\Documents\Visual Studio 2008\Projects\Pcard\Pcard\Form1.cs 14 16 Pcard

Edit: Edit: Bien, j'ai maintenant ce travail, mais je suis tombé sur un problème connexe, donc je préfère le mettre ici que dans une nouvelle question.

Passant mainDeck à la Form1 fonctionne très bien, mais que si je veux avoir un clic sur un bouton d'appel d'une méthode de cet objet. J'ai essayé en passant mainDeck comme ceci:

 private void button1_Click(object sender, Deck mainDeck, EventArgs e)
{
mainDeck.Shuffle();
}

J'obtiens cette erreur:

Error 1 No overload for 'button1_Click' matches delegate 'System.EventHandler' C:\Users\Chris\Documents\Visual Studio 2008\Projects\Pcard\Pcard\Form1.Designer.cs 51 35 Pcard

GRR!

qui ligne et quel est le message complet.
int Val = mainDeck.ReturnCard(10)
Est pont principal défini quelque part dans votre projet
Oui, voir le post (Édité).
Juste pensé que je ferais remarquer que vous pourriez gérer la button1_Click question en gardant une poignée pour une variable membre privée appelée "_mainDeck" (par exemple) dans votre Form1.cs. Cependant, l'utilisation d'une variable statique de Programme.cs, comme l'a souligné par Le Roi, est aussi une approche adaptée.

OriginalL'auteur Biosci3c | 2010-03-10