Results 1 to 11 of 11

Thread: Return codes!!

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    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

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  4. #4
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    Not sure that this is the beat way but this will make sure you string is 30 characters long.

    PHP Code:
    string s1s2;
    int a,b;

    s1 "my string 1";

    if (
    s1.Length 30)
    {
        
    30-s1.Length;
        
    0;

        
    s2 "";
        do
        {
            
    s2 += " ";
            
    ++;
        }
        while (
    a);
                    
        
    s1 s1 +s2;
        
    System.Windows.Forms.MessageBox.Show("|" s1 "|");


  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  6. #6
    Member
    Join Date
    Nov 2002
    Posts
    32

    What do u think of this code?

    string name="Henrik";
    while(name.Length < 30)
    {
    name+=" ";
    }

    -Sujala

  7. #7
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    Cool 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

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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......
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Member
    Join Date
    Nov 2002
    Posts
    32
    Thank you, Scott !

  11. #11
    Member
    Join Date
    Nov 2002
    Posts
    32

    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
  •  



Click Here to Expand Forum to Full Width