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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:51:10+00:00 2026-05-23T01:51:10+00:00

I’m trying to learn how from this site http://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspx but the code won’t compile

  • 0

I’m trying to learn how from this site http://msdn.microsoft.com/en-us/library/cc265158(v=vs.95).aspx but the code won’t compile and i get tons of errors.

Here’s my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bail
{
    public class ListboxMenuItem
    {
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Address { get; set; }

        public ListboxMenuItem(String firstName, String lastName, String address)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Address = address;
        }
    }

    class ListboxMenuItems
    {
        List<ListboxMenuItem> Items;

        Items = new List<ListboxMenuItem>();
        Items.Add(new ListboxMenuItem("Michael", "Anderberg", "12 North Third Street, Apartment 45"));
        Items.Add(new ListboxMenuItem("Chris", "Ashton", "34 West Fifth Street, Apartment 67"));
        Items.Add(new ListboxMenuItem("Cassie", "Hicks", "56 East Seventh Street, Apartment 89"));
        Items.Add(new ListboxMenuItem("Guido", "Pica", "78 South Ninth Street, Apartment 10"));

    }
}

All errors are with Items

For example Items = new List<ListboxMenuItem>(); produces the error

Error 1 Invalid token ‘=’ in class, struct, or interface member declaration ListboxMenuItems.cs 26 15 Bail

  • 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-23T01:51:10+00:00Added an answer on May 23, 2026 at 1:51 am

    At first I only corrected the error, however after seeing the link I noticed the old answer doesn’t really help.
    You can still find it on the bottom though.

    The New Answer

    I have just looked at the link, and it seems like you’re doing the wrong thing anyway.
    You need your class to inherit from ObservableCollection<T>, and instead of a List field/property you should be using the functionality of the base class (which already has Add method):

    class ListboxMenuItems : ObservableCollection<ListboxMenuItem>
    {
        public ListboxMenuItems ()
        {        
            // 'Add' here means 'base.Add'
            Add (new ListboxMenuItem ("Michael", "Anderberg", "12 North Third Street, Apartment 45"));
            Add (new ListboxMenuItem ("Chris", "Ashton", "34 West Fifth Street, Apartment 67"));
            Add (new ListboxMenuItem ("Cassie", "Hicks", "56 East Seventh Street, Apartment 89"));
            Add (new ListboxMenuItem ("Guido", "Pica", "78 South Ninth Street, Apartment 10"));
        }
    }
    

    All of this is clearly written in the reference you provided so you should be more careful with adopting code from documentation.

    The Old Answer

    You have put the initialization code right inside the class declaration, where normally methods, fields and properties are placed.

    Initialization code is placed in a special method called ‘constructor’ that has the same name as the class, doesn’t have a return type, and is placed inside of the corresponding class:

    class ListboxMenuItems
    {
        public List<ListboxMenuItem> Items { get; private set; }
    
        public ListboxMenuItems ()
        {        
            Items = new List<ListboxMenuItem> {
                new ListboxMenuItem ("Michael", "Anderberg", "12 North Third Street, Apartment 45"),
                new ListboxMenuItem ("Chris", "Ashton", "34 West Fifth Street, Apartment 67"),
                new ListboxMenuItem ("Cassie", "Hicks", "56 East Seventh Street, Apartment 89"),
                new ListboxMenuItem ("Guido", "Pica", "78 South Ninth Street, Apartment 10")
            };
        }
    }
    

    I changed Items from being a field to a property. This is a better practice because you can specify who can change it (in our case, private set allows to set it only from within ListboxMenuItems).

    I also used list initializer syntax that allows you to drop many Add calls in favor of a cleaner, clutterless syntax.

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

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.