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

  • Home
  • SEARCH
  • 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 6937755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:24:17+00:00 2026-05-27T12:24:17+00:00

I have the following class hierarchy: public class AI { public AI() { }

  • 0

I have the following class hierarchy:

public class AI {
    public AI() { }
    public virtual void Update(float frameTime) { }
}

public class Boss : AI {
    public Boss() : base() { }
    public override void Update(float frameTime) { 
        Console.WriteLine("Boss Update"); 
    }
}

I have a Character that holds an AI variable that I then store a Boss instance in and try to cast it as such to get the Boss’s Update function rather than the base class’s.

AI ai = new Boss();
(Boss)ai.Update(0f);

This doesn’t work though, what is the proper method for doing this in C#? It was properly working with another AI class where I didn’t even have to cast it at all, it just ran the right version of Update, so I must have changed something unintentionally.

  • 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-27T12:24:18+00:00Added an answer on May 27, 2026 at 12:24 pm

    The dot operator has higher precedence than casting, so your code is being interpreted as:

    (Boss)(ai.Update(frameTime));
    

    You need to add an extra pair of parentheses to get what you want:

    ((Boss)ai).Update(frameTime);
    

    However it shouldn’t be necessary to perform this cast since your method is virtual.

    You may also want to consider changing your AI type to be an abstract class or (if possible) an interface.

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

Sidebar

Related Questions

Let's say I have the following class hierarchy: class Base { protected: virtual void
Say I have the following hierarchy: public class MyClass { protected virtual void Method()
I have the following class hierarchy in C++: class Base { virtual void apply()
I have the following hierarchy of classes class classOne { virtual void abstractMethod() =
I heave roughly speaking the following hierarchy class Base { public: virtual int value()
I have the following code: #include <iostream> class Grandma { public: virtual void foo()
We have the following class hierarchy: public class Base { public Base() { //
I have the following class hierarchy public abstract BaseClass : System.Web.UI.UserControl { public virtual
I have a following class hierarchy: class Base{ .... virtual bool equal(Base *); }
Consider the following hierarchy: class Base { virtual void Method() = 0; virtual void

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.