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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:49:22+00:00 2026-05-15T07:49:22+00:00

The Problem It’s something I came across a while back and was able to

  • 0

The Problem

It’s something I came across a while back and was able to work around it somehow. But now it came back, feeding on my curiosity – and I’d love to have a definite answer.

Basically, I have a generic dgv BaseGridView<T> : DataGridView where T : class. Constructed types based on the BaseGridView (such as InvoiceGridView : BaseGridView<Invoice>) are later used in the application to display different business objects using the shared functionality provided by BaseGridView (like virtual mode, buttons, etc.).

It now became necessary to create a user control that references those constructed types to control some of the shared functionality (eg. filtering) from BaseGridView. I was therefore hoping to create a public property on the user control that would enable me to attach it to any BaseGridView in Designer/code: public BaseGridView<T> MyGridView { get; set; }. The trouble is, it doesn’t work 🙂 When compiled, I get the following message:

The type or namespace name ‘T’ could not be found (are you missing a using directive or an assembly reference?)

Solutions?

I realise I could extract the shared functionality to an interface, mark BaseGridView as implementing that interface, and then refer to the created interface in my uesr control.

But I’m curious if there exists some arcane C# command/syntax that would help me achieve what I want – without polluting my solution with an interface I don’t really need 🙂

EDIT: For reference, I did try this innocent workaround: BaseGridView<object> MyGridView { get; set; }, and… it still isn’t the answer: Cannot implicitly convert type ‘InvoiceGridView’ to ‘BaseGridView<object>’.

Partial success (edit 2)

Ok, because covariance is only supported on interfaces, I admitted defeat and defined an interface (only showing some of it):

public interface IBaseGridView<out T> where T : class
{
    bool ScrollTo(Predicate<T> criteria);
    bool ScrollTo(T object);
}

I am now able to cast my beloved InvoiceGridView to an IBaseGridView<object> – which is awesome and I’m a happy boy again 🙂 However, the second ScrollTo is giving me trouble upon compilation:

Invalid variance: The type parameter ‘T’ must be contravariantly valid on ‘GeParts.Controls.IBaseGridView.ScrollTo(T)’. ‘T’ is covariant.

I’m now having to modify the signature to ScrollTo(object o) – which isn’t ideal but gets the job done. What suprised me was that the compiler complained about the second ScrollTo yet was happy with the first one. So it seems that one isn’t allowed to pass instances of an out T, but using the type itself (eg. in Predicate<T>) is fine? Seems rather picky…

  • 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-15T07:49:23+00:00Added an answer on May 15, 2026 at 7:49 am

    Since you wrote

    But I’m curious if there exists some arcane C# command/syntax that would help me achieve what I want

    I’d like to add that C# 4.0 makes it possible to substitute derived types for a base type using < out T > for covariance. So you could do

    public BaseGridView<Object> MyGridView { get; set; }

    So you get a well known type but you can return whatever BaseGridView you want. The only catch is unfortunately that covariance is only allowed on interfaces! 🙁

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

Sidebar

Ask A Question

Stats

  • Questions 451k
  • Answers 451k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use a HTML parser like Jsoup. String html1 = "<div… May 15, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer See the W3 Unicode in XML and other markup languages… May 15, 2026 at 8:49 pm
  • Editorial Team
    Editorial Team added an answer You can do it with a behavior applied to the… May 15, 2026 at 8:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.