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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:43:05+00:00 2026-05-21T03:43:05+00:00

I have the following code. My intention that this custom object can be cast

  • 0

I have the following code. My intention that this custom object can be cast to type Int32 using the syntax

i = (int) obj;

where ‘obj’ is of type ‘SubClass’.

The operator for the cast is defined in each SubType of the abstract SuperType, and is also defined in the abstract SuperType. Since the operator in the abstract super type cannot be marked as virtual, a cast from the super type intentionally causes an exception to be thrown.

If I try to cast within a foreach loop, where the loop iterates over a list of type ‘SuperType’ C# attempts to cast using the operator on the SuperType (and throws the programmed exception), where I want it to cast using the operator

I was hoping that it would cast using the sub type.

I’m probably going about this the hard way, but am interested now as to whether I can achieve this in what I felt was an elegant solution.

Any advice appreciated!

// Loop…

foreach (PriceIndexAction objs in objList)
{
    int i = (int) action;
}

// SuperClass..

   public abstract class SuperClass
    { 

        protected SuperClass( DateTime p1, int p2, int p3, int p4 )
        {
           ....
        }

        public static implicit operator Int32(PriceIndexAction obj)
        {
            throw new InvalidCastException( "No cast to System.Int32 exists for the abstract class   PriceIndexAction.");
        }
    }

// SubClass..

public class SubClass : SuperClass
{

      public SubClass(DateTime p1, int p2 )
                    : base( p1, p2, 1, 2 )
      {
         ....
      }


      public static implicit operator Int32(SubClass obj)
      {
          return 2;
      }
 }`enter code here`
  • 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-21T03:43:05+00:00Added an answer on May 21, 2026 at 3:43 am
    public abstract class SuperClass
    {
        public static implicit operator int(SuperClass obj)
        {
            return obj.ToInt32();
        }
    
        protected abstract int ToInt32();
    }
    

    And then override/implement ToInt32 in derived classes.

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

Sidebar

Related Questions

The scenario We have a web application that makes a call to a web
One of my colleges seem to have an 'undefined index' error on a code
I am developing a system that will have a database backend. I am intending
hello I have a NSMutableArray iconlocarr . I also have another array containing dictionary
I am having a lot of trouble with 'base types' in the Code Only
I am using ASP.NET MVC 3 and Autofac for my dependency injection. I am
EDIT It appears the user has to enter some data for his profile, otherwise
I've just come across a pretty strange problem with VS2010 and Script#, which most
I intend to display (4, 8 or 16 bit per channel - no alpha)
Stackoverflow seems to be one of the most authoritative and easy to use technical

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.