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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:46:52+00:00 2026-05-26T00:46:52+00:00

I have two classes one is derived from CheckBoxList and the second one from

  • 0

I have two classes one is derived from CheckBoxList and the second one from DropDownList. The code inside them is exactly the same. The only difference is that I need first one at places where I need to show checkboxlist and second one to show dropdownlist. Below is my code:

using System;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;

    namespace Sample
    {
        public class MyCheckBoxList : CheckBoxList
        {
            public int A { get; set; }
            public int B { get; set; }
            protected override void OnLoad(EventArgs e)
            {
                //dummy task
                Collection<int> ints = new Collection<int>();
                //........
                this.DataSource = ints;
                this.DataBind();
            }
        }
    }

The second one

using System;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;

namespace Sample
{
    public class MyDropDownList : DropDownList
    {
        public int  A { get; set; }
        public int  B { get; set; }
        protected override void OnLoad(EventArgs e)
        {
            //dummy task
            Collection<int> ints = new Collection<int>();
            //........
            this.DataSource = ints;
            this.DataBind();
        }
    }
}

Now as you can see the internal code is exactly the same which I want to avoid. How can I make a common class for it so as to remove code duplicacy?

  • 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-26T00:46:53+00:00Added an answer on May 26, 2026 at 12:46 am

    You can create a third class

    public class Entity
    {
        public int  A { get; set; }
        public int  B { get; set; }
        Collection<int> GetCollection()
        {
            //dummy task
            Collection<int> ints = new Collection<int>();
            //........
            return ints;
        }
    }
    

    And then use it in other classes

    public class MyDropDownList : DropDownList
    {
        public MyDropDownList() { Entity = new Entity(); }
    
        public Entity {get;set;}
        protected override void OnLoad(EventArgs e)
        {
            this.DataSource = Entity.GetCollection();
            this.DataBind();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes one of which inherits from the other. Im trying to
I have two classes, derived from a common class. The common class has a
I have two objects that are derived from same the base class. Lets say
I have two classes, and want to include a static instance of one class
I have got two classes, one a subclass of the other (say Animal and
Let's say I have two models, Classes and People. A Class might have one
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
I have two classes, a base class and a derived class. My base class
I have a set of classes which are all derived from a common base
I have two classes. One is a management class, which stores a bunch of

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.