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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:42:47+00:00 2026-06-02T12:42:47+00:00

Suppose I have these two objects: public class Object1 { string prop1; string prop2;

  • 0

Suppose I have these two objects:

public class Object1
{
  string prop1;
  string prop2;
}

public class Object2
{
  string prop1;
  int prop2;
  int prop3;
}

and the following classes & methods:

public class Object1Service
{
  public Object1 GetObject(Object1 o) { return o; }
  public void SaveProperty2(Object1 o, string s) { o.prop2 = s; }
}

public class Object2Service
{
  public Object2 GetObject(Object2 o) { return o; }
  public void SaveProperty2(Object2 o, int i) { o.prop2 = i; }
}

How do I turn this into a generic?

I preferably need something so that the services implement an interface and just call some generic base class if possible.

Would it help if the two objects share a common parent class? If so, how will they be structured?


Addendum:

It’s the return that is my real problem.

public T GetObjectByKey<T>(string key)
{
    using (DBEntities db = new DBEntities())
    {
        try
        {
            T returnedEntity = default(T);

            switch (EntityDictionary[typeof (T)])
            // I have a dictionary setup like this dictionary<type, string>
            {
                case "Object1" :
                    returnedEntity = ( from r in db.ObjectONESets
                                        where r.prop1 == key
                                        select new T
                                        {
                                            prop1 = r.prop1,
                                            prop2 = r.prop2
                                        }).FirstOrDefault();
                    break ;
                case "Object2" :
                    returnedEntity = ( from r in db.ObjectTWOSets
                                        where r.prop1 == key
                                        select new T
                                        {
                                            prop1 = r.prop1,
                                            prop2 = r.prop2,
                                            prop3 = r.prop3
                                        }).FirstOrDefault();
                    break ;
            }
            return returnedEntity;
        }
        catch (NullReferenceException )
        {
            return default(T);
        }
    }
}

Without putting ALL my object properties in the base object, it has no way of knowing that prop1-3 is a property of T.
If I do put all properties (common or not) in the base object, and if I need Object1, it then has a prop3 attached to it which I do not need.
I’m not sure if I’m making much sense at this point, or if what I’m asking is even doable with Generics.

  • 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-02T12:42:48+00:00Added an answer on June 2, 2026 at 12:42 pm

    I believe that you will not gain a benefit with generics here. The reason is because prop2 is a different type in each of these classes. If they were the same, then you could put the common properties into a base class and do something like this

    public class BaseObject
    {
        string prop1;
        string prop2
    }
    
    public class Object1 : BaseObject
    {
    
    }
    
    public class Object2 : BaseObject
    {
        int prop3;
    }
    
    public class ObjectService<T> where T is BaseObject
    {
        public T GetObject(T o) { return o; }
        public void SaveProperty2(T o, string i) { o.prop2 = i; }
    }
    

    You could at least make the GetObject a generic using your current example…however that entire method really seems pointless:

    public class ObjectService
    {
      public T GetObject<T>(T o) { return o; }
      public void SaveObject2Property2(Object2 o, int i) { o.prop2 = i; }
      public void SaveObject1Property2(Object1 o, string s) { o.prop2 = s; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The scenario Suppose I have the following two model classes: public class ProductColor {
Suppose we have these business objects with some properties in common: public class A
Suppose I have two classes class A { b bInstance; public A ( b
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
Suppose I have two classes deriving from a third abstract class: public abstract class
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
Suppose I have an IEnumerable<int> and I want these to be converted into their
suppose I have a file alpha.h: class Alpha { public: template<typename T> void foo();
Suppose I have a definition for a door: class Door { public void Lock()
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;

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.