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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:00:58+00:00 2026-05-26T05:00:58+00:00

I have two ViewModels which are similar, and i need to cast one to

  • 0

I have two ViewModels which are similar, and i need to cast one to another.

This is the first one:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestInheritance.Models
{
    public class ShowMatrixQuestionViewModel : ShowQuestionViewModel
    {
        public Dictionary<MatrixRows, List<MatrixColumns>> columnrow;
        public List<MatrixColumns> columns;
        public List<MatrixRows> rows;

        public ShowMatrixQuestionViewModel()
        {
            columns = new List<MatrixColumns>();
            rows = new List<MatrixRows>();
            columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
        }
    }

    public class MatrixColumns
    {
        public int Column_ID { get; set; }
        public int Column_Number { get; set; }
        public String Column_Description { get; set; }
        public Boolean IsAnswer { get; set; }
        public int Procent { get; set; }
    }

    public class MatrixRows
    {
        public bool Delete { get; set; }
        public int Row_Id { get; set; }
        public String Row_Number { get; set; }
        public String Row_Description { get; set; }
    }

}

The second looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestInheritance.Models.ViewModels.New
{
    public class ShowMatrixProcentViewModel : ShowMatrixQuestionViewModel
    {
        // Dette er for at vi kan bruge DisplayTemplates
        public ShowMatrixProcentViewModel()
            : base()
        {
        }
    }
}

When i try to cast a ShowMatrixQuestionViewModel to a ShowMatrixProcentViewModel i get a InvalidCastException:

MatrixColumns mc = new MatrixColumns()
            {
                Column_Description = "Hej",
                Column_ID = 1,
                Column_Number = 1,
                IsAnswer = false
            };

            MatrixColumns mc2 = new MatrixColumns()
            {
                Column_Description = "Med",
                Column_ID = 1,
                Column_Number = 1,
                IsAnswer = false
            };

            MatrixRows mr = new MatrixRows()
            {
                Row_Description = "2005",
                Row_Id = 1,
                Row_Number = "1"
            };

            MatrixRows mr2 = new MatrixRows()
            {
                Row_Description = "2008",
                Row_Id = 1,
                Row_Number = "2"
            };

            List<MatrixColumns> matrixcolumns = new List<MatrixColumns>();
            matrixcolumns.Add(mc);
            matrixcolumns.Add(mc2);
            List<MatrixRows> matrixrows = new List<MatrixRows>();
            matrixrows.Add(mr);
            matrixrows.Add(mr2);

            ShowMatrixQuestionViewModel avm = new ShowMatrixQuestionViewModel()
            {
                IsAnswered = true,
                Question_ID = 1,
                Question_Number = "1",
                Question_Type = "hej",
                Question_Wording = "spørgsmål1",
                Visible = true,
                columns = matrixcolumns,
                rows = matrixrows
            };

            ShowMatrixProcentViewModel sm = (ShowMatrixProcentViewModel)avm;

return View(avm);

The exception says:

System.InvalidCastException: Unable to cast object of type ‘TestInheritance.Models.ShowMatrixQuestionViewModel’ to type ‘TestInheritance.Models.ViewModels.New.ShowMatrixProcentViewModel’.

Do i need to initialize some stuff in the ShowMatrixProcentViewModel before i can cast??

Thanks in advance

  • 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-26T05:00:59+00:00Added an answer on May 26, 2026 at 5:00 am

    Since ShowMatrixProcentViewModel inherit from ShowMatrixQuestionViewModel you can’t just cast from ShowMatrixQuestionViewModel to ShowMatrixProcentViewModel. That’s basic OO. You can only cast in the sub-class > super-class direction. The reason is that ShowMatrixQuestionViewModel doesn’t contain everything that ShowMatrixProcentViewModel does.

    You could use something like AutoMapper but your should probably look at your design/architecture instead since something like this is signs of bad structure.

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

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have two ViewModels in my MVVM app. the one is bind to main
I have two datagrids in a single view but the collections which are ItemsSource
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have a web solution (in VS2010) with two sub-projects: Domain which holds the
I have two resources Dock and Undock in my View which is a UserControl(Dock.xaml),
We have two WebApplication in one MVC solution, one for Desktop and the other
This is one of those scenarios where Paralysis by Analysis seems to have taken
I have a simple form with two fields and an optional field which is
I have two separate binding problems with listboxes with an itemtemplate which contains a

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.