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.
Related
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
class Program
{
static void Main(string[] args)
{
//I want to call the bird method here
}
public void bird(Program program)
{
birdSpeech();
}
public void birdSpeech()
{
Console.WriteLine("Chirp Chirp");
}
}
How do I call bird in the main, I also tried to call the method from a object of the class but that didn't work
Does it even make sense to do this (I try to avoid static methods as much as possible).
If a method can be made static without changing the code, you should make it static. To answer your question though, you can create an instance of Program in your Main function and call the non-static methods through it.
class Program
{
static void Main(string[] args)
{
var p = new Program();
p.bird();
}
public void bird()
{
birdSpeech();
}
public void birdSpeech()
{
Console.WriteLine("Chirp Chirp");
}
}
Could do something like this
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var p = new Program();
p.bird(p);
}
public void bird(Program program)
{
birdSpeech();
}
public void birdSpeech()
{
Console.WriteLine("Chirp Chirp");
}
}
In C# , in Visual Studio, using a Console Application, is there a way to make methods in a class and call them in main program using readline?
Aka, a way to choose which methods to open when the program is running.
Easiest way is a switch statement for <4 cases, and a Dictionary for 4 or more cases.
class Program
{
private static IDictionary<string, Action> MethodMappings = new Dictionary<string, Action> {
{"Method1", Method1},
{"Method2", Method2},
...
}
public static void Main(string[] args) {
var methodCall = Console.ReadLine();
if (!MethodMappings.ContainsKey(methodCall)) {
//unrecognized command
}
MethodMappings[methodCall].Invoke();
}
private static void Method1() { ... }
private static void Method2() { ... }
}
This is very much possible using Reflection. Here is sample code to help you out:
class Program
{
public static void Hello1()
{
Console.WriteLine("\nHello 1");
}
public static void Hello2()
{
Console.WriteLine("\nHello 2");
}
static void Main(string[] args)
{
while (true)
{
String method = Console.ReadLine();
Type methodType = typeof(Program);
MethodInfo voidMethodInfo = methodType.GetMethod(method);
voidMethodInfo.Invoke(method,null);
}
}
}
For more information you can visit here.
This is a Console Program
Scenario:
I have a many function in my c# code and is set to be Public and Private. And I have static int Main(string args[]) of course. In int Main i calling some function Public and Private and thats interupting the function when i debug that. It says if main was static so all function must be static, yet my code in function Public and Private must be have "that" function. If i change static int Main with my public int main, debug cant be done because int Main must be static.
First im saying i really sorry for giving a non-understable question
If that's so confusing understanding my scenario, see my Example Code Here
namespace Example
{
class Program
{
public class ClassInPublic
{
public int Num;
public string Sentence;
}
ClassInPublic DeclaringInstance = new ClassInPublic();
private void Foo()
{
DeclaringInstance.Num = 35;
DeclaringInstance.Sentence = "Hello World";
Console.Write("This Private Function! And Assign Variable Value From ClassInPublic Variable");
}
private void Foo2()
{
Console.Write("This Single Foo didnt have any Instance Class");
}
static int Main(string args[])
{
Foo();
}
}
}
That's my case
And with help from others i already build this
namespace Example
{
class Program
{
public static class ClassInPublic
{
public int Num;
public string Sentence;
}
ClassInPublic DeclaringInstance = new ClassInPublic();
private static void Foo()
{
DeclaringInstance.Num = 35;
DeclaringInstance.Sentence = "Hello World";
Console.Write("This Private Function! And Assign Variable Value From ClassInPublic Variable");
}
private static void Foo2()
{
Console.Write("This Single Foo didnt have any Instance Class");
}
public static int Main(string args[])
{
Foo();
}
}
}
Error sign is lower right now, the rest is the ClassInPublic
It says i cannot declare
cannot declare instance members in static class
May someone assist me to overcome this?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Example
{
class Program
{
public void foo()
{
Console.WriteLine("This is foo in Class Program");
}
static void fooStatic()
{
Console.WriteLine("This is Static foo");
}
static void Main(string[] args)
{
Program fooInstance = new Program();
fooStatic();
fooInstance.foo();
}
}
}
Here is a small example. Static functions are one per class. When you make public or private function they exist for each instance.
Edit:
Here is what I think you are trying to do:
namespace Example
{
class Program
{
public class ClassInPublic
{
static public int Num;
static public string Sentence;
}
ClassInPublic DeclaringInstance = new ClassInPublic();
private static void Foo()
{
ClassInPublic.Num = 35;
ClassInPublic.Sentence = "Hello World";
Console.WriteLine("This Private Function! And Assign Variable Value From ClassInPublic Variable");
}
private static void Foo2()
{
Console.Write("This Single Foo didnt have any Instance Class\n");
}
public static int Main(string[] args)
{
Foo();
return 0;
}
}
}
Is it by any chance possible to call a method without referencing to its class?
For instance, you have a helper class:
class HelperTools
{
public static void DoWork()
{ /*...*/ }
}
And then you need to call it:
class MainClass
{
public static void Main()
{
HelperTools.DoWork();
}
}
Is it possible to call DoWork(); without a reference? Like this:
public static void Main()
{
DoWork();
}
Just for sake of simplicity.
Not quite, but here are 5 patterns that get you close:
namespace My.Namespace
{
using H = MyHelperClass;
public class MyHelperClass
{
public static void HelperFunc1()
{
Console.WriteLine("Here's your help!");
}
}
public class MyHelperClass2
{
public static void HelperFunc4()
{
Console.WriteLine("Here's your help!");
}
}
public interface IHelper{ }
public static class HelperExtensions
{
public static void HelperFunc3(this IHelper self)
{
Console.WriteLine("Here's your help!");
}
}
public class MyClass : MyHelperClass2, IHelper
{
private static readonly Action HelperFunc2 = MyHelperClass.HelperFunc1;
private static void HelperFunc5()
{
Console.WriteLine("Here's your help!");
}
public void MyFunction()
{
//Method 1 use an alias to make your helper class name shorter
H.HelperFunc1();
//Method 2 use a class property
HelperFunc2();
//Method 3 extend an interface that has extension methods.
//Note: you'll have to use the this keyword when calling extension
this.HelperFunc3();
//Method 4 you have access to methods on classes that you extend.
HelperFunc4();
//Method 5 put the helper method in your class
HelperFunc5();
}
}
}
No. Java has the concept of importing static like this, but C# does not. (IMO, a naked DoWork() without any clue as to where the implementation resides is non-ideal.)
a few years late but maybe this will help someone else...
Use a using static directive to reference the static class: (introduced in C# 6)
using static HelperTools;
class MainClass
{
public static void Main()
{
DoWork();
}
}
---------------- HelperTools.cs--------------------
class HelperTools
{
public static void DoWork()
{ /*...*/ }
}
The only place you can call DoWork from without referencing the class name is within the class itself. For instance, if you add a non-static method to HelperTools:
public void foo()
{
DoWork();
}
You can call DoWork from within it, even though foo() is not static.