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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:13:33+00:00 2026-05-15T13:13:33+00:00

A simple question… I have an abstract class Cell and two classes BorderCell and

  • 0

A simple question…

I have an abstract class Cell and two classes BorderCell and BoardCell, which inherit from the Cell class. Then I have a Cells array with the type of Cell[], which contains objects of the type BorderCell and BoardCell.

abstract class Cell 
{
}
class BorderCell : Cell 
{
    public void Method1(){};
}
class BoardCell: Cell
{
    public void Method2(){};
}

...

Cell[] Cells = new Cell[x];
for (int i = 0; i < x; i++){
    Cells[i] = new BorderCell();
    // or
    Cells[i] = new BoardCell();
}

Now I want to cast a cell to BorderCell and run its Method1, like this:

(Border)Cells[i].Method1();

But this doesn’t work, I have to use:

BorderCell borderCell = (BorderCell)Cells[i];
borderCell.Method1();

Is this the only (and the right way) to do this)?

  • 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-15T13:13:33+00:00Added an answer on May 15, 2026 at 1:13 pm

    No, you just need brackets to make it clear what you want the cast to apply to:

    ((Border)Cells[i]).Method1();
    

    Basically the “.” binds tighter than the cast, so your original code:

    (Border)Cells[i].Method1();
    

    is equivalent to:

    (Border)  (Cells[i].Method1());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question, say I have two regular expressions rtmp.*?\b/ rtmp.+?\b/ It seems to be
Simple question about Jquery-UI sortable lists (http://jqueryui.com/demos/sortable/#default) I have made: <ul id=sortable> <li class=ui-state-default>An
Simple question: Will this hierarchy cause a DP? //Abstract base class class A {
Simple question, I have code like this: class Context[A] { def t: A }
Simple question - In c++, what's the neatest way of getting which of two
Simple question (I hope): I have a simple two column table, a measurement and
Simple question: Is there a class or interface that encapulates the getting of a
Simple question to which I can't find any nice answer by myself: Let's say
Simple question I hope.... I have a Win Form with a Tab Control and
simple question about intent action naming convention: I have a new service that handles

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.