Reputation: 11
FIXED
It was failing in 6 different spots in "form1.designer.cs". It said I needed to used it statically, so I changed all the errors from "this.xxxx" to "form1.xxxx" and it worked. Not sure I completely understand what I did though...
FIXED
Fairly new to programming here and just working on some basic OOP in C#. I am trying to call a method from another class in my Button Click event. The method passes one parameter which is user inputted text. I think the problem has something to do with the Method being static, but the parameter is dynamic, because it is user inputted. Please help me out!
public partial class Form1 : Form
{
string _yourName = textBox1.Text;
public Form1()
{
InitializeComponent();
}
private void Button1Click(object sender, EventArgs e)
{
if (DogCheckBox.Checked)
{
AnimalNoise.Bark(_yourName);
}
if (CatCheckBox.Checked)
{
AnimalNoise.Meow(_yourName);
}
if (FishCheckBox.Checked)
{
AnimalNoise.Girgle(_yourName);
}
}
}
public class AnimalNoise
{
public static void Bark(String name)
{
MessageBox.Show(name + " Bark");
}
public static void Meow(String name)
{
MessageBox.Show(name + " Meow");
}
public static void Girgle(String name)
{
MessageBox.Show(name + " Girgle");
}
}
Upvotes: 0
Views: 4188
Reputation: 1495
Your exact problem isn't clearly stated but I believe you are not ever setting textBox1.Text. Set it during your ButtonClick event.
public Form1()
{
InitializeComponent();
}
private void Button1Click(object sender, EventArgs e)
{
string _yourName = textBox1.Text;
if (DogCheckBox.Checked)
{
AnimalNoise.Bark(_yourName);
}
if (CatCheckBox.Checked)
{
AnimalNoise.Meow(_yourName);
}
if (FishCheckBox.Checked)
{
AnimalNoise.Girgle(_yourName);
}
}
}
Upvotes: 0
Reputation: 5655
First of all, you can't access textBox1
before it's actually created. You should capture the name inside the click callback. I think the error you're seeing might be because somewhere in your code you have something else called AnimalNoise
you'll have to make sure there is nothing else called AnimalNoise and put AnimalNoise in it's own file.
Upvotes: 3