如何调用引用C#中对象的函数?

时间:2022-10-20 22:29:50

this is probably a really dumb question but i couldn't really find an answer anywhere

这可能是一个非常愚蠢的问题,但我无法在任何地方找到答案

Basically I have a function:

基本上我有一个功能:

public int cardCheck(ref PictureBox userCard1, ref PictureBox userCard2, ref PictureBox userCard3, ref PictureBox dealerCard1, ref PictureBox dealerCard2, ref PictureBox dealerCard3, int card, int cardNumber)

and I need to call upon that function. If I call that function like this:

我需要调用这个功能。如果我这样调用这个函数:

uCard1 = cardCheck(card, cardNumber);

it tells me that I need references to those picture boxes. However, when I add them in like so:

它告诉我,我需要参考那些图片框。但是,当我这样添加它们时:

uCard1 = cardCheck(PictureBox userCard1, PictureBox userCard2, PictureBox userCard3, PictureBox dealerCard1, PictureBox dealerCard2, PictureBox dealerCard3, card, cardNumber);

it tells me that PictureBox is a type, which can't be used in that context.

它告诉我PictureBox是一种类型,不能在该上下文中使用。

I'm sure I'm making a really dumb syntax mistake, but I'm not sure where. How do you correctly call upon a function that references objects in your program?

我确定我犯了一个非常愚蠢的语法错误,但我不知道在哪里。如何正确调用引用程序中对象的函数?

1 个解决方案

#1


0  

Based just on your second line of code, what you're missing is something like this:

仅基于您的第二行代码,您缺少的是这样的:

uCard1 = cardCheck(ref card, ref cardNumber);

uCard1 = cardCheck(参考卡,参考卡号);

In C# when the parameter is labeled ref in the declaration, you have to add ref at the call site, as well. Similarly for out.

在C#中,当参数在声明中标记为ref时,您还必须在呼叫站点添加ref。同样出来。

But specifically, based on the declaration for cardCheck that you showed us, you probably need all of the params:

但具体来说,根据您向我们展示的cardCheck声明,您可能需要所有参数:

int uCard1 = cardCheck(ref userCard1, ref userCard2, ref userCard3, ref dealerCard1, ref dealerCard2, ref dealerCard3, card, cardNumber);

int uCard1 = cardCheck(ref userCard1,ref userCard2,ref userCard3,ref dealerCard1,ref dealerCard2,ref dealerCard3,card,cardNumber);

ref means that you're passing in an object, and if the function you're calling modifies it then you will get back that modified version.

ref意味着你传入一个对象,如果你正在调用的函数修改它,那么你将获得修改后的版本。

You must also have defined your own

您还必须定义自己的

PictureBox userCard1 = new PictureBox(blah blah);
PictureBox userCard2 = new PictureBox(blah blah);
PictureBox userCard3 = new PictureBox(blah blah);
PictureBox dealerCard1 = new PictureBox(blah blah);
PictureBox dealerCard2 = new PictureBox(blah blah);
PictureBox dealerCard3 = new PictureBox(blah blah);
int card = 0;
int cardNumber = 0;

Something like that...

这样的东西......

Of course you can call your local variables whatever you want to. And the function you're calling, cardCheck is what determines what will happen with all of those variables, and all of those values. You have to make all of that make sense - we don't know the semantics of the code you're quoting to us.

当然,您可以随意调用本地变量。而你正在调用的函数,cardCheck决定了所有这些变量会发生什么,以及所有这些值。你必须使所有这些都有意义 - 我们不知道你引用给我们的代码的语义。

#1


0  

Based just on your second line of code, what you're missing is something like this:

仅基于您的第二行代码,您缺少的是这样的:

uCard1 = cardCheck(ref card, ref cardNumber);

uCard1 = cardCheck(参考卡,参考卡号);

In C# when the parameter is labeled ref in the declaration, you have to add ref at the call site, as well. Similarly for out.

在C#中,当参数在声明中标记为ref时,您还必须在呼叫站点添加ref。同样出来。

But specifically, based on the declaration for cardCheck that you showed us, you probably need all of the params:

但具体来说,根据您向我们展示的cardCheck声明,您可能需要所有参数:

int uCard1 = cardCheck(ref userCard1, ref userCard2, ref userCard3, ref dealerCard1, ref dealerCard2, ref dealerCard3, card, cardNumber);

int uCard1 = cardCheck(ref userCard1,ref userCard2,ref userCard3,ref dealerCard1,ref dealerCard2,ref dealerCard3,card,cardNumber);

ref means that you're passing in an object, and if the function you're calling modifies it then you will get back that modified version.

ref意味着你传入一个对象,如果你正在调用的函数修改它,那么你将获得修改后的版本。

You must also have defined your own

您还必须定义自己的

PictureBox userCard1 = new PictureBox(blah blah);
PictureBox userCard2 = new PictureBox(blah blah);
PictureBox userCard3 = new PictureBox(blah blah);
PictureBox dealerCard1 = new PictureBox(blah blah);
PictureBox dealerCard2 = new PictureBox(blah blah);
PictureBox dealerCard3 = new PictureBox(blah blah);
int card = 0;
int cardNumber = 0;

Something like that...

这样的东西......

Of course you can call your local variables whatever you want to. And the function you're calling, cardCheck is what determines what will happen with all of those variables, and all of those values. You have to make all of that make sense - we don't know the semantics of the code you're quoting to us.

当然,您可以随意调用本地变量。而你正在调用的函数,cardCheck决定了所有这些变量会发生什么,以及所有这些值。你必须使所有这些都有意义 - 我们不知道你引用给我们的代码的语义。