I am trying to raed the value of userInput variable in my parent class which is stored in my child class, but I can't reach it and I get many errors one after one after each try. Can you please help me?
This is apart of my code:
//Child class
class tictactoe
{
public void beginGame()
{
ConsoleKeyInfo gebruikerInvoer;
gebruikerInvoer = Console.ReadKey();
//Rest of code
}
}
//Parent class
namespace Quizz
{
class Program
{
public static void Main(string[] args)
{
tictactoe minigame1 = new tictactoe();
while (minigame1.gebruikerInvoer)
{
//Rest of code
}
}
}
}
This is the error I get
'tictactoe' does not contain a definition for 'gebruikerInvoer' and no accessible extension method 'gebruikerInvoer' accepting a first argument of type 'tictactoe' could be found (are you missing a using directive or an assembly reference?)
I think that I will need to make a method to call it from the parent, if so: what type should I give since the variable is a ConsoleKeyInfo that is later converted to string?
gebruikerInvoer.KeyChar.ToString()
As the comments have made apparent, you need to first make the variable visible to the outside world. Local variables are always hidden from other classes. So lets fix this first:
class tictactoe
{
public ConsoleKeyInfo gebruikerInvoer; //Make public and move owner to class not method
public void beginGame()
{
gebruikerInvoer = Console.ReadKey();
//Rest of code
}
}
//Parent class
namespace Quizz
{
class Program
{
public static void Main(string[] args)
{
tictactoe minigame1 = new tictactoe();
while (minigame1.gebruikerInvoer) //CS0029 ConsoleKeyInfo cannot be implicitly converted to type Bool
{
//Rest of code
}
}
}
}
Moving on from there you need to define a statement to test the variable's value in a way that can return a true or false statement to use a while loop. To fix that we can do something like this:
class tictactoe
{
public ConsoleKeyInfo gebruikerInvoer;
public void beginGame()
{
gebruikerInvoer = Console.ReadKey();
//Rest of code
}
}
//Parent class
namespace Quizz
{
class Program
{
public static void Main(string[] args)
{
tictactoe minigame1 = new tictactoe();
bool minigameRun = minigame1.gebruikerInvoer.KeyChar == 't'
? true : false; //Assign a true value if the user entered the letter 't', else false
while (minigameRun) //Runs rest of code while minigameRun is true
{
//Rest of code
}
}
}
}
Then to escape the while loop you can use return, break, or upon some condition change the local bool minigameRun to false. The bool is initially assigned in this code using the ?: operator. You can read more about its use here
Related
class Program
{
public static string playerName;
static void Main(string[] args)
{
playerName = Console.ReadLine();
}
public static void userInterface()
{
Console.Writeline("Name:" + playerName)
}
}
Been trying to understand where im falling short for a few hours now and cannot figure it out, wondering if any of the SO residents can help me.
Im trying to display a inputted username in a GUI using C# console, I have defined it as a public variable in the class and called it in the method, however its throwing me this exception and displaying a null value?
Any help is appreciated.Class and Main The method im trying to call the variable to
EDIT the desired aim is to have the program display the users inputted username in the UI at the top of the console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Player player = new Player
{
Name = "name coming from your input class"
};
UserInterface userInterface = new UserInterface(player);
}
}
class UserInterface
{
public UserInterface(Player player)
{
Console.SetWindowSize(220, 55);
Console.WriteLine("Name: {0}", player.Name);
}
}
class Player
{
public string Name { get; set; }
}
}
Or something alike. So you need to provide values for your variables.
Just call the method userInterface() after this line playerName = Console.ReadLine();
this will display the accepted value on console.
I am trying to 'share' variables between methods in C#. I am quite new to C#, I know there's no such thing as a 'global variable' and I'm not quite sure how to correctly use static variables.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectOne
{
static bool tooHigh;
static internal waistMeasurment;
class Main
{
static void Main(string[] args)
{
GetVariables();
}
public static void GetVariables() //This method gets the users height & waist measurments
//and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
{
Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
waistMeasurment = Console.ReadLine(); //This writes the users input under the string 'waistMeasurment'
ValidateWaist();
if tooHigh ==true
{
waistMeasurment = Console.ReadLine();
ValidateWaist();
}
}
public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
{
if (waistMeasurment < 60) //Checks the lower bound of the waist limit
{
Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
tooHigh = true;
}
else
{
tooHigh = false;
}
}
}
}
Im having issues with the tooHigh and waistMeasurment
C# is an Object-Oriented Programming Language, which means it consists of namespaces, that hold classesand structs, which hold fields and properties (so, variables) and methods.
You're absolutely right, there are no global variables in C#. Although, you could hack together a class with a static variable and use it as a global, it's best to keep all the variables local and under control.
What you want to achieve, is to place the two variables (tooHigh and waistMeasurment) within the static Main class as static variables.
You also don't use if statements in the style of Python and internal was invented for methods, not variables. According to your code, you are looking for the type of integer, since later on, you are checking whether the variable waistMeasurment is less than 60. In order to do this, you first have to cast the variable to an integer. The proper way to this, is with the int.TryParse method.
namespace ProjectOne
{
class Main
{
static bool tooHigh;
static int waistMeasurment;
static void Main(string[] args)
{
GetVariables();
}
public static void GetVariables() //This method gets the users height & waist measurments
//and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
{
Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
int.TryParse(Console.ReadLine(), out waistMeasurment); //This writes the users input under the string 'waistMeasurment'
ValidateWaist();
if (tooHigh)
{
int.TryParse(Console.ReadLine(), out waistMeasurment);
ValidateWaist();
}
}
public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
{
if ((int)waistMeasurment < 60) //Checks the lower bound of the waist limit
{
Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
tooHigh = true;
}
else
{
tooHigh = false;
}
}
}
}
Static can be used to share variable but need to be declared inside Class, there were some more errors in your program, I have modified your program you can use it and debug it for learning
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static bool tooHigh;
static int waistMeasurment;
static void Main(string[] args)
{
GetVariables();
}
public static void GetVariables() //This method gets the users height & waist measurments
//and calls the 'ValidateWaist' and 'ValidateHeight' methods for validation
{
Console.Write("What is your waist measurment? "); //This prints a string prompting the user to input their waist measurment
int.TryParse(Console.ReadLine(), out waistMeasurment); //This writes the users input under the string 'waistMeasurment'
ValidateWaist();
if (tooHigh == true)
{
int.TryParse(Console.ReadLine(), out waistMeasurment); ;
ValidateWaist();
}
}
public static void ValidateWaist() //This method validates the user input so that it fits within the minimum bound
{
if (waistMeasurment < 60) //Checks the lower bound of the waist limit
{
Console.Write("Your waist measurment must be above 59cm? "); //Output error feedback
tooHigh = true;
}
else
{
tooHigh = false;
}
}
}
}
As stated in the comments you have to change your code in order to place the variables inside a Type:
What you've got:
namespace ProjectOne
{
static bool tooHigh;
static internal waistMeasurment;
class Main
{
//your code
}
}
What you have to write:
namespace ProjectOne
{
class Main
{
static bool tooHigh;
static internal waistMeasurment;
//your code again
}
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
I have a Class and Method like below
public class Wakeup : World
{
public void MethodA(string)
{
Log.writeline("Wakeup World");
}
}
And below one is another class and method in which I am trying to call "MethodA"
public class Hello : World
{
public void MethodB()
{
Wakeup p = new Wakeup;
p.MethodA(string);
}
}
This Isn't working. Unable to call "MethodA" inside "MethodB"
Note : Both the classed are Inherited to some other class called "World"
Can anyone suggest me how can I achieve this ?
Create instance of first class inside second one correctly, also pass some string value instead string type in second class method call
public class Wakeup : World
{
public void MethodA(string text)
{
Log.writeline(text);
}
}
public class Hello : World
{
public void MethodB()
{
Wakeup p = new Wakeup();
p.MethodA("Wakeup World");
}
}
You have a typo, you have not placed () at the end of instantiating your Wakeup class. It should be as follow:
Wakeup p = new Wakeup();
Another thing, you should not pass the type itself to the method, in other words do not pass the type word string, but rather a string value. A string value is placed within quotation " " marks, as follow: "Hello, World".
So the following code for you Class Hello should work. Note how I instantiated your Wakeup class, and passed a value to Method A. Here is the complete code:
public class Wakeup : World
{
public void MethodA(string strValue)
{
Console.WriteLine(strValue);
}
}
public class Hello : World
{
public void MethodB()
{
Wakeup p = new Wakeup();
p.MethodA("Hello, World");
}
}
works great for me
public class World { }
public class Wakeup : World
{
public void MethodA(string a)
{
Console.WriteLine("Wakeup World");
}
}
public class Hello : World
{
public void MethodB()
{
Wakeup p = new Wakeup();
p.MethodA("Dsa");
}
}
public partial class Form1 : Form
{
public disp(string strVal)
{
lbl1.text = strVal;
}
private void button1_Click(object sender, EventArgs e)
{
class1 cl = new class1;
cl.Show1("test",this);
}
}
---------------- Class ------------
public class class1
{
private Form frm1;
public void Show1(string xName , object xfrmObj)
{
frm1 = (form) xfrmObj;
frm1.disp(xName ); // here I am getting error .
}
}
------------------------------------------/
here i am trying to access 'disp' function from class and i have pass 'form1' as a object , but i am getting error
The error message that I am getting is
Error 3
'System.Windows.Forms.Form' does not contain a definition for 'disp'
and no extension method 'disp' accepting a first argument of type 'System.Windows.Forms.Form' could be found
(are you missing a using directive or an assembly reference?)
this syntax in vb.net is working perfect.
please help me.....
Rajesh.
you need to cast frm1 = (Form1)xfrmObj; instead of casting it to form in your Show1(xName,xfrmObj) method.
EDIT: OP has stated in a comment that he needs this to work for several different forms.
You can make all your forms implement the same Interface, like so:
public partial class Form1 : Form, ICanDisplay
{
public void disp(string strVal)
{ //...
}
}
public partial class Form2 : Form, ICanDisplay
{
public void disp(string strVal)
{ //...
}
}
public interface ICanDisplay
{
void disp(string strVal);
}
then, change your method so it casts to ICanDisplay:
public class class1
{
private Form frm1;
public void Show1(string xName , object xfrmObj)
{
frm1 = (ICanDisplay) xfrmObj;
frm1.disp(xName);
}
}
However, as #Heinzi has noted, you should change your Show1-method to the following:
public void Show1(string xName, IDisplayForm xfrmObj)
{
xfrmObj.Disp(xName);
}
this will make the cast entirely unnecessary. The next step is to select meaningful names for your variables, functions and classes.
If I have the following code:
namespace foo {
public class FooClass {
public static void Main (string[] argsRaw) {
Console.WriteLine(Console.In.ReadToEnd());
}
}
}
And I run it, process will stop once the end of the stream is reached.
Output:
(Text of my input stream)
[1]+ Stopped bin/Debug/foo.exe
How do I get my program to behave more like grep, which does not stop after hitting EOF?
I figured it out.
You need to make sure that Main's return type is an int.
namespace foo {
public class FooClass {
public static int Main (string[] argsRaw) {
Console.WriteLine(Console.In.ReadToEnd());
return 0;
}
}
}
Works.