Method is not valid in the current context error.

  • Thread starter Thread starter George O'Gorman
  • Start date Start date
G

George O'Gorman

Guest
Hello. I'm getting an error trying to generate a random name in a class, then display it in a form. The error is in a button click, and says it is not valid in the current context. Any help is massively appreciated. Thanks in advance!

Windows Forms App .Net Framework.

Form2

RandomNameGenerator randomnamegenerator = new RandomNameGenerator();

private void GenerateNPCButton_Click(object sender, EventArgs e)
{
DungeonCreationText.Text = randomnamegenerator.NameGenerator.Charname; // Error here on NameGenerator.
}


RandomNameGenerator class

public class RandomNameGenerator
{
public string NameGenerator()
{
Random rnd = new Random();
string Temp = "";
string Charname = "";
int mf = rnd.Next(2) + 1; // Decide if character is male or female.
int no = rnd.Next(10) + 1; // Decide if character is nobility.
int nfn = rnd.Next(3) + 1; // Number of first names.
if (no < 10) { nfn = 1; }
if (mf > 1) // Female
{
for (int i = 0; i < nfn; i++)
{
int fn = rnd.Next(34);
for (int ii = 0; ii < fn; ii++)
{
Temp = "";
Temp = Temp + Lore.FirstNamesFemale[ii];
}
Charname = Charname + Temp + " ";
}
}
else // Male
{
for (int i = 0; i < nfn; i++)
{
int fn = rnd.Next(34);
for (int ii = 0; ii < fn; ii++)
{
Temp = "";
Temp = Temp + Lore.FirstNamesMale[ii];
}
Charname = Charname + Temp + " ";
}

}
int ln = rnd.Next(51);
for (int ii = 0; ii < ln; ii++)
{
Temp = "";
Temp = Temp + Lore.LastNames[ii];
}
Charname = Charname + Temp;
return Charname;


}

Continue reading...
 
Back
Top