我正在尝试将值(来自对象)放在变量中,并将其放在表单的文本框中。
以下是表单代码:
public Form1(Deck mainDeck)
{
InitializeComponent();
int Val = mainDeck.ReturnCard(10);
textBox1.Text = Val.ToString();
}
mainDeck是我的Program.cs文件中的一个对象
问题在于这一行:int Val = mainDeck.ReturnCard(10);
哎呀,错误的错误。 这是真实的:
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
这是我的Program.cs文件:
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));
}
}
}
编辑:好的,我正在将mainDeck传递给Form,但我现在遇到了一个不同的错误:
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
编辑:编辑:好吧,我现在有这个工作,但我遇到了一个相关的问题,所以我宁愿把它放在这里而不是一个新的问题。
将mainDeck传递给Form1可以正常工作,但是如果我想要一个按钮单击,则调用此对象中的方法。我尝试过像这样传递mainDeck:
private void button1_Click(object sender, Deck mainDeck, EventArgs e)
{
mainDeck.Shuffle();
}
我收到此错误:
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!
答案 0 :(得分:7)
最简单的方法是更改表单的构造函数以传入mainDeck。
public Form1(Deck mainDeck)
{
InitializeComponent();
int Val = mainDeck.ReturnCard(10);
textBox1.Text = Val.ToString();
}
如果您需要在表单代码的其他部分引用它,您可以保留一个私有成员变量...
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();
}
然后在你的Program.cs文件中......
Application.Run(new Form1(mainDeck));
编辑:为了回应涉及 Deck 类可访问性的新错误,这是因为您的Deck类不公开。将 public 关键字添加到类声明中,以便:
namespace Pcard
{
public class Deck
{
// and so on ...
请注意,将Deck类保存在单独的.cs文件中可能会更清晰,例如Deck.cs。
答案 1 :(得分:3)
这条线 Deck mainDeck = new Deck(); 只需创建一个名为mainDeck的局部变量...只能在Main()方法中访问...
将声明置于类中的Main方法之上...将程序类设为public,将maindeck设为public static ...就像这样
public class Program
{
// Create a Deck object
public static Deck mainDeck = new Deck();
static void Main(string[] args)
{
.....
}
然后您可以从表单中访问
int Val = Program.mainDeck.ReturnCard(10);
希望这有帮助。
答案 2 :(得分:1)
假设mainDeck是静态的,并且是公共的或内部的,您可以通过
访问它int val = Program.mainDeck.ReturnCard(10);
答案 3 :(得分:1)
问题在于您尝试将mainDeck
称为全局变量,而C#中不允许这样做。
您获得的错误是因为编译器假定mainDeck
是您尝试引用的类型,并且没有mainDeck
的定义。
显然这不是你想要的。要解决此问题,您必须使用mainDeck类中的静态变量使mainDeck
可以使用该引用,或者使用Singleton模式来解决问题。
修改强>
添加代码后,将mainDeck
引用传递给“Form1”可能是最简单的。作为Form1的构造函数的一部分或可以设置的属性。