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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:54:33+00:00 2026-06-15T21:54:33+00:00

I have a class: public class ClassA<T> { public T Value {get; set;} public

  • 0

I have a class:

public class ClassA<T>
{
  public T Value {get; set;}
  public string Description {get; set;}
}

ClassA can have a value of any type, however: I need to be able to set other properties of anything that is ClassA regardless of the type value. So I have a method:

public void DoWork(ClassA<object> source, ClassA<object> destination)
{
  destination.Description = source.Description;
}

However when I try to do this I get a runtime error saying that:
cannot convert from ClassA<DateTime> to ClassA<object>

I thought that anything could be passed as an object… I’ve looked all over the web for something similar, maybe I’m just not getting the keywords right.

Anyone have any suggestions?

Thanks

  • 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-06-15T21:54:34+00:00Added an answer on June 15, 2026 at 9:54 pm

    You won’t be able to do that.

    If you could pass a ClassA<int> into a method accepting a ClassA<object> wrapper then that method would be able to call wrapper.Value = "not an integer";. What should happen then? You’ve just set a string to an object that can only hold integers. Since the compiler knows it can’t enforce what you could put it, it just doesn’t let you pass the object in the first place.

    As for the actual solution, two come to mind.

    You could make your method generic, if that’s an option:

    public void DoWork<TSource, TDestination>(ClassA<TSource> source, ClassA<TDestination> destination)
    {
      destination.Description = source.Description;
    }
    

    Or you could make a covariant interface:

    public interface IWrapper<out T>
    {
        T Value {get;}
        string Description {get;set;}
    }
    
    public class ClassA<T> : IWrapper<T>
    {
        //...
    }
    
    public void DoWork(IWrapper<object> source, IWrapper<object> destination)
    {
      destination.Description = source.Description;
    }
    

    For your DoWork method you don’t even actually use the Value, so you could make the interface non-generic, non-covariant, and just remove Value. The advantage here is that you could use that interface to access the value properties of any number of ClassA objects that have a common base type.

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

Sidebar

Related Questions

i have this class public class Image { public string url { get; set;
I have a class: public class LevelInfo { public int Id { get; set;
I have the following classes: public class Person { public String FirstName { set;
In AS3, if I have a class such: public class dude { //default value
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
I'm creating a class that will have one public method, which returns a value
I have a model similar to this: public class myModel { public ClassA ObjectA
I have a class like so: public abstract class ClassA<T> { protected ClassA(IInterface interface)
I have class with back reference: public class Employee : Entity { private string
i have a class: public class Vars { public static final String NAME =

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.