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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:03:17+00:00 2026-06-02T04:03:17+00:00

VERY new to C#, using Visual Studio 2010. In my old PHP, to build

  • 0

VERY new to C#, using Visual Studio 2010. In my old PHP, to build this menu, what I would do is have 2 arrays of hyper-links. Level1 array would be displayed if a ‘requester’ logs in, Level1 AND Level2 array would be merged and sorted and displayed if an ‘administrator’ logs in.
In C#, on my default page (which has a Site.Master page), I find myself doing this:

case NewCourseRequestView.Administrator:
    if (access.Administrator)
    {
        UserTypeLabel.Text = "Administrator Details:";

        AdministratorMenuList.Items.Add("View Un-Approved Requests");
        adminContent.Visible = true;
    }
    break;

case NewCourseRequestView.Requestor:
    if (access.Requestor)
    {
        UserTypeLabel.Text = "Requester Details:";

        RequestorMenuList.Items.Add("Request A New Course");
        RequestorMenuList.Items.Add("View My New Course Requests");
        requestContent.Visible = true;
    }
    break;

How can I

  1. have these list items act as hyper-links AND
  2. have these 2 lists in 2 separate arrays (collections?) and merge them if I have access.Administrator?

I need a fairly specific explanation, 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-02T04:03:18+00:00Added an answer on June 2, 2026 at 4:03 am

    I suggest you to start from the design first, no matter what language you’re using.
    Imagine you will need to add one more User Type (for example, “Guest”) which has different
    set of menu items.
    What needs to be done then? Adding one more “if” statement and, basically copy-pasting.
    This is not good at all. And now imagine what happens when you have 10 user types.
    Your switch-case statement will become giant monster and everyone in your team (even you) will be afraid to add a new Item there or change an existing one.

    Ok, that was sad, let’s have some fun now 🙂

    Ideally, your “client” code, which I suppose you’re going to place into the Page_Load method will have only one line:

    protected void Page_Load(object sender, EventArgs e){
        BuildMenu();
    }
    

    How cool it is to have only one line in your method and have everything “magically” sorted out.

    Let’s say that we have some kind of a policy which regulates available items for our user types.
    The basic interface is pretty simple:

    public abstract class MenuItemsPolicy{
    
        public abstract List<MenuItem> GetMenuItems();
    
        protected virtual MenuItem CreateMenuItem(string text, string url){
            //add parameter checks, etc.
            MenuItem item = new MenuItem();
            item.Text = text;
            item.NavigateUrl = url;
            return item;
        }
    }
    

    At the moment, we have two user types, so we will have exactly two implementations.
    The Requestor:

    public class RequestorMenuItems : MenuItemsPolicy{
        public override List<MenuItem> GetMenuItems(){
            return new List<MenuItem>(){
                CreateMenuItem("Request A New Course", "~/Courses/RequestNewCourse.aspx"),
                CreateMenuItem("View My New Course Requests", "~/Courses/ViewMyCourseRquests.aspx")
            };
        }
    }
    

    And an Administrator (pay attention where the “merge” is)

    public class AdministratorMenuItems : MenuItemsPolicy{
        public override List<MenuItem> GetMenuItems(){
            List<MenuItem> resultingMenuItems = (new RequestorMenuItems()).GetMenuItems();
            resultingMenuItems.Add(CreateMenuItem("View Un-Approved Requests", "~/Administration/ViewUnAprroved.aspx"));
            return resultingMenuItems;
        }
    }
    

    As you can see, we retrieve Requestor’s items and then adding one more item there.
    If this needs to be changed in future, your client/calling code will know nothing about
    the implementation details because you depend on abstract entity. That means you’ll need no changes in your client/calling code the rules will be changed; or if available links for a particular user type will need to be changed in future.

    To simplify the example, I have used enum for User Types:

    public enum UserAccessType{
        Requestor = 0,
        Administrator = 1
    }
    

    And now, let’s have a look on the implementation.
    I have placed a simple and standard asp.net menu control onto the page:

    <form id="form1" runat="server">
        <asp:menu runat="server" Id="mainMenu"></asp:menu>
    <div>
    

    And Here’s the “codebehind”:

    public partial class _Default : System.Web.UI.Page{
    
        //We will initialize this variable a bit later 
        //for example from URL parameter ?userType=1 or something like that
        private UserAccessType _currentUserAccess;
    
        //Ideally, dictionary should be a member of a class
        //I left a simple dictionary just to avoid over-complicaation
        private readonly Dictionary<UserAccessType, MenuItemsPolicy> _userMenuItems = new Dictionary<UserAccessType, MenuItemsPolicy>();
    
        protected override void OnInit(EventArgs e){
            //Associating our user types with Menu Items
            _userMenuItems.Add(UserAccessType.Administrator, new AdministratorMenuItems());
            _userMenuItems.Add(UserAccessType.Requestor, new RequestorMenuItems());
    
            //Init the Current User / Access Type. For example, you can read the value from Request["UserType"]
            //Here I've just assigned a value
            _currentUserAccess = UserAccessType.Administrator;
    
            base.OnInit(e);
        }
    
        protected void Page_Load(object sender, EventArgs e){
            BuildMenu();
        }
    
        private void BuildMenu(){
            mainMenu.Items.Clear();
            var menuItems = GetMenuItems();
            foreach(var item in menuItems){
                mainMenu.Items.Add(item);
            }
        }
    
        private List<MenuItem> GetMenuItems(){
            MenuItemsPolicy currentPolicy = _userMenuItems[_currentUserAccess];
            return currentPolicy.GetMenuItems();
        }
    }
    

    I hope, the example above answers your questions.
    The example, of course, is not ideal. However, it helps to keep your code relatively clean and maintainable.

    P.S. I do recommend you to go through the article:
    http://objectmentor.com/resources/articles/Principles_and_Patterns.pdf

    refer to the OCP example – you’ll see, how close it is to your situation.

    Thanks for reading 🙂

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

Sidebar

Related Questions

I'm using Visual Studio 2010 with the new website publish dialog. I have a
I have created an OData/WCF service using Visual Studio 2010 on Windows XP SP3
I'm using Visual C# Studio 2010 express and have been trying to batch update
I am using Visual Studio 2010 Professional Edition, and Windows Vista. Firstly, I have
I have a web application solution existing under Visual Studio 2010. Part of this
I'm using C# with XNA in Visual Studio 2010 Ultimate. I have a program
I'm very new to VS2010, so this is more a question about using Visual
I have several projects in a solution in visual studio 2010. This is the
Guys i am very new to jQuery. I have started using the auto complete
I have built a very simple blog application using Ruby on Rails. New to

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.