Arbitrary and non sense string inside a code block - c#

This is something , i don't understand , if i put any arbitrary string inside a code block, it will throw some compile time error but if i put something like below , it will not.
static void Main(string[] args)
{
int i = 5;
ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp://www.google.com
Console.WriteLine(i.ToString());
Console.ReadLine();
}
any idea why this is happening? I just found it accidentally , not sure why , may be i am missing something.

That is a Label.
Look at the : at the end.
If you remove the : in the end. It won't compile
goto and label

ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp: is a label, because it's followed by :.
You can then use it with goto statement:
static void Main(string[] args)
{
int i = 5;
ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp://www.google.com
Console.WriteLine(i.ToString());
Console.ReadLine();
goto ghfhfghfghfghfhfghfhfghfghfghfhfghfghfghghttp;
}

Related

Is it possible to keep portions of the console from scrolling in .NET?

Figure 1: the bottom portion is to scroll, while the top portion is to stay put.
So I'm writing a CLI .NET Core application.
The top portion of the image is to stay put whereas the bottom portion is to scroll under the top portion.
If there was a more direct access to the console, it would be rather easy. But as we are in C#, no such (cross-platform) access exists that I am aware of.
So, how would I achieve the desired goal here, without platform-locking?
You can try this if it might give you an idea. Based on this post C# Console : How to make the console stop scrolling automatically?.
static readonly int maxLine = 10;
static int currentLine = 0;
static void Main(string[] args)
{
setHeader();
while (true)
{
writeLine("Please type and enter:");
string value = Console.ReadLine();
writeLine($"Your input is: {value}");
writeLine($"Current Line: {currentLine}");
if (currentLine > maxLine)
{
resetCursorPosition();
writeLine($"Your input is: {value}");
writeLine($"Current Line: {currentLine}");
}
}
}
private static void writeLine(string value)
{
Console.WriteLine(value);
currentLine = currentLine + 1;
}
private static void resetCursorPosition()
{
Console.Clear();
Console.SetCursorPosition(0, 0);
currentLine = 0;
setHeader();
}
private static void setHeader()
{
writeLine("----------------------------------------------");
writeLine("---------------MY CUSTOM HEADER---------------");
writeLine("----------------------------------------------");
}
Output:

C# How to end an infinite loop that also recieves user input with the press of Escape in Console Application

I have just started learning C# and I'm practicing some basic coding in the console application and I'm trying to make a program that adds two integers together by using an infinite loop and a method. But I want to be able to end the loop by pressing escape but the problem is, is when I press any key besides escape after the loop has completed for the first time, the program crashes. It says "Input string was not in a correct format." Any suggestions would be greatly appreciated!
Thanks!
namespace ConsoleApplication2
{
class Program
{
static void Addxy(int x, int y)
{
Console.WriteLine(x+y);
}
static void Main(string[] args)
{
while(true)
{
Addxy(Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine()));
ConsoleKeyInfo end;
end = Console.ReadKey();
if (end.Key == ConsoleKey.Escape)
break;
else
{
}
}
}
}
}
You have two Console.ReadLine() before Console.ReadKey(). So the first two inputs should be numerals followed by the Escape key to terminate the program. Remember to press Enter after the first two numeral entry.
You could use break to end the loop, but a more elegant way would be to define a variable that determines if the loop should keep running. Here is my suggestion for your problem:
static void Main(string[] args)
{
bool keepLooping = true;
while (keepLooping)
{
//Do your stuff here
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
keepLooping = false;
}
}
}
or an even shorter version if you prefer that:
static void Main(string[] args)
{
bool keepLooping = true;
while (keepLooping)
{
//Do your stuff here
keepLooping = Console.ReadKey().Key != ConsoleKey.Escape;
}
}

list find all elements

