none
C# - Is there a way to inherit from two different classes? RRS feed

  • Question

  • I have 3 different .cs files, say

    - Transaction.cs
    - Account.cs
    - Status.cs

    What I want to do is 

    public class Transaction: Account, Status

    And doing this will cause error since C# does not support multiple inheritance, Is there a way I can inherit from two different classes?

    // Transaction.cs
    public class Transaction
    {
            public int TransactionId { get; set; }
            public DateTime TransactionDate { get; set; }
            public decimal Amount { get; set; }
            public string Description { get; set; }
    }   

    // Account.cs
    public class Account
    {
            public int AccountId { get; set; }
            public string AccountName { get; set; }
            public string AccountType { get; set; }
    }

    // Status.cs
    public class Status
    {
            public int StatusId { get; set; }
            public string StatusName { get; set; }
    }
    Wednesday, December 11, 2019 2:40 AM

Answers

  • You can implement any number of interfaces - do the full versions of either 'Account' or 'Status' have any logic or data?  If not, as your example does not, you can change either or both to interfaces - my version below just makes 'Status' an interface:

    // Transaction.cs
    public class Transaction : Account, Status
    {
            //implementation of Status interface:
            int Status.StatusId { get; set; }
            string Status.StatusName { get; set; }
    
            public int TransactionId { get; set; }
            public DateTime TransactionDate { get; set; }
            public decimal Amount { get; set; }
            public string Description { get; set; }
    }   
    
    // Account.cs
    public class Account
    {
            public int AccountId { get; set; }
            public string AccountName { get; set; }
            public string AccountType { get; set; }
    }
    
    // Status.cs
    public interface Status
    {
            int StatusId { get; set; }
            string StatusName { get; set; }
    }
    



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Marked as answer by iHandler Thursday, December 12, 2019 3:17 AM
    Wednesday, December 11, 2019 3:51 PM

All replies

  • The only way would be for Account to inherit Status, and Transaction inherit Account.

    In any case, that does not sound right. A transaction is not an account, or a status. They sound like three completely separate classes to me, so you should not want to inherit one from another. Unless I misunderstand what they mean.

    Wednesday, December 11, 2019 2:53 AM
  • Hello,

    I agree with Ante on this. If we look at this as data to be stored say in a database the Account would be the parent table, Transaction would be a child table. Guessing the Status would be off Transaction. So unless I got this completely incorrect this ends up being a master detail relation.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, December 11, 2019 3:54 AM
    Moderator
  • You can't inherit from a base class that is created for carrying data only that has public properties. Inheriting of a class is based on the base class' methods/behavior and not properties. Properties are not methods/behavior.

    https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance

    <copied>

    Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class that reuses (inherits), extends, or modifies the behavior of a parent class. The class whose members are inherited is called the base class. The class that inherits the members of the base class is called the derived class.

    <end>

    Wednesday, December 11, 2019 6:47 AM
  • You can implement any number of interfaces - do the full versions of either 'Account' or 'Status' have any logic or data?  If not, as your example does not, you can change either or both to interfaces - my version below just makes 'Status' an interface:

    // Transaction.cs
    public class Transaction : Account, Status
    {
            //implementation of Status interface:
            int Status.StatusId { get; set; }
            string Status.StatusName { get; set; }
    
            public int TransactionId { get; set; }
            public DateTime TransactionDate { get; set; }
            public decimal Amount { get; set; }
            public string Description { get; set; }
    }   
    
    // Account.cs
    public class Account
    {
            public int AccountId { get; set; }
            public string AccountName { get; set; }
            public string AccountType { get; set; }
    }
    
    // Status.cs
    public interface Status
    {
            int StatusId { get; set; }
            string StatusName { get; set; }
    }
    



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Marked as answer by iHandler Thursday, December 12, 2019 3:17 AM
    Wednesday, December 11, 2019 3:51 PM
  • You can implement any number of interfaces - do the full versions of either 'Account' or 'Status' have any logic or data?  If not, as your example does not, you can change either or both to interfaces - my version below just makes 'Status' an interface:

    // Transaction.cs
    public class Transaction : Account, Status
    {
            //implementation of Status interface:
            int Status.StatusId { get; set; }
            string Status.StatusName { get; set; }
    
            public int TransactionId { get; set; }
            public DateTime TransactionDate { get; set; }
            public decimal Amount { get; set; }
            public string Description { get; set; }
    }   
    
    // Account.cs
    public class Account
    {
            public int AccountId { get; set; }
            public string AccountName { get; set; }
            public string AccountType { get; set; }
    }
    
    // Status.cs
    public interface Status
    {
            int StatusId { get; set; }
            string StatusName { get; set; }
    }



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Dave, you're correct.  Thanks !
    Thursday, December 12, 2019 3:17 AM