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. Passing value to base class

Passing value to base class

Scheduled Pinned Locked Moved C#
question
4 Posts 4 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.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    class MyClass
    {

        public MyClass(int b)
        {
            Console.WriteLine(b);
        }
    
        public MyClass()
            : this(43)
        {
            Console.WriteLine("Default Const");
        }
    }
    class MyClass2 : myClass 
    {
     
        public MyClass2(int b) : base(b) 
        {
            Console.WriteLine(b);
        }
    
        public MyClass2()
            
        {
            Console.WriteLine("Default Const");
        }
    }
    

    In the above code in my MyClass2 non- default constructor. It is giving a value back to the base class. I want to know if at the same time can I make the default constructor run. I mean something like this

    public MyClass2(int b) : base(b),this()

    or this

    public MyClass2(int b) : base(b) : this()

    How do I do it

    L L R 3 Replies Last reply
    0
    • H humayunlalzad

      class MyClass
      {

          public MyClass(int b)
          {
              Console.WriteLine(b);
          }
      
          public MyClass()
              : this(43)
          {
              Console.WriteLine("Default Const");
          }
      }
      class MyClass2 : myClass 
      {
       
          public MyClass2(int b) : base(b) 
          {
              Console.WriteLine(b);
          }
      
          public MyClass2()
              
          {
              Console.WriteLine("Default Const");
          }
      }
      

      In the above code in my MyClass2 non- default constructor. It is giving a value back to the base class. I want to know if at the same time can I make the default constructor run. I mean something like this

      public MyClass2(int b) : base(b),this()

      or this

      public MyClass2(int b) : base(b) : this()

      How do I do it

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      humayunlalzad wrote:

      How do I do it

      Best practice would be to refactor the code out into a member function so that it can be called.

      led mike

      1 Reply Last reply
      0
      • H humayunlalzad

        class MyClass
        {

            public MyClass(int b)
            {
                Console.WriteLine(b);
            }
        
            public MyClass()
                : this(43)
            {
                Console.WriteLine("Default Const");
            }
        }
        class MyClass2 : myClass 
        {
         
            public MyClass2(int b) : base(b) 
            {
                Console.WriteLine(b);
            }
        
            public MyClass2()
                
            {
                Console.WriteLine("Default Const");
            }
        }
        

        In the above code in my MyClass2 non- default constructor. It is giving a value back to the base class. I want to know if at the same time can I make the default constructor run. I mean something like this

        public MyClass2(int b) : base(b),this()

        or this

        public MyClass2(int b) : base(b) : this()

        How do I do it

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

        humayunlalzad wrote:

        public MyClass2(int b) : base(b),this() or this public MyClass2(int b) : base(b) : this()

        Does not compute! :) Why not just set the values in the constructor and make life easier for yourself? :)

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

        1 Reply Last reply
        0
        • H humayunlalzad

          class MyClass
          {

              public MyClass(int b)
              {
                  Console.WriteLine(b);
              }
          
              public MyClass()
                  : this(43)
              {
                  Console.WriteLine("Default Const");
              }
          }
          class MyClass2 : myClass 
          {
           
              public MyClass2(int b) : base(b) 
              {
                  Console.WriteLine(b);
              }
          
              public MyClass2()
                  
              {
                  Console.WriteLine("Default Const");
              }
          }
          

          In the above code in my MyClass2 non- default constructor. It is giving a value back to the base class. I want to know if at the same time can I make the default constructor run. I mean something like this

          public MyClass2(int b) : base(b),this()

          or this

          public MyClass2(int b) : base(b) : this()

          How do I do it

          R Offline
          R Offline
          Richard Blythe
          wrote on last edited by
          #4

          The best approach would be something like this:

          ;
          class MyClass2 : myClass
          {
          public MyClass2(int b) : base(b)
          {
          Console.WriteLine(b);
          }

          public MyClass2() : base(THE CONST VALUE) //<------------------     
          {         
              Console.WriteLine("Default Const");     
          } 
          
          
          private void 
          

          }

          "Make it as simple as possible, but no simpler" Issac Newton

          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