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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:26:36+00:00 2026-05-29T16:26:36+00:00

I am writing a custom dictionary class for my task but it is giving

  • 0

I am writing a custom dictionary class for my task but it is giving error. What is the problem at this class ? Thank you

        public struct MyValue
    {
        public int irValue1;
        public int irValue2;
    }

    public class csCustomDictionary : Dictionary<string, MyValue>
    {
        public void Add(string srKey, int irVal1, int irVal2)
        {
            if (this.ContainsKey(srKey) == true)
            {
                this[srKey].irValue1 = this[srKey].irValue1 + irVal1;
                this[srKey].irValue1 = this[srKey].irValue2 + irVal2;
            }
            else
            {
                MyValue val;
                val.irValue1 = irVal1;
                val.irValue2 = irVal2;
                this.Add(srKey, val);
            }
        }
    }
}

this is the error message

enter image description here

C# 4.0

Is this modified version correct

    public class csMyValue
{
    public int irValue1;
    public int irValue2;
}

public class csCustomDictionary : Dictionary<string, csMyValue>
{
    public void Add(string srKey, int irVal1, int irVal2)
    {
        if (this.ContainsKey(srKey) == true)
        {
            this[srKey].irValue1 = this[srKey].irValue1 + irVal1;
            this[srKey].irValue1 = this[srKey].irValue2 + irVal2;
        }
        else
        {
            csMyValue val = new csMyValue();
            val.irValue1 = irVal1;
            val.irValue2 = irVal2;
            this.Add(srKey, val);
        }
    }
}
  • 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-29T16:26:38+00:00Added an answer on May 29, 2026 at 4:26 pm

    The problem is that you’re using a struct as your Value in the Dictionary.

    When you write: this[srKey], you actually retrieve a copy of the struct stored as the Dictionary’s value. As such, the .irValue1 is trying to set a field on the copy (that will soon disappear).

    If you change MyValue to be a class, this should work. This is more appropriate, as well, as you’re making a mutable type, and mutable structs are very rarely a good idea.

    That being said, you could force this to work via making a temporary, setting the fields, then setting the value in your Dictionary:

    if (this.ContainsKey(srKey) == true)
    {
        MyValue tmp = this[srKey];
        tmp.irValue1 += irVal1;
        tmp.irValue2 += irVal2;
        this[srKey] = tmp;
    }
    

    If you change MyValue to be a class instead of a struct, you could write:

        MyValue tmp = this[srKey];
        tmp.irValue1 += irVal1;
        tmp.irValue2 += irVal2;
    

    Since the stored value would be a reference, this will do the change that you wish without requiring lots of dictionary lookups.

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

Sidebar

Related Questions

I'm writing a custom json deserializer and I'm writing this: public class MyObjectToJson :
I'm writing a custom swing component (something completely new, but think JTree or JList).
As part of writing custom command (COM-Visible dll with class that implements Interwoven command
I am constantly writing custom WebParts but after I deploy them, they always get
I need help writing custom functions for my casio fx-9860g I have done this
I know I can do this by writing custom type descriptors etc., however given
I have an object like this: class MyObject { public string Object.Prop1 { get;
I created custom django-admin command. I use this tutorial Writing custom django-admin commands For
Overview of the problem: I've been playing with writing custom http server apps for
When it comes to writing custom MySQL database-driven PHP session management for a VERY

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.