The name 'txtPlayerMove' does not exist in the current context
Hi,
I'm very new to C# and I'm trying to insert a text string into a text box that sits on a form in the same project. The class that is trying to send the string to the text box is in a different file than the form code.
I get this error:
"The name 'txtPlayerMove' does not exist in the current context"
from this code:
public bool throwSnow(int range)
{
//calculates likeliness of a hit at a given range
//returns true if snowball hit
bool hit = false;
int myRoll;
Random roller = new Random();
if (snowballs <= 0)
{
txtPlayerMove.Text = Convert.ToString(name + " is out of snowballs!");
}
... (i didn't include the rest of the code)
I'm sure this is a complete newbie problem, but I'm not quite sure how to fix it. Any suggestions would be much appreciated!
Thanks,
Oliver
[919 byte] By [
ooboyle] at [2007-12-27]
Random roller = new Random();
if (snowballs <= 0)
{
txtPlayerMove.Text = Convert.ToString(name + " is out of snowballs!");
}
Do you have any TextBox on your form that called txtPlayerMove ?!
If you don't try to rename your textbox name property with it.
Babak Izadi
LotraSoft Ltd.
Hello All.
Oliver:
You have to have a way for your method ThrowSnow() to know about the instance of your form that has the txtPlayerMove textbox on it.
Let's say the class for your form is called MainForm. You have an instance of that form running called currentForm.
If ThrowSnow() is called from currentForm, you can change the signature of ThrowSnow() to
public bool ThrowSnow(int range, MainForm frm) .. frm.txtPlayerMove.Text = Convert.ToString(name + "is out of snowballs."); |
|
and when you call this method from currentForm,
ThrowSnow(rangeToTarget, this); |
|
This is just one of several ways to do it. How specifically are you calling the method and from where?
Hi Mark,
What you describe sounds like it's probably the problem, but I believe I'm calling things the other way around. I have a mainForm that's loaded when the program is executed. When I click a button on this form, a class in another .cs file is called, as below (player is already declared at the beginning of the mainForm class --> Fighter player) and it sends the constructor the name that the player typed into a text box:
player = new Fighter(txtPlayerName.Text);
The Fighter class has some properties that I can successfully interrogate from mainForm, so things work well to that point. The Fighter class does some things that produce output (like throwing snowballs), and I need to register this output in a text box called txtPlayerMove that sits on my main form. The text box is properly named, so that's not what's broken.
So, Fighter is instantiated from mainForm and then Fighter needs to send a string back to mainForm's txtPlayerMove text box.
Let me know if you need more information.
Thanks for the help guys!
Oliver
Hello All.
Oliver:
Okay, from looking over the code in your OP, and from what you're wanting to do, it looks like you're breaking encapsulation pretty badly. Ideally, you want each method in a class to handle one single piece of behavior for an object of that class. The problem is that you're trying to do too many things in one method.
If you need to check that the player has any ammo before making the throw, that should be in a different method. Failing that, you could add an out parameter for the message, but you wouldn't get your hands on it until the method returned. If you need it before any other stuff is done in the method, it really shouldn't be in the method in the first place.
I suggest adding a method to Fighter called HasAmmo(), like so:
public bool HasAmmo()
{
//check here for snowballs
}
Then, on your main Form, you could do:
if (player.HasAmmo())
{
//handle player.ThrowSnow()
}
else
{
txtPlayerMove.Text = player + "is out of snowballs";
}
If you just absolutely want to do it from ThrowSnow(), then adding the form instance to the parameter list like I mentioned before would work. Just don't tell anybody it was my idea.
Hi Mark,
My Fighter class is pretty simple (this is a very simple game!). Just so the context is correct, I originally made this game to run in the console, and now I'm trying to make it visual. Here is the entire code for my Fighter class, without the really simple constructor and without the properties. As you can see, it's pretty simple, and it's the only method in the class. The form either get's a reply that the Fighter is out of snowballs, or the result of the hit (hit or miss) from a return value. So does this make it more acceptable to send it from throwSnow()? Otherwise, all this stuff would either need to go into my _click event, or I would need to add this class to the form.cs file, which I don't think is correct either.
public bool throwSnow(int range)
{
//calculates likeliness of a hit at a given range
//returns true if snowball hit
bool hit = false;
int myRoll;
Random roller = new Random();
if (snowballs <= 0)
{
txtPlayerMove.Text = Convert.ToString(name + " is out of snowballs!");
}
else
{
myRoll = roller.Next(10);
if (myRoll > range)
{
hit = true;
} // end hit if
snowballs--;
} // end out of snowballs if
return hit;
} // end throwSnow method
Thanks again for your help!
Oliver
Mark,
I tried:
public bool ThrowSnow(int range, MainForm frmVisualSnowball) .. frmVisualSnowball.txtPlayerMove.Text = Convert.ToString(name + "is out of snowballs."); |
But "MainfForm" doesn't appear in blue in the IDE. Is MainForm supposed to be something else?
Oliver
Hello All.
Oliver:
Yes, it's supposed to be the name of your Form class that defines frmVisualSnowball. So, if that class is named frmVisualSnowball, and in the code in your app where you instantiate that class, like:
frmVisualSnowball form = new frmVisualSnowball();
then, the signature of ThrowSnow() would be:
public bool ThrowSnow(int range, frmVisualSnowball variableName)
{
... variableName.txtPlayerMove.Text = name + "is out of snowballs";
}
On a side note, if "name" is a field of Fighter, and is presumably a string, then why the call to Convert.ToString() ?
HTH.
But my frmVisualSnowball is instantiated from my Program.cs file as such:
namespace VisualSnowball
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmVisualSnowball());
}
Application.Run(new frmVisualSnowball() is how my mainform is being instantiated. But there's no variable... so I'm still a little confused as to what I'm supposed to be feeding into the signature.
Thanks,
Oliver
Hello All.
Oliver:
Here's a prototype of the class for the form:
public partial class frmVisualSnowball : Form { Fighter player; public frmVisualSnowball() { InitializeComponent(); } public string PlayerMoveText { get { return txtPlayerMove.Text; } set { txtPlayerMove.Text = value; } } private void button1_Click(object sender, EventArgs e) { int rangeToTarget = 5; player.ThrowSnow(rangeToTarget, this); } private void frmVisualSnowball_Load(object sender, EventArgs e) { player = new Fighter(txtPlayerName.Text); } } |
|
and here's a prototype of the Fighter class:
class Fighter { private string name; public Fighter(string name) { } public bool ThrowSnow(int range, frmVisualSnowball form) { bool retVal = true; form.PlayerMoveText = name + "is out of snowballs"; return retVal; } } |
|
HTH.
Hi Mark,
Interestingly, that's almost how I had it configured at one point, but I got the following error. I still get this error when I duplicate your code:
Error 1 'VisualSnowball.frmVisualSnowball.txtPlayerMove' is inaccessible due to its protection level
What am I missing?!!
Thanks!
Oliver
forget that last message. I just realized that I forgot to make the property for txtPlayerMove.
I assume this is required because txtPlayerMove was declared as "private" in the form's class...
sorry, newbie move there.
This should work, but I'll let you know.
Thanks,
Oliver
here's another question before I add the property:
Should I put the property in the frmVisualSnowball.Designer.cs file where the txtPlayerMove control is declared, or should I put it in the frmVisualSnowball.cs file, which is where all my events are?
Thanks!
Oliver
Hello All.
Oliver:
No, you pretty much want to stay out of frmVisualSnowball.Designer.cs. That's where Visual Studio Form Designer puts the code that it generates when you design a form visually. Before the advent of partial Classes, all of that code was mixed in with your code, and it was a real mess.
Besides, whenever the Visual Studio Form Designer has to regenerate that code, anything you put in there will probably get tossed by the code generation tool anyway.
So, put it in frmVisualSnowball.cs .
HTH.
Ah, ok! The missing piece! I'll do a little research into partial classes so that I understand them better.
Will let you know what happens after I add the property.
O.