C #: how do you feel about the content in Program.cs in form.cs?

I am trying to put a value (from an object) into a variable and put it in a text box on the form.

Here is the form code:

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

mainDeck is the object in the Program.cs file

The problem is this: int Val = mainDeck.ReturnCard (10);

Error, wrong error. This is the real option:

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

Here is my Program.cs file:

 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)); } } } 

Edit : Ok, I submit mainDeck to the form, but now I have a different error: 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: Ok, now this works for me, but I ran into a related problem, so I would rather post it here than in the new question.

Passing mainDeck to Form1 works fine, but what about if I want to click, call the method on that object. I tried to skip mainDeck like this:

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

I get this error:
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!

+4
source share
4 answers

Line Deck mainDeck = new deck (); just creates a local variable called mainDeck ... which is only available in the Main () method ...

Put the Declaration above the Main method in the class ... Make the program class public, and maindeck - static public ... like this

 public class Program { // Create a Deck object public static Deck mainDeck = new Deck(); static void Main(string[] args) { ..... } 

Then you can access your form as

  int Val = Program.mainDeck.ReturnCard(10); 

Hope this helps.

+3
source

The easiest way is to change the constructor of the form to submit to mainDeck.

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

If you need to refer to it in other parts of the code of your form, you can save the private member variable lying around ...

 private Deck _mainDeck; public Form1(Deck mainDeck) { InitializeComponent(); _mainDeck = mainDeck; int Val = _mainDeck.ReturnCard(10); textBox1.Text = Val.ToString(); } } private void SomeOtherMethod() { int blah = _mainDeck.Blah(); } 

And then in the Program.cs file ...

 Application.Run(new Form1(mainDeck)); 

Edit: In response to a new error related to the availability of the Deck class, this is because your Deck class is not public. Add the public keyword to the class declaration, therefore:

 namespace Pcard { public class Deck { // and so on ... 

Note that it might be cleaner to keep your Deck class in a separate .cs file, such as Deck.cs.

+7
source

Assuming mainDeck is static and public or internal, you can access it through

 int val = Program.mainDeck.ReturnCard(10); 
+1
source

The problem is that you are trying to reference mainDeck as a global variable that is not allowed in C #.

The error you get is that the compiler assumes that mainDeck is the type you are trying to reference and has no definition for mainDeck .

Obviously, this is not what you want. To solve the problem, you need to make the link accessible to mainDeck with a static variable from your mainDeck class, or perhaps use the Singleton template to solve the problem.

Edit: After you have added your code, the easiest way is to pass the mainDeck link to "Form1". Either as part of the Form1 constructor, or a property that can be set.

+1
source

Source: https://habr.com/ru/post/1303636/


All Articles