Lucy_H

Hi I am a beginner in C# and I am still learning and understanding simple C# language.

I have created my own class called Card. I have declared a local variable of Card named playCard. I have declared, assigned and used this variable in a method.

When a button is clicked on the form, I then want to use this assigned variable again. I cannot declare a new variable of the same name in the buttons click event as this assigns a new value to playCard. How can I use the assigned playCard variable (assigned in the method Private void PlayGame()) in my button click event

Lucy



Re: Visual C# General Accessing an assigned local variable

~rabin

If you have declared the playCard inside a function, then you cannot access that variable from the event handler. So you have to declare a class level variable for your purpose like:
Code Block

public class Card
{

private int playCard = 2;
//you can change the value of playCard variable from any function inside this class
private void PlayGame()
{
playCard = 4;//this will set the new value for playCard
}
private void button1_Click(object sender, EventArgs e)
{
//do something with playCard here
int myVar = playCard * 5; //here myVar is a local variable, you can't access this variable from outside this function, and if PlayGame() function has been called previously then playCard = 4 otherwise 2 as assigned in the definition.
}

}







Re: Visual C# General Accessing an assigned local variable

Lucy_H

Thank you for your quick reply.

I cannot assign playCard to its appropriate value just inside the class. In my method PlayGame() I have changed the text of a label to equal playCard. Can I get this text from the button click event and convert it back to a Card type As this is a type that I have created I do not know how or if the string can be converted

Lucy





Re: Visual C# General Accessing an assigned local variable

Lucy_H

Is there a way of calling a method but not calling the first line of the method

I have a generic List called PlayHand. I add to this list in the method PlayGame(). When the button is clicked I want to add a Card type to this list. But after the method PlayGame() has been called and the method has completed, the generic list PlayHand clears.

Can anyone help Im really stuck on this!

Lucy





Re: Visual C# General Accessing an assigned local variable

Zamial

Yea I think we can help but your missing a few of the more fundamental points from your knowledge. For one thing variable scope is absolutely crucial that you understand it.

But there's no contest here, if your wanting to learn something - good luck.

With regards to some of your questions.

I see from your posts a few questions. Let's take your first post.

You have something like

class ClassName

{

private void MethodName()

{

private variable playCard = new Card();

Card.PlaceOnTop();

}

private event Button1_Clicked(sender object, EventArgs e)

{

}

}

<--- The variables you create inside the method are only available inside the method. This helps keep resources used to a minimum. If you want a variable accessible to all methods, events in the class. You declare the variable in the class itself.

You can still assign it's values in the method and use the variables as it's assigned elsewhere. So above becomes

class ClassName

{

private variable playCard;

private void MethodName()

{

playCard = new Card();

Card.PlaceOnTop();

}

private event Button1_Clicked(sender object, EventArgs e)

{

}

}

Please note in above example we have moved the declaration of the variable only. This is no available to all methods and events and will carry it's value with it until it goes out of scope. Which will be when the class itself is disposed of.

So now you should protect yourself because you can potentially access the variable when it does not have values you expect. It starts getting complicated if lots of methods and events are playing with this variable. But anyway you must have your variable declared in the class to use it amongst methods.

If your making a web app this is even harder to achieve as variables are lost on post backs.

POST 2.

You do not have to worry, please remember that you can assign the variable in your playgame method even still. All you are doing is declaring the variable as a class variable, where you assign and use is up to you.

POST 3.

Love it Smile. Call a method and omit the first line. There is and there isn't. You can write a way to do this but you would never want to in my opinion. You could pass a boolean flag to your method like

private void Method(bool runLine1)

{

if(runLine1 == true)

{

//Line 1 is here

}

// rest of code here

}

You would never want to do this. Now with regards to the list being clear. I am going to presume the list is declared and assigned in the method and as such is destroyed when the method finishes. Again you want a class level list.

Let us know if the above assumption is correct.

Where to declare a variable is an important part of the learning process extremely necessary. If this doesn't make sense then I should explain it better. Let us know how it goes.





Re: Visual C# General Accessing an assigned local variable

Lucy_H

Thank you for your help. It was very helpful. The problem I was having was exactly how you clearly explained but slightly more complicated. In my method PlayGame() I add players and then add cards to each player and display the collection of cards for the current player. Then for each player I show another card and wait for the user to click a button if they want to add this card or they click another button if they want to take then next random card.Which ever card they take, I then wanted to display their collection of cards. The user was then allowed to pick a card to throw away and the cycle starts again but without adding a new collection of cards to players.

I have figured out a solution to this complicated application. When I reach the section in the method where I want the user to choose to take the current shown card or to take a new card, I show a new form as a dialog. If dialogResult is Yes then take the current card else if No is clicked then take the next drawn card. When the user is asked which card they would like to discard, form3 is shown as a dialog. On this form there is a combo box with the items of the listbox on from1, the user will select a item from the combo box and click OK. The selected index of the combobox is an integer and this integer will be the index that is removed from the players collection of cards.

I am now struggling slightly with showing all items from the listbox on form1 into the listbox of form2.

Lucy





Re: Visual C# General Accessing an assigned local variable

Zamial

Good to hear it helped Smile

I think I can see where your next problem might lie as well. But maybe not of course. I just remember the problems I used to have. Passing values between forms used to be a PITA.





Re: Visual C# General Accessing an assigned local variable

Lucy_H

Another problem has been solved. I have made public a string[] of the listbox items in form1 like this:

public string[] Items

{

get

{

string[] retval = new string[listBox2.Items.Count];

for (int ix = 0; ix < listBox2.Items.Count; ++ix)

{

retval[ix] = listBox2.Items[ix].ToString();

}

return retval;

}

}

I then add this range to the listbox in form2.

I am now having slight problems in organising the players. I have allowed the user to add items to another listbox which will be the players. I am not sure if this is the most effective way but I will solve this problem through trial and error.

Lucy





Re: Visual C# General Accessing an assigned local variable

Lucy_H

For each item in the listbox, I want to add as a player. So far I have this code but I know it's not right.

int numberOfPlayers = listBox1.Items.Count;

Player[] players = new Player[numberOfPlayers];

for (int p = 0; p < players.Length; p++)

{

string playerName = listBox1.Items.ToString();

players[p] = new Player(playerName);

}

For each current player I want to display their name in a label so I have the following code:

label1.Text = players[currentPlayer].Name.ToString() + "'s turn.";

But this displays as:

System.Windows.Forms.Label, Text: System.Windows.Forms.ListBox+ObjectCollection's turn.

How can I add the items in the listbox as players

Lucy





Re: Visual C# General Accessing an assigned local variable

~rabin

You are doing one thing wrong here.

It should be

string playerName = listBox1.Items[p].ToString(); //marked in red.

Then it should be working fine provided that your Player class is correctly written, as I don't know exactly what does it do. Does Player.Name return the same value passed to its constructor






Re: Visual C# General Accessing an assigned local variable

Lucy_H

Thank you! That now works perfectly!

Here is my code for my Players class:

using System;

using System.Collections.Generic;

using System.Text;

using TestingGameLibrary;

namespace TestingGame

{

public class Player

{

private string name;

public string Name

{

get

{

return name;

}

}

private Player()

{

}

public Player(string newName)

{

name = newName;

}

public bool HasWon()

{

//Some code

}

}

}

Lucy