...

View Full Version : Help! C# [CODE]



jameswray50
12-21-2011, 07:56 PM
I got a book to learn C# coding and it directed me to do a program thing called "Hello World" i entered the code correctly but when i run the debugger the console pops up and says "Hello. My name is Inigo Montoya." as i directed it to, but the console only pops up for a few milliseconds and then closes. I thought that maybe a experienced person could help. Here is the Code i entered:


class HelloWorld
{
static void Main()
{
System.Console.WriteLine("Hello. My name is Inigo Montoya.");
}
}

I used the correct format altho in the post the codes spaces might be off. O and im also using "Microsoft visual studios custom C#" to do the coding and what not.

VIPStephan
12-21-2011, 08:26 PM
So, and why are you posting this in the HTML/CSS forum then?
Moved to the correct forum.

melloorr
12-21-2011, 08:34 PM
I got a book to learn C# coding and it directed me to do a program thing called "Hello World" i entered the code correctly but when i run the debugger the console pops up and says "Hello. My name is Inigo Montoya." as i directed it to, but the console only pops up for a few milliseconds and then closes. I thought that maybe a experienced person could help. Here is the Code i entered:


class HelloWorld
{
static void Main()
{
System.Console.WriteLine("Hello. My name is Inigo Montoya.");
}
}

I used the correct format altho in the post the codes spaces might be off. O and im also using "Microsoft visual studios custom C#" to do the coding and what not.

I only does that in Visual studios. To stop it, just type: System.Console.ReadLine(); after your WriteLine

jameswray50
12-21-2011, 08:54 PM
I'm a little confuzed... First I'm new to all this so the **** I'm getting from you guys (intentional or not) is very annoying while I'm trying to learn this stuff.

So based on what "melloorr" said i was un-able to figure that out... Do you think i completed this lesson in my book "hello World" console? i can get it to run just for a millisecond.

melloorr
12-21-2011, 08:57 PM
Im a little confuzed... First im new to all this so the **** im getting from you guys (intentional or not) is very annoying while im trying to learn this stuff.

So based on what "melloorr" said i was un-abble to figgure that out... Do you think i compleated this lesson in my book "hello World" consol? i can get it to run just for a millisecound.


class HelloWorld
{
static void Main()
{
System.Console.WriteLine("Hello. My name is Inigo Montoya.");
System.Console.ReadLine();
}
}


That should work, so you exit by pressing enter

alykins
12-22-2011, 12:58 AM
what melloorr posted is a viable solution. but you could simplify it to this...


using System;

namespace YourNameSpaceHere

class Hello
{
static void WriteHello()
{
Console.WriteLine("Hello World")
Console.ReadLine();
}
}


now let's go through and see why it is only open/up/running for a few milliseconds... Look at the function... the very first line is write something... program goes "ok- I'll write what you told me to in code" ... after program does this it moves on... so now the program says "now what?" ... well there is nothing there so the program is done and closes.... now if this were more than a "main program" eg it was a function to display/set something this part would happen and set those variables... so you could have something like this


class MyProgram
{
static void Main()
{
MyProgram _start = new MyProgram();
_start.hello();
Console.ReadLine();
}

public void hello()
{
Console.WriteLine("Hello World!");
}
}


what happens now is the program drops into the Main and creates an object of type class and then calls that objects function hello which writes the output... I assume that will come later in the book though

I do not know what melloorr's first post meant... if you were to compile that code and run anywhere (ie not just in Visual Studio) it would do the same thing no matter what- there is nothing more to the program... If melloorr meant he/she uses VS then idk why he/she said it at all... as to your comment


First I'm new to all this so the **** I'm getting from you guys (intentional or not) is very annoying while I'm trying to learn this stuff.


I saw nothing offensive or "giving ****" ... VIP was just pointing out you posted in wrong place as to not confuse other members and also to make sure you get an answer... You may read this as giving you **** but the intention is not... I do a lot of C# and (at least I feel) I help a lot of ppl here as best I can- I will not help you if you spaz out and you may get infractions/banned if you do... calm down some anyone who bashes newbies gets infractions pretty quick- we are all under the understanding this is a learning based environment and is targeted as such- thus we all understand the need to help and not flame others and it is quickly addressed by the mods.

Post back if you do not understand the resolution melloorr posted (or mine which is essentially the same thing) and I can try to explain it better... if you are working through things in the book and post here make sure it is a new thread for new issues and also (if you could) mark them as resolved when you get it "down"/"answered" (to do this click edit on your very first post in thread and then click go advanced there is a drop down list that you can choose from in the upper left corner)... Again I do C# and am very active here so I can help you as best as possible (idk how many C# coders are here/active)... Welcome and happy posting

alykins
12-22-2011, 12:58 AM
Edit: Posted reply twice somehow deleted so wasn't so "big"

melloorr
12-22-2011, 01:24 AM
I do not know what melloorr's first post meant... if you were to compile that code and run anywhere (ie not just in Visual Studio) it would do the same thing no matter what- there is nothing more to the program... If melloorr meant he/she uses VS then idk why he/she said it at all... as to your comment

I just meant that, in Visual Studio, if you debug, then it will go off as soon as it has wrote the line, unless you have something to actually stop it. But if you actually RUN it, then it will stay on and say 'Press Enter to continue...' (or something like that)

I didn't want to make it too complicated for him, so I was a bit vague, but as it happens, it wasn't the best idea. :/

alykins
12-22-2011, 03:26 AM
I just meant that, in Visual Studio, if you debug, then it will go off as soon as it has wrote the line, unless you have something to actually stop it. But if you actually RUN it, then it will stay on and say 'Press Enter to continue...' (or something like that)


no- if you run it outside of VS it would also end... the only way to stop it is to add something like what you added (Console.ReadLine();) or some other method to make it "sit" there... once the program has reached it's end it really has "reached it's end" ... if you would build/publish the original posted code in release mode it would do the same thing, start->throw that message to the console-> end

melloorr
12-22-2011, 03:33 PM
Well this is what I got when I ran my code without debugging:

http://i1216.photobucket.com/albums/dd368/mellor303/c-1.png

And as you can see, it has nothing to stop it from exiting

EDIT
As it happens you are right, I just ran the .exe and it went off. Suppose it only stops at the end if you run without debugging. But to be fair, I have never built a .exe before now, and I haven't coded c# for a good few months, so I have forgotten most of it :/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum