Skip to content
Fix Code Error

Calling the base constructor in C#

March 13, 2021 by Code Error
Posted By: lomaxx

If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that?

For example, if I inherit from the Exception class I want to do something like this:

class MyExceptionClass : Exception
{
     public MyExceptionClass(string message, string extraInfo)
     {
         //This is where it's all falling apart
         base(message);
     }
}

Basically what I want is to be able to pass the string message to the base Exception class.

Solution

Modify your constructor to the following so that it calls the base class constructor properly:

public class MyExceptionClass : Exception
{
    public MyExceptionClass(string message, string extrainfo) : base(message)
    {
        //other stuff here
    }
}

Note that a constructor is not something that you can call anytime within a method. That’s the reason you’re getting errors in your call in the constructor body.

Answered By: Jon Limjap

Related Articles

  • Vue.JS & Spring Boot - Redirect to homepage on 404
  • How to limit the number of total animated drop cubes…
  • How to filter a RecyclerView with a SearchView
  • Javax.net.ssl.SSLHandshakeException:…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Smart way to truncate long strings
  • Nested class: Calling child class properties in parent class
  • Throw HttpResponseException or return…
  • How can I throw CHECKED exceptions from inside Java…
  • What is a NullReferenceException, and how do I fix it?
  • What are the calling conventions for UNIX &…
  • error: expected class-name before ‘{’ token
  • How does JavaScript .prototype work?
  • Why do Python classes inherit object?
  • I want to create a SQLite database like in the…
  • After a little scroll, the sticky navbar just is not…
  • Active tab issue on page load HTML
  • Using NextJS to do SSR with Class Components and…
  • Are static methods inherited in Java?
  • org.springframework.beans.factory.BeanCreationExcept…
  • Automapper Aftermap alternative for Nested Mapping
  • Getting value from appsettings.json in .net core
  • Restful API service
  • How to fix Hibernate LazyInitializationException:…
  • Convert Json String to C# Object List
  • How to iterate through a JSON response with nested…
  • Item position in RecyclerView only changing when…
  • org.hibernate.MappingException: Could not determine…
  • Usage of __slots__?
  • I need to extract working hour breaks out of a Time…
  • UnsatisfiedDependencyException: Error creating bean…
  • What are the nuances of scope prototypal /…
  • Max function using divide and conquer approach is…
  • render function or template not defined in…
  • What are access specifiers? Should I inherit with…
  • TLS 1.3 server socket with Java 11 and self-signed…
  • Why not inherit from List?
  • Iterator invalidation rules
  • HQL Responding with…
  • Validation failed for one or more entities. See…
  • Remix error The transaction has been reverted to the…
  • Polymer paper input error icons
  • What is and how to fix…
  • Post request with many-to-many relation
  • Playing HTML5 video on fullscreen in android webview
  • model validation error message isn't working And…
  • Aurelia, long list of bindables on custom element. Refactor?
  • Xamarin iOS new StackTrace() kills App: Assertion…
  • Convert Json to Datatable when json has multiple levels
  • Confused about the Visitor Design Pattern
  • Binding a WPF ComboBox to a custom list
  • Proper use of the IDisposable interface
  • ASP.NET Core Web API exception handling
  • Autowiring fails: Not an managed Type
  • LINQ query to include the parent name in the child item
  • What's wrong with overridable method calls in constructors?
  • Reset/remove CSS styles for element only
  • java-POST method 415 Error
  • How to generate JAXB classes from XSD?
  • Select Tag Helper in ASP.NET Core MVC
  • Bulk insert into PostgreSQL using dapper in .NET core
  • Ef Core foreign key unique which is not what I want
  • The target ... overrides the `OTHER_LDFLAGS` build…
  • Why postman indicated 404 not found
  • Do subclasses inherit private fields?
  • Composition or Inheritance for classes with almost…
  • IOException: read failed, socket might closed -…
  • Typescript error : A 'super' call must be the first…
  • Create code first, many to many, with additional…
  • How to create a Carousel in NextJS?
  • How to "properly" create a custom object in JavaScript?
  • Inheriting constructors
  • Error: the entity type requires a primary key
  • Aurelia js providing dynamic content to popover body
  • ORA-00984: column not allowed here
  • how to make column bool data to be true with the…
  • Fix top buttons on scroll of list below
  • How to create ranges and synonym constants in C#?
  • Different rendering techniques in emberjs handlebars…
  • JSON Response Formatted Odd
  • {" was not expected.} Deserializing Twitter XML
  • How do Mockito matchers work?
  • How to download and save an image in Android
  • Creating a singleton in Python
  • Is it possible to apply CSS to half of a character?
  • CRUD - Create select dropdown list autofills another…
  • When I use '/js/app.js' on my Laravel view my…
  • Inheriting from a template class in c++
  • Do you (really) write exception safe code?
  • mapping type Issue with nested objects in AutoMapper
  • How does Python's super() work with multiple inheritance?
  • How do I ignore files in Subversion?
  • How to end C++ code
  • Jquery fadeToggle Trouble
  • recyclerview opens wrong item after filtering list…
  • typescript - cloning object
  • How Should I Declare Foreign Key Relationships Using…
  • javax.xml.bind.JAXBException: Class *** nor any of…
  • String MinLength and MaxLength validation don't work…
  • How to post data to a table that has a many to many…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to compare arrays in JavaScript?

Next Post:

What does “use strict” do in JavaScript, and what is the reasoning behind it?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error