Simple managed app won't work...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Excuse me if you consider this a cross post, however, it was originally destined for this group not the vc.atl group, but somehow it went there instead of here! (obviously it was something I did, but hey ho) ... now back to the problem in hand

I've made what I consider to be the simplest of applications, but it won't work. This is my code

#using <mscorlib.dll>
#include <tchar.h>
using namespace System;

int _tmain(void)
{
String* args[] = Environment::GetCommandLineArgs();

try
{
try
{
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(args->Length)));
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR WITH args\n---------------\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

if(!args)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(args->Length > 1)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[0]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(S"I am a programmer.");
}
Console::WriteLine(S"");
}
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR\n-----\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

return 0;
}

and this is the result of running the application:

W:\C++\MTest\Debug>MTest

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

ERROR
-----

Input string was not in a correct format.
Int32 ParseInt32(System.String, System.Globalization.NumberStyles, System.Globalization.Nu
mberFormatInfo)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at main() in w:\c++\mtest\mtest.cpp:line 36

W:\C++\MTest\Debug>MTest 2

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

You supplied too many arguments
Vielleicht das nachster zeit!
 
Looks like you want an app that takes a single command-line argument (eg. 'mtest 3'), and it specifying the number of times 'I am a programmer.' is displayed.

args->length is the number of command-line parts - of which there is at least 1, the full path to the app itself (eg. 'c:\test\mtest.exe'). So change it your code to;

if(args->length < 2)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(args->Length > 2)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[1]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(S"I am a programmer.");
}
Console::WriteLine(S"");
}

billr said:
Excuse me if you consider this a cross post, however, it was originally destined for this group not the vc.atl group, but somehow it went there instead of here! (obviously it was something I did, but hey ho) ... now back to the problem in hand

I've made what I consider to be the simplest of applications, but it won't work. This is my code

#using <mscorlib.dll>
#include <tchar.h>
using namespace System;

int _tmain(void)
{
String* args[] = Environment::GetCommandLineArgs();

try
{
try
{
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(args->Length)));
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR WITH args\n---------------\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

if(!args)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(args->Length > 1)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[0]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(S"I am a programmer.");
}
Console::WriteLine(S"");
}
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR\n-----\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

return 0;
}

and this is the result of running the application:

W:\C++\MTest\Debug>MTest

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

ERROR
-----

Input string was not in a correct format.
Int32 ParseInt32(System.String, System.Globalization.NumberStyles, System.Globalization.Nu
mberFormatInfo)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at main() in w:\c++\mtest\mtest.cpp:line 36

W:\C++\MTest\Debug>MTest 2

ERROR WITH args
---------------

Object reference not set to an instance of an object.
Int32 main()
at main() in w:\c++\mtest\mtest.cpp:line 20

You supplied too many arguments
Vielleicht das nachster zeit!
 
martink said:
Looks like you want an app that takes a single command-line argument (eg. 'mtest 3'), and it specifying the number of times 'I am a programmer.' is displayed.

args->length is the number of command-line parts - of which there is at least 1, the full path to the app itself (eg. 'c:\test\mtest.exe'). So change it your code to;

that makes sense, however, this is the line throwing the first Exception
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(args->Length)));

and the Exception message is
Object reference not set to an instance of an object.

This Exception is thrown whether or not any command line args are supplied
 
Thank you martink, with your help I seem to have found the problem, though a little more reading is obviously required with regards to property accessors.

First off, I must adjust my array checking accordingly, to take into account the fact that the first command-line arg is the full path to the app.

Next, instead of simpy using the args->Length property I have changed the code to call the C++ accessor function. i.e. args->get_Length(), and this works a treat.

thus the fresh code looks like this ....

try
{
int len = args->get_Length() - 1;

try
{
Console::WriteLine(String::Format(S"You supplied {0} arguments", __box(len)));
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR WITH args\n---------------\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}

if(len == 0)
Console::WriteLine(S"\nYou didn't supply any arguments\nVielleicht das nachster zeit!\n\n");
else if(len > 1)
Console::WriteLine(S"\nYou supplied too many arguments\nVielleicht das nachster zeit!\n\n");
else
{
int counter = Int32::Parse(args[1]);
for(int i = 0; i < counter; i++)
{
Console::WriteLine(String::Format(S"{0}: I am a programmer.", __box(i+1)));
}
Console::WriteLine(S"");
}
}
catch(Exception* e)
{
Console::WriteLine(S"\nERROR\n-----\n");
Console::WriteLine(e->Message);
Console::WriteLine(e->TargetSite);
Console::WriteLine(e->StackTrace);
}
 
Back
Top