Click to See Complete Forum and Search --> : Return codes!!
MrNorth
Nov 18th, 2002, 05:29 PM
If I want my program to abend with a certain return code, when a certain exception is raised, can I just use:
catch(blabla)
{
return 99;
}
And the program (actually the main function) abends with this returncode and exits the program...?
Which is the BEST way to handle this kinds of things... my application, yet small will run non stop in a manufacturing environment, and I want it to be as failprofe as possible. I have implemented, try, catch, finally all that is left is the returncodes to the windows app that manages ALL the programs running...
I prefer if you give any answer with ObjectOrientation in mind... classes inheritance etc... cause I am an OO gay :=)
best regards
henrik
kovan
Nov 18th, 2002, 07:01 PM
if the object ur returning is a int, double or any other numeric value, return 0 and check in main for 0
if its objects, classes, return null and check for null
MrNorth
Nov 18th, 2002, 08:38 PM
Hmmm, well I have hoped for another solution rather than to use returncodes within class methods.
Anyway, another problem has emerged.... you see I have 3 string variables I want to put together to a single string and store to a file. My problem is that the string has to be strict formatted. The app that will read this file is starting to read on certain positions, rather than searching for a delimiter... SO, my question is how do I create a string that has x nr of spaces between each parameter? Do I have to go down to byte level to do it, or is there an easier way?
for example I have variables name, town and age. To store thenm in a single string, the name start on position 0
town start on position 30 and age start on position 50. All in between these positions have to be spaces... how do I create such a string?????
best regards
Henrik
Jeremy Martin
Nov 18th, 2002, 10:27 PM
Not sure that this is the beat way but this will make sure you string is 30 characters long.
string s1, s2;
int a,b;
s1 = "my string 1";
if (s1.Length < 30)
{
a = 30-s1.Length;
b = 0;
s2 = "";
do
{
s2 += " ";
b ++;
}
while (b < a);
s1 = s1 +s2;
System.Windows.Forms.MessageBox.Show("|" + s1 + "|");
}
MrNorth
Nov 18th, 2002, 11:36 PM
Not sure if I dare to show that code to my boss... too many foobar variables for my taste... but it got the job done, nicely :) It will do for testing purposes, thanks.
If you all wonder, why I have to format the string like that... I will only say one word: COBOL
best regards
Henrik
sujalaroy
Nov 19th, 2002, 12:30 AM
string name="Henrik";
while(name.Length < 30)
{
name+=" ";
}
-Sujala
Scott Penner
Nov 19th, 2002, 02:09 PM
Console.WriteLine( string.Format("{0,-30} {1,-20} {2}", Name,Town, Age) );
>> look up "format strings, alignment"
Also, you should be able to have a global error handling method that sets a static variable which can be looked at by Main before exiting the program. Do not rely upon returning values from each method. This is not the way to do it in C#. Always throw exceptions. If you need to, you can process the exception locally, then rethrow it to bubble the actual exception up to the Main() method.
I'm not sure if that's what you are looking for so let me know if I can clarify anything.
MrNorth
Nov 19th, 2002, 04:38 PM
Thanks Scott! That really is the "right" way to do it... Even though I own a few C# books (including Herb Schildts reference bible) I had no clue this could be done...
Life is a learning process.... oh, speaking of learning, can anyone recommend a good book that involves Object orientated design (preferrably an applied-style book, with example projects), and it isn't negative if it involves C#... I have taken a 5 week course in UML and patterns, but that really didn't help me all that much... too abstract...
best regards
Henrik
techgnome
Nov 19th, 2002, 05:00 PM
Originally posted by MrNorth
If you all wonder, why I have to format the string like that... I will only say one word: COBOL
EEEWWW! Now I have to wash my eyes out from reading that......
sujalaroy
Nov 19th, 2002, 11:33 PM
Thank you, Scott !
sujalaroy
Nov 20th, 2002, 12:47 AM
You can use one of the following methods to create a new version of an existing string that is right-aligned or left-aligned a specified number of spaces. The new string can either be padded with empty spaces (also called white spaces) or with custom characters.
Method Name -- Use
String.PadLeft Right-aligns and pads a string so that its rightmost character is a specified distance from the beginning of the string.
String.PadRight Left-aligns and pads a string so that its rightmost character is a specified distance from the end of the string.
PadLeft
The String.PadLeft method creates a new string that is right-aligned so that its last character is a specified number of spaces from the first index of the string. White spaces are inserted if you do not use an override that allows you to specify your own custom padding character.
The following code example uses the PadLeft method to create a new string that has a total length of twenty spaces.
[Visual Basic]
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.PadLeft(20, "-"c))
[C#]
string MyString = "Hello World!";
Console.WriteLine(MyString.PadLeft(20, '-'));
This example displays --------Hello World! to the console.
PadRight
The String.PadRight method creates a new string that is left-aligned so that the current string is extended a specified number of spaces to the right of the string's first index. This method fills in the new string with white spaces if you do not specify a custom character.
The following code example uses the PadRight method to create a new string that has a total length of twenty spaces.
[Visual Basic]
Dim MyString As String = "Hello World!"
Console.WriteLine(MyString.PadRight(20, "-"c))
[C#]
string MyString = "Hello World!";
Console.WriteLine(MyString.PadRight(20, '-'));
This example displays Hello World!-------- to the console.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.