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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:16:33+00:00 2026-05-24T11:16:33+00:00

I have two very similar but not identical C# objects. I am copying the

  • 0

I have two very similar but not identical C# objects. I am copying the values from one class to another.

Each class has some properties that expose an enumeration type. The inside of the enumerations are the same but the names are different e.g.

public enum EnumA
{
 A,
 B
} 

public EnumA EnumAProperty
{
 get{ return enumA;}
}

public enum EnumB
{
 A,
 B
} 

public EnumB EnumBProperty
{
 get{ return enumB;}
}

I want to assign the value returned from EnumBProperty to EnumAProperty is this possible?

  • 1 1 Answer
  • 4 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-24T11:16:33+00:00Added an answer on May 24, 2026 at 11:16 am

    You can do via casting but I would not recommend it as it is fragile — if any of the enum members are reordered or new items added the result may not be what you expect.

    EnumAProperty = (EnumA) EnumBProperty;
    

    Even worse with the casting is if you have items in your source enum with no equivalent in the destination — below there are more colours than shapes:

    enum Color { Red = 0, Yellow = 1, Blue = 2 };
    enum Shape ( Square = 0, Triangle = 1 };
    
    Color color = Color.Red;
    Shape shape = (Shape) color;
    

    shape could end up with the value 2 even though this value is not defined.

    Instead, I’d suggest you use a switch statement to map:

    EnumAProperty = ConvertToA(EnumBProperty);
    
    ...
    
    private static EnumA ConvertToA(EnumBProperty b)
    {
        switch (b)
        {
            case EnumB.Flannel: return EnumA.HandTowel;
            case EnemB.Vest: return EnumA.UnderShirt;
            ...
            default: throw new ArgumentOutOfRangeException("b");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two very similar programs each trying to run two threads OddThread and
I have two tables that have virtually identical content and very similar structure. They
I'm not very good in SQL and HQL... I have two domains: class Hotel
I am trying to use one join model for two separate but very similar
I have two controls. The XAML's are big and very similar. One difference is
I have two Haskell functions, both of which seem very similar to me. But
I have two queries that are functionally identical. One of them performs very well,
I have two very simple, identical UITableViews in my app that are populated with
I have two arrays, one is very large (more than million entries) and other
I have two very similar specs for two very similar controller actions: VoteUp(int id)

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.