4

Ok, this question has been answered in SO and here is the How to pass value to base constructor

public SMAPIException( string message) : base(message)
{
    TranslationHelper instance = TranslationHelper.GetTranslationHelper; // singleton class
    string localizedErrMessage = instance.GetTranslatedMessage(message, "" );
    // code removed for brevity sake.
}

But suppose I want to manipulate the "message" info and then set the base class constructor, then how to do it.

Pseudo code below:

public SMAPIException( string message) : base(localizedErrMessage)
{
    TranslationHelper instance = TranslationHelper.GetTranslationHelper; // singleton class
    string localizedErrMessage = instance.GetTranslatedMessage(message, "" );
    // code removed for brevity sake.
}

// So basically I want the localizedErrMessage to be sent instead of message to base class constructor, is it possible? Please guide me.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • 9
    You can create a method that takes the message changes it and returns that and call it in your base constructor like `: base(MakeChanges(message))`. – juharr Sep 07 '18 at 14:04
  • @juharr: so in many files I need to do this, so do I need to replicate this small helper method all across my code. – Unbreakable Sep 07 '18 at 14:10
  • 2
    why don't you just manipulate the message within the base constructor instead of the child class? Especially if TranslationHelper is a singleton.... – KING Sep 07 '18 at 14:14
  • 1
    Possible duplicate of [Modifying parameter values before sending to Base constructor](https://stackoverflow.com/questions/1651444/modifying-parameter-values-before-sending-to-base-constructor) – Matt Rowland Sep 07 '18 at 14:20
  • @Unbreakable Just make it an helper method in a static class and reuse it as needed. – juharr Sep 07 '18 at 15:03
  • @juharr: Yes that works. Thank you for the inputs. :) – Unbreakable Sep 07 '18 at 15:09

3 Answers3

6

This should work:

public class SMAPIException : Exception
{
    public SMAPIException(string str) : base(ChangeString(str))
    {
        /*   Since SMAPIException derives from Exceptions we can use 
         *   all public properties of Exception
         */
        Console.WriteLine(base.Message);
    }

    private static string ChangeString(string message)
    {
        return $"Exception is: \"{message}\"";
    }
}

Note that ChangeString has to be static !

Example:

SMAPIException ex = new SMAPIException("Here comes a new SMAPIException");

//  OUTPUT //
// Exception is "Here comes a new SMAPIException"     

Inspecting your BaseType:

// Summary:
//     Initializes a new instance of the System.Exception class with a specified error
//     message.
//
// Parameters:
//   message:
//     The message that describes the error.
public Exception(string message);

Calling base(string message) is the same as new Exception("message")

So you can get the passed value using the Message-Property.

BUT ! this only works if SMAPIException does not hide it's base member new string Message {get; set;} !

Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • @FelixD.: Ok, I want to access the same manipulated string in my derived class constructor too, is it possible, intellisense says I have something like base.Message (Is it same as the manipulated string), if not how to access the same manipulated string in derived constructor. Kindly note that I dont want to call the helper method again in derived class. – Unbreakable Sep 07 '18 at 14:32
  • @MattRowland : Can you kindly guide me, how to have access to that manipulated string in derived class too. Kindly note that SMAPIException derives from `Exception` C Csharp class. – Unbreakable Sep 07 '18 at 14:35
  • I see. So in Message property of base class the `ChangeString(str)` message gets set. Thanks! – Unbreakable Sep 07 '18 at 14:43
3

Have a static factory method, and make the constructor private:

class SMAPIException
{
    private SMAPIException(string message) : base(message)
    {
        // whatever initialization
    }

    public static SMAPIException CreateNew(string message)
    {
        string localizedErrMessage;
        // do whatever to set localizedErrMessage

        return SMAPIException(localizedErrMessage);
    }
}

You can then use:

SMAPIException localizedEx = SMAPIException.CreateNew("unlocalizedString");
Felix D.
  • 4,811
  • 8
  • 38
  • 72
Ian
  • 1,221
  • 1
  • 18
  • 30
0

You can call your static method in the parameter list of the base class constructor.

public SMAPIException( string message) 
      : base(TranslationHelper.GetTranslationHelper.GetTranslatedMessage(message, ""))
{
}
Antoine V
  • 6,998
  • 2
  • 11
  • 34