|
-
Nov 18th, 2002, 06:29 PM
#1
Thread Starter
Frenzied Member
Return codes!!
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
-
Nov 18th, 2002, 08:01 PM
#2
Frenzied Member
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
-
Nov 18th, 2002, 09:38 PM
#3
Thread Starter
Frenzied Member
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
-
Nov 18th, 2002, 11:27 PM
#4
Addicted Member
Not sure that this is the beat way but this will make sure you string is 30 characters long.
PHP Code:
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 + "|");
}
-
Nov 19th, 2002, 12:36 AM
#5
Thread Starter
Frenzied Member
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
-
Nov 19th, 2002, 01:30 AM
#6
Member
What do u think of this code?
string name="Henrik";
while(name.Length < 30)
{
name+=" ";
}
-Sujala
-
Nov 19th, 2002, 03:09 PM
#7
Hyperactive Member
I can beat that:
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.
-scott
he he he
-
Nov 19th, 2002, 05:38 PM
#8
Thread Starter
Frenzied Member
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
-
Nov 19th, 2002, 06:00 PM
#9
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......
-
Nov 20th, 2002, 12:33 AM
#10
Member
-
Nov 20th, 2002, 01:47 AM
#11
Member
Found 2 functions specifically for padding a string. I have copied the documentation.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|