Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1049323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:35:51+00:00 2026-05-16T16:35:51+00:00

I have two classes. The base class is A. The inherited class is B.

  • 0

I have two classes. The base class is A. The inherited class is B. I would like copy a base class from one object into the base class of another object without affecting the original class. However, .NET seems to ignore the copying. Is this not possible in .NET. I know this is possible in C++. I have included C++ code to illustrate what I am trying to achieve.

I understand in that in this particular example I can directly assign the value bClass.ValueA = aClass.ValueA. But what if Class A had private members? This would not be possible.

Example Classes

Public Class A
    Public ValueA As String

End Class

Public Class B
    Inherits A
    Public ValueB As String

End Class

The Code

    Dim aClass As New A
    Dim bClass As New B

    aClass.ValueA = "AClass"

    bClass.ValueA = "BClass"
    bClass.ValueB = "BClass"

    Dim baseBClass As A
    baseBClass = CType(bClass, A)
    baseBClass = aClass

Results:

aClass.ValueA = “AClass”

bClass.ValueA = “BClass”

bClass.ValueB = “BClass”

baseBClass.ValueA = “AClass”

Intended Results:

aClass.ValueA = “AClass”

bClass.ValueA = “AClass”

bClass.ValueB = “BClass”

baseBClass.ValueA = “AClass”

C++ Explanation Comparison

#include <string>
#include <iostream>

using namespace std;

class A {
public:
    string ValueA;
};

class B : public A {
public:
    string ValueB;
};



int main(int argc, char *argv[]) {
    A aClass;
    B bClass;
    aClass.ValueA = "AClass";

    bClass.ValueA = "BClass";
    bClass.ValueB = "Blcass";

    cout << aClass.ValueA <<endl;
    cout << bClass.ValueA << endl;
    cout << bClass.ValueB << endl;

    A *baseBClass;
    baseBClass = (A*) &bClass;
    *baseBClass = aClass;

    cout << aClass.ValueA <<endl;
    cout << bClass.ValueA << endl;
    cout << bClass.ValueB << endl;
    cout << baseBClass->ValueA << endl;


    return 0;
}

Intended and Actual Results:

aClass.ValueA = “AClass”

bClass.ValueA = “AClass”

bClass.ValueB = “BClass”

baseBClass->ValueA = “AClass”

I do not think this is possible without pointers. I have tried

Ctype(bClass, A) = aClass
Directcast(bClass, A) = aClass

I get this Error:

Expression is a value and therefore
cannot be the target of an assignment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:35:52+00:00Added an answer on May 16, 2026 at 4:35 pm

    One alternative is to have a C# class that does the pointer manipulation and adds extension method to the B Class. It would be very similar to the C++ solution. This seems the best solution as it would be almost identical to the C++ implementation.

    Instead I used reflection. It is only a shallow copy but in this case it does not matter. C++ default copy constructor is only a shallow clone. With some embellishment you can check to see if IClonable exists and attempt to clone it, otherwise instantiate a new object. I have updated this to include private members.


    Imports System.Runtime.InteropServices
    Imports System.Reflection
    
    Public Class A
        Private ValueA As String
        Public Sub New(ByVal ValueA As String)
            Me.ValueA = ValueA
        End Sub
        Public Overrides Function ToString() As String
            Return String.Format("{0}:{1}={2}", _
            Me.GetType.Name, "ValueA", ValueA)
        End Function
    End Class
    
    Public Class B
        Inherits A
        Private ValueB As String
        Public Sub New(ByVal ValueA As String, ByVal ValueB As String)
            MyBase.New(ValueA)
            Me.ValueB = ValueB
        End Sub
        Public Overrides Function ToString() As String
            Return String.Format("{0}:{1}={2},{3}", _
            Me.GetType.Name, "ValueB", ValueB, MyBase.ToString)
        End Function
    End Class
    
    Module Inheritence
    
        Sub Main()
            Dim aClass As New A("AClass")
            Dim bClass As New B("BClass", "BClass")
    
            Console.WriteLine(aClass)
            Console.WriteLine(bClass)
    
            For Each item As FieldInfo In aClass.GetType.GetFields( _
                                                 BindingFlags.Public _
                                                 Or BindingFlags.Instance _
                                                 Or BindingFlags.NonPublic _
                                                 Or BindingFlags.Static)
                item.SetValue(bClass, item.GetValue(aClass))
            Next
            Console.WriteLine(aClass)
            Console.WriteLine(bClass)
            Console.ReadKey()
        End Sub
    
    End Module
    

    Base Class Clone

    This is the site I based my solution upon. It is a shallow clone but easy and quick to implement
    http://www.codeproject.com/KB/cs/cloneimpl_class.aspx

    This solution is better as it attempts to do a deep clone on objects that support IClonable otherwise it just sets a new instance.
    http://whizzodev.blogspot.com/2008/03/object-cloning-using-il-in-c.html

    Clone Routines:

    This is an interesting site that explains various cloning techniques.
    http://developerscon.blogspot.com/2008/06/c-object-clone-wars.html

    Another interesting Resource.
    Deep cloning objects

    Discussion about Deep cloning
    Create a Deep Copy in C#

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.