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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:54:14+00:00 2026-05-26T07:54:14+00:00

I was reading a post at VS 2008, ASP.NET: Generate Local Resources . Mehdi

  • 0

I was reading a post at VS 2008, ASP.NET: Generate Local Resources.
Mehdi Golchin showed us a beautiful job of StateManagedCollection.
However I was wondered about using multiple classes of IStateManager in one StateManagedCollection.
As you can see below:
public class MenuItemCollection : StateManagedCollection
{

public MenuItem this[int index] 
{ 
    get { return (MenuItem)((IList)this)[index]; } 
} 

public int Add(MenuItem item) 
{ 
    return ((IList)this).Add(item); 
} 

public void Remove(MenuItem item) 
{ 
    ((IList)this).Remove(item); 
} 

// Write Insert and RemoveAt methods 

protected override void SetDirtyObject(object o) 
{ 
    ((MenuItem)o).SetDirty(); 
} 

}
This MenuItemCollection class can have only one child class(“MenuItem”).
If I want to use another class as well as MenuItem class, for example MenuItem2 class, how do I have to write the codes?
Anyone can help me?
Thanks in advance.

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

    Write a generic version – for example,

    public class GenericStateManagedCollection<T> : StateManagedCollection 
       where T: IStateManager, new()
    {
    
        public T this[int index]
        {
            get { return (T)((IList)this)[index]; }
        }
    
        public int Add(T item)
        {
            return ((IList)this).Add(item);
        }
    
        public void Remove(T item)
        {
            ((IList)this).Remove(item);
        }
    
        // Write Insert and RemoveAt methods
    
        protected override void SetDirtyObject(object o)
        {
            ((T)o).SetDirty();
        }
    
        protected override object CreateKnownType(int index)
        {
            return Activator.CreateInstance<T>();
        }
    
        protected override Type[] GetKnownTypes()
        {
            return new Type[] { typeof(T) };
        }
    
    }
    

    And use it as

    public class MenuItemCollection : GenericStateManagedCollection<MenuItem> { }
    public class XyzItemCollection : GenericStateManagedCollection<XyzItem> { }
    

    EDIT:

    I have most probably mis-understood your question! Assuming now that you want to put two different type of objects into the StateManagedCollection. From usage perspective, it doesn’t make sense to have objects of completely unrelated types into the collection – you need to have some base class. For example, consider DataControlFieldCollection which holds instances of (abstract) type ‘DataControField. BoundField, ButtonField etc inherits fromDataControField`.

    So you need to go via similar route – for example,

    public class MenuItemBase : IStateManager
    {
       // Use implementation from link you quoted (Mehdi Golchin's answer)
       ...
    }
    
    public class MenuItem : MenuItemBase
    {
      ...
    }
    
    public class MenuItem2 : MenuItemBase
    {
      ...
    }
    
    public class MenuItemCollection : StateManagedCollection
    {
    
        public MenuItemBase this[int index]
        {
            get { return (MenuItemBase)((IList)this)[index]; }
        }
    
        public int Add(MenuItemBaseitem)
        {
            return ((IList)this).Add(item);
        }
    
        public void Remove(MenuItemBaseitem)
        {
            ((IList)this).Remove(item);
        }
    
        // Write Insert and RemoveAt methods
    
        protected override void SetDirtyObject(object o)
        {
            ((MenuItemBase)o).SetDirty();
        }
    
        // important to override CreateKnownType and GetKnownTypes
        private static readonly Type[] _knownTypes = new Type[] {typeof(MenuItem), typeof(MenuItem2) }
    
        protected override Type[] GetKnownTypes()
        {
           return _knownTypes;
        }
    
        protected override object CreateKnownType(int index)
        {
           switch (index)
           {
              case 0:
                 return new MenuItem();
    
              case 1:
                  return new MenuItem2();
    
              default:
                  throw new Exception("Invalid Index");
           }
         }
    }
    

    Note: Untested code

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

Sidebar

Related Questions

I am reading a post about mobile web development and ASP.NET MVC here: http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx
After reading Steve Sandersons post on swf upload. http://blog.stevensanderson.com/2008/11/24/jquery-ajax-uploader-plugin-with-progress-bar/ I have implemented the swf
I generate all my columns in a subclassed DataGridView programmatically. However Visual Studio 2008
Reading this post has left me wondering; are nightly builds ever better for a
After reading this post I kinda felt in the same position as the guy
When reading a post some points were given without example : To implement IEnumerable
I remember reading a post about a year or so ago by Scott Hanselman
I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
I'm reading a post about iPhone programming and I've noticed that the talk about
Well I was reading this post and then I came across a code which

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.