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 608103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:25:42+00:00 2026-05-13T17:25:42+00:00

Writing an asp.net mvc app and i have something like this… Public Class AA

  • 0

Writing an asp.net mvc app and i have something like this…

Public Class AA
'... has some variables...
End Class

Public Class BB
Inherits AA
Public ExtraVariable As Integer ' just adds another variable and thats it!
End Class

So, now in my program, i just want to copy object of type AA to an empty variable of type BB?

it makes sense to do this, as i expect all the fields in AA type object to be copied to the newly created BB type object, and the ExtraVariable in the BB type object i would (will) just assign a value to it later (after the copy) on my own time!!

I know copying BB type to AA type would not be appropriate as there would be lost data!

But im trying to copy AA to BB, I’ve used both DirectCast and CType to do this, and i keep getting “unable to cast” error!!!

Note: I’m using vb.net (but can read c#, no problems)

  • 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-13T17:25:43+00:00Added an answer on May 13, 2026 at 5:25 pm

    As anon says you probably want to pass AA into BB’s constructor, where you can copy all the elements:

    public class AA
    {
    //some variables
    }
    
    public class BB : AA
    {
        public BB(AA aa)
        {
        //Set BBs variables to those in AA
        someVariable= aa.someVariable
        }
    
        public int SomeExtraProperty{get;set;}
    }
    

    but obviously you’ll have any inherited constructors available to you which you could reuse if appropriate.

    EDIT

    based on some of the comments above, you could also do:

    public class BB : AA
    {
        private AA _aa;
        public BB(AA aa)
        {
        //Set BBs variables to those in AA
        _aa=aa;
        }
    
        public int SomeExtraProperty{get;set;}
    
        //override inherted members and just delegate to the internal object
        public override int SomeMethod()
        {
           return _aa.SomeMethod();
        }
    }
    

    or maybe go for the Decorator Pattern

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

Sidebar

Related Questions

Newbie question. I’m writing an ASP.Net MVC app in VB.Net and have been using
I'm writing an app using asp.net-mvc deploying to iis6. I'm using forms authentication. Usually
I am writing a small app to teach myself ASP.NET MVC, and one of
In my Asp.net MVC app, I have two methods on a controller, one for
I'm considering writing a blog hosting app in ASP.NET MVC. I'm new to .NET,
I'm writing an app with ASP.NET MVC where, for various reasons, the navigation is
I'm writing an ASP.NET MVC application and trying to use a RESX file to
I'm writing applications with ASP.NET MVC. In contrast to traditional ASP.NET you're a lot
I'm just starting to pick up ASP.Net MVC and find myself writing a lot
I was writing some ASP.NET control when I came to the scenario where I

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.