Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. calling base-constructor

calling base-constructor

Scheduled Pinned Locked Moved C#
helpcsharpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rupel
    wrote on last edited by
    #1

    hi, i'm designing an exception for my program and have the following problem. i want an additional constructor, which gets an int as paramter, builds a string out of it and calls the base-classes constructor with a string. something like

    class MyException : ApplicationExcpetion
    {
    ...
    public MyException(int errorCode)
    {
    string message;
    switch(errorCode)
    {
    case // blabla, setting message here correctly
    default: message="unkown errorcode"; break;
    }
    base(message);
    }
    }

    but C# tells me: "Use of keyword base is not valid in this context" :( i'm stuck. anyone any idea? thx! :wq

    J 1 Reply Last reply
    0
    • R Rupel

      hi, i'm designing an exception for my program and have the following problem. i want an additional constructor, which gets an int as paramter, builds a string out of it and calls the base-classes constructor with a string. something like

      class MyException : ApplicationExcpetion
      {
      ...
      public MyException(int errorCode)
      {
      string message;
      switch(errorCode)
      {
      case // blabla, setting message here correctly
      default: message="unkown errorcode"; break;
      }
      base(message);
      }
      }

      but C# tells me: "Use of keyword base is not valid in this context" :( i'm stuck. anyone any idea? thx! :wq

      J Offline
      J Offline
      jparsons
      wrote on last edited by
      #2

      You can only call the base constructor like the following. class MyException: ApplicationException { pubilc MyException(int errorcode) : base ( MyException.GetErrorString(errorcode) ) { // Constructor logic } } The base constructor can only be called in that location. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

      R L 2 Replies Last reply
      0
      • J jparsons

        You can only call the base constructor like the following. class MyException: ApplicationException { pubilc MyException(int errorcode) : base ( MyException.GetErrorString(errorcode) ) { // Constructor logic } } The base constructor can only be called in that location. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

        R Offline
        R Offline
        Rupel
        wrote on last edited by
        #3

        yeah, with GetErrorString being a static function is does work - thanks a lot! sometimes it's like a blocking in your head, when you're coding all day long... :rolleyes: :wq

        1 Reply Last reply
        0
        • J jparsons

          You can only call the base constructor like the following. class MyException: ApplicationException { pubilc MyException(int errorcode) : base ( MyException.GetErrorString(errorcode) ) { // Constructor logic } } The base constructor can only be called in that location. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          I would also prefer to be able to delay the base construction. Look at my case. class MyException: ApplicationException { int errorcode; pubilc MyException() { errorcode = GetErrorCode(); base ( MyException.GetErrorString(errorcode) ); } } Now I wouldnt have to call the GetErrorCode() function everytime when i need to throw it :) Maybe there is a way to pre-initialise some variables...:confused: MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

          J 1 Reply Last reply
          0
          • L leppie

            I would also prefer to be able to delay the base construction. Look at my case. class MyException: ApplicationException { int errorcode; pubilc MyException() { errorcode = GetErrorCode(); base ( MyException.GetErrorString(errorcode) ); } } Now I wouldnt have to call the GetErrorCode() function everytime when i need to throw it :) Maybe there is a way to pre-initialise some variables...:confused: MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

            J Offline
            J Offline
            jparsons
            wrote on last edited by
            #5

            leppie wrote: Maybe there is a way to pre-initialise some variable You could use a static constructor to preinitialize some variables. Jared jparsons@jparsons.org www.prism.gatech.edu/~gte477n

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups