Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Can't compile C# from command line

Hi! Am new to programming and C# in particular. Av been following the lessons in the C# school for some time now, trying to run the examples as well as the exercises on my own. I can compile and run programs with Visual Studio.Net, but can't do the same using the command line even after following the author's instructions. Please, how do i compile and run my C# programs without Visual Studio.Net. Am I missing an important step?

Secondly, when i tried running the code example that was to take argument from the command line, the program did not wait for an input from the command line, but threw an exception instead.Here is a portion of the code:

static void Main(string[] userInput)
{
int input = int.Parse(userInput[0]);
......................
}
How do I get by this?
Thanks.

Comments

  • JonathanJonathan Member Posts: 2,914
    : Hi! Am new to programming and C# in particular. Av been following
    : the lessons in the C# school for some time now, trying to run the
    : examples as well as the exercises on my own. I can compile and run
    : programs with Visual Studio.Net, but can't do the same using the
    : command line even after following the author's instructions. Please,
    : how do i compile and run my C# programs without Visual Studio.Net.
    : Am I missing an important step?
    What error do you get? But I suspect that you need to add the directory containing the C# compiler (csc.exe - search for it on your computer) to your path.

    : Secondly, when i tried running the code example that was to take
    : argument from the command line, the program did not wait for an
    : input from the command line, but threw an exception instead.Here is
    : a portion of the code:
    :
    : static void Main(string[] userInput)
    : {
    : int input = int.Parse(userInput[0]);
    : ......................
    : }
    : How do I get by this?
    What gets passed to Main there is the command line arguments. So if you ran your program as:

    myprogram 42

    You'd get the string 42 in userInput. If you want it to wait for user input, use something more like:

    int input = int.Parse(Console.ReadLine().Trim());

    Though when you get to learning exceptions, you'll want to stick an exception handler around it in case the user enters something that isn't a number. But if that doesn't make sense to you yet, don't worry - it will when you get to that point in the school. :-)

    Hope this helps!

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • RibakeRibake Member Posts: 2
    : Hi! Am new to programming and C# in particular. Av been following
    : the lessons in the C# school for some time now, trying to run the
    : examples as well as the exercises on my own. I can compile and run
    : programs with Visual Studio.Net, but can't do the same using the
    : command line even after following the author's instructions. Please,
    : how do i compile and run my C# programs without Visual Studio.Net.
    : Am I missing an important step?
    ---------------------------------------------------------------------------
    Thank you very much. I was able to locate csc.exe in C:WINDOWSMicrosoft.NETFrameworkv2.0.50727 which i added to my path.
    However, i was able to compile only source files saved directly in C drive and not sub-drives like my desktop. I tried specifying the full paths of these source files but it won't work except i save directly in C. Pls, how can i compile my C# codes saved in sub drives of C?

    ---------------------------------------------------------------------------
    :
    : Secondly, when i tried running the code example that was to take
    : argument from the command line, the program did not wait for an
    : input from the command line, but threw an exception instead.Here is
    : a portion of the code:
    :
    : static void Main(string[] userInput)
    : {
    : int input = int.Parse(userInput[0]);
    : ......................
    : }
    : How do I get by this?
    : Thanks.
    ---------------------------------------------------------------------------
    Thanks for this as well. Since I was able to compile and run from the command line, I was able to supply my arguments while running i.e
    C:> CSharpSchool 23

    However, running the same code in Visual Studio.NET still throws an exception despite tying ur suggestion.

    Thank u so much in anticipation.
    :

  • JonathanJonathan Member Posts: 2,914
    : Thank you very much. I was able to locate csc.exe in
    : C:WINDOWSMicrosoft.NETFrameworkv2.0.50727 which i added to my
    : path.
    : However, i was able to compile only source files saved directly in C
    : drive and not sub-drives like my desktop. I tried specifying the
    : full paths of these source files but it won't work except i save
    : directly in C. Pls, how can i compile my C# codes saved in sub
    : drives of C?
    You really need to copy and paste from the command line what errors you're seeing; it's hard to know what your problem is without you doing so. Do the paths of these source files contain spaces? If so, you must put them in quotes.

    : Thanks for this as well. Since I was able to compile and run from
    : the command line, I was able to supply my arguments while running
    : i.e
    : C:> CSharpSchool 23
    :
    : However, running the same code in Visual Studio.NET still throws an
    : exception despite tying ur suggestion.
    Sure, because there are no command line arguments specified there. If you go inside the project settings somewhere and look for startup options, you should see a place to specify a command line argument. But maybe what you really want to be doing is to use Console.ReadLine() method instead.

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
Sign In or Register to comment.