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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:20:27+00:00 2026-05-31T09:20:27+00:00

Last week I handled some object casting (DataGridView Columns Control casts) and I tried

  • 0

Last week I handled some object casting (DataGridView Columns Control casts) and I tried to cast a DataGridView TextBox Column to TextBox Control where I had a compile time error.

I found out that I should cast that TextBox Column to DataGridViewTextBoxColumn.

So how does the compiler decide if a type can be cast to another type (objects mostly)?

Next you have cases where the compiler lets you to do some casts but you will get a run time error.

  • 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-31T09:20:29+00:00Added an answer on May 31, 2026 at 9:20 am

    All classes and types inherit from object. Beyond that, it depends what class another class derives from. Using this, the compiler can determine if something can be cast from one to another. Some of these checks can be determined at compile-time, and some at run-time.

    Consider:

    class A
        class B
        class C
        class D
            class E
    

    Now, give a reference to E, I can safely cast to D or A (or object). The compiler can tell that if I try and cast E to C or B no conversion is available, because E does not inherit from these 2, though they share a common base-class of A.

    Now, consider if I have a reference to A and attempt to cast to E. This check cannot be performed at compile time and will fail at runtime if the instance in question is not actually an instance of E (as, conceivably, it could be any of ‘A, B, C, D or E’).

    In addition to this, as Silvermind points out, we can provide our own implicit and explicit conversions where none exist. Consider this:

    public class Person
    {
        private string name_;
    
        public Person(string name)
        {
            name_ = name;
        }
    
        // allow conversion to a string
        public static implicit operator string(Person p)
        {
            return p.name_;
        }
    }
    

    The above allows us to go:

    Person p = new Person("Moo-Juice");
    string name = (string)p;
    

    Without our implicit operator, this would (obviously) fail at compile time as no conversion exists from Person to string by default.

    Explicit operators are similar, but work the other way. This occurs if we wanted to cast a string to a person. So we could add the following explicit operator:

    public static explicit operator Person(string name)
    {
        return new Person(name);
    }
    

    And now we can do this:

    string name = "Moo-Juice";
    Person p = (Person)name;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Last week in our java course we were introduced the Object class and some
I was writing some Unit tests last week for a piece of code that
By some coincidence this problem has come up twice in the last week. A
During last week I received some code and was asked to improve the performance.
In the last week I've created two classes which my team expressed some concerns
Last week a young student ask me if marshalling is the same as casting.
Just last week, I was doing some PHP stuff. I worked a little solution
Last week I started attempting to use Winforms to automate some reports. I am
In the last week I tried to calculate angle between three points - first
Last week I needed to test some different algorithmic functions and to make it

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.