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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:45:32+00:00 2026-05-24T23:45:32+00:00

I am trying to implement a string indexer for a C# class, but when

  • 0

I am trying to implement a string indexer for a C# class, but when you set a property the dictionary gets set and not the property. It is probably something simple that i am missing, i just can’t see it.

objFiveProp temp = new objFiveProp();
temp["index1"] = 3;

sets the temp._items[“index1”].value to 3.

Class:

public class objFiveProp
{

    #region Properties
    private Dictionary<string, int> _items;
    public int this[string key]
    {
        get { return _items[key]; }
        set { _items[key] = value; }
    }

    public int index1 { get; set; }
    public int index2 { get; set; }
    public int index3 { get; set; }
    public int index4 { get; set; }
    public int index5 { get; set; }

    #endregion
    #region Constructor

    public objFiveProp()
    {
        index1 = 0;
        index2 = 0;
        index3 = 0;
        index4 = 0;
        index5 = 0;
        _items = new Dictionary<string, int>();
        _items.Add("index1", index1);
        _items.Add("index2", index2);
        _items.Add("index3", index3);
        _items.Add("index4", index4);
        _items.Add("index5", index5);

    }

    #endregion

}
  • 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-24T23:45:33+00:00Added an answer on May 24, 2026 at 11:45 pm

    That’s how it works. The Dictionary contains a copy of the integers you use to set it up – not a reference to the properties.

    I would tackle this by using something like:

    public class objFiveProp
    {
        private Dictionary<string, int> _items;
        public int this[string key]
        {
            get { return _items[key]; }
            set { _items[key] = value; }
        }
    
        public int Index1 
        {
            get { return this["index1"]; } 
            set { this["index1"] = value; }
        }
        public int Index2
        {
            get { return this["index2"]; } 
            set { this["index2"] = value; }
        }
    
        // ....
    
        public objFiveProp()
        {
            _items = new Dictionary<string, int>();
            _items.Add("index1", index1);
            _items.Add("index2", index2);
            _items.Add("index3", index3);
            _items.Add("index4", index4);
            _items.Add("index5", index5);    
        }
    
    #endregion
    

    This causes your properties to always pull the values stored in your dictionary, as well as save there, so there aren’t two copies of the values.

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

Sidebar

Related Questions

I am trying to implement a new class inherited from List<string> , to load
I'm trying to implement this extenstion of the Karplus-Strong plucked string algorithm, but I
I'm trying to implement a simple search function. I have a string array which
I have been trying to implement an efficient string comparing algorithm that will given
I am trying to implement a simple string copy in the following code. However,
I am trying to implement an open source string-to-link converter, but i have an
I am trying to implement string unescaping with Python regex and backreferences, and it
All I am currently trying implement something along the lines of dim l_stuff as
I am trying to implement a PKI verification scheme, where a message string is
I am trying to implement postfix-expression evaluation, here is my code: #include<iostream> #include<string.h> using

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.