Inheritance problem
-
:confused:Why won't this code work? using System; using System.Windows.Forms; namespace Practice { class Practice { public static void Main() { // declare and assign a string variable called human string human; human = "Walking & talking life"; // declare and assign an int variable called mind int mind; mind = 100; // Print the value of the variables to the console Console.WriteLine(mind); Console.WriteLine(human); } } class Continued { public static void notMain() { MessageBox.Show(human); MessageBox.Show(mind); } } }
-
:confused:Why won't this code work? using System; using System.Windows.Forms; namespace Practice { class Practice { public static void Main() { // declare and assign a string variable called human string human; human = "Walking & talking life"; // declare and assign an int variable called mind int mind; mind = 100; // Print the value of the variables to the console Console.WriteLine(mind); Console.WriteLine(human); } } class Continued { public static void notMain() { MessageBox.Show(human); MessageBox.Show(mind); } } }
You did not mention what did not work, but I can guess it is the line: MessageBox.Show(mind); unlike
Console.WriteLine
,MessageBox.Show
does not have an overload that takes anint
. Try: MessageBox.Show(mind.ToString()); -------- "I say no to drugs, but they don't listen." - Marilyn Manson -
:confused:Why won't this code work? using System; using System.Windows.Forms; namespace Practice { class Practice { public static void Main() { // declare and assign a string variable called human string human; human = "Walking & talking life"; // declare and assign an int variable called mind int mind; mind = 100; // Print the value of the variables to the console Console.WriteLine(mind); Console.WriteLine(human); } } class Continued { public static void notMain() { MessageBox.Show(human); MessageBox.Show(mind); } } }
The human and mind variables are only visible inside the Main method. To do what you want you need something like this:
class Practice
{
public static string human;
public static int mind;public static void Main() { human = "Walking & talking life"; mind = 100; }
}
class Continued
{
public static void notMain()
{
MessageBox.Show(Practice.human);
MessageBox.Show(Practice.mind);
}
}