I making a hashtable(not really important I think), I wanna print out all my elements from my variabel linked list, the insertionOrder(it holds strings, but doesn't matter), I have to do like this, in my method i want to return insertionOrder, but not sure how to do that so the method get all the elements, and in my class Program I wanna print out the insertionOrder, I can't find a solution, thanks! Why I have to do like this? my teacher want it like this. I just wonder if you know a solution. Thanks!
class Hashtable
{
private LinkedList<object> insertionOrder = new LinkedList<object>();
.
.
public LinkedList<object> GetInsertionOrder()
{
return insertionOrder;
}
}
class Program
{
static void Main(string[] args)
{
Hashtable hT= new Hashtable(8);
Console.Writeline("The elements: " + hT.insertionOrder());
}
}
A few things;
insertionOrder is declared outside the main class, as private, so you're not going to be able to get to it from Main().
insertionOrder has absolutely nothing to do with the Hashtable. I'm surprised your IDE isn't throwing up an error at the hT.insertionOrder() call, since it's not valid.
Try something like this.
class Program
{
private LinkedList<object> insertionOrder = new LinkedList<object>();
static void Main(string[] args)
{
int count = 1;
foreach (object o in insertionOrder)
{
Console.Writeline("The Element(" + count + "): " + o.ToString());
count++;
}
}
}

C# Restarting a console application

I've created a small application that does a small conversion. At the end of the program I've created a method that allows the user to make another calculation if they press 'r'. All I want it to do is if they press r, take them back to the beginning of Main, else terminate program. I do not want to use goto. This is what I've got so far, and the error I'm getting.
http://puu.sh/juBWP/c7c3f7be61.png
I recommend you use another function instead of Main(). Please refer to the code below:
static void Main(string[] args)
{
doSomething();
}
public static void WouldYouLikeToRestart()
{
Console.WriteLine("Press r to restart");
ConsoleKeyInfo input = Console.ReadKey();
Console.WriteLine();
if (input.KeyChar == 'r')
{
doSomething();
}
}
public static void doSomething()
{
Console.WriteLine("Do Something");
WouldYouLikeToRestart();
}
A while loop would be a good fit, but since you say the program should run and then give the user the option to run again, an even better loop would be a Do While. The difference between while and Do While is that Do While will always run at least once.
string inputStr;
do
{
RunProgram();
Console.WriteLine("Run again?");
inputStr = Console.ReadLine();
} while (inputStr == "y");
TerminateProgram();
In your case, you want to repeat something so of course you should use a while loop. Use a while loop to wrap all your code up like this:
while (true) {
//all your code in the main method.
}
And then you prompt the user to enter 'r' at the end of the loop:
if (Console.ReadLine () != "r") {//this is just an example, you can use whatever method to get the input
break;
}
If the user enters r then the loop continues to do the work. break means to stop executing the stuff in the loop.

How call the main method or restart the app console?

When an exception occurs I want to restart all the processing or start the Main method, after this other method:
public void DisplayMessage(string message) {
Console.WriteLine(message, "Rebuild Log Files");
Console.WriteLine(" Press Enter to finish, or R to restar the program...");
string restart = Console.ReadLine();
if(restart.ToUpper() == "R") {
//Call the Main method or restart the app
}
Console.ReadKey();
}
Note: the main method contains some user written data.
How can I do this?
Ok you have a main
void main(...)
{
some code
}
All you need to do is...
void main()
{
runStartUpCode();
}
void runStartUpCode()
{
some code
}
Then when you need to restart the code, call runStartUpCode() again.
if(restart.ToUpper() == "R") {
Close();
System.Diagnostics.Process.Start(Application.ExecutablePath);
}
static void Main(string[] args) {
try {
// code here
} catch /* or finally */ {
DisplayMessage(/* pass in any state from Main() here */);
}
}
static void DisplayMessage(/* passed in state from Main() */) {
// original DisplayMessage() code
// if R was pressed
Main(/* put any args to pass to Main() in here */);
}
I actually just got done with this problem earlier by the time you read this post. I kind of duplicated the ORIGINAL main method, changed a few options on the original, and left the copy to call the new main method. Here is what I mean.
static void Main(string[] args)
{
Program CallingTheRealMain = new Program();
CallingTheRealMain.Main2();
}
public void Main2()
{
//Any code here
}
I originally needed to do this because I needed to loop back to the main method but couldn't because it was static. This code worked fine for me, hope it does for you too if you choose to implement it. Hope I helped. Happy coding!
DialogResult result = MessageBox.Show("Do You Really Want To Logout/Exit?", "Confirmation!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
this.Close();
System.Diagnostics.Process.Start(Application.ExecutablePath);
}
I think your design approach should change if what your app to autorestart.... here's a Pseudocode
main (){
errorObj = null;
internalFunct(errorObj);
if(errorObj != null) return;
secondINternalFunction();
}
Running the app...
while(!errorObj){
main();
}
Why i prefer this approach, because Main, or function recall is avoided... If your working with small memory, there is not point recalling main on the stack of limited space... you have no choice but to be iterative...
New to C#, I apologize for mistakes.
Use this:
static void Restart() {
String[] n = new String[10];
n[0] = "hi";
Main(n);
}
Main takes a string array for reasons I am unaware of. Seems useless to me, because I tried removing it and nothing seemed to change. So, maybe this works too:
Try removing the string[] args part of Main() so you can just type Main(); without
issues.

Categories

Resources