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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:53:55+00:00 2026-06-01T05:53:55+00:00

I have created a dictionary in Class A and would like to return the

  • 0

I have created a dictionary in Class A and would like to return the entire list (Key, Value) in class B. Are there any suggestions on how can I do this? I have tried a number of things from the internet but nothing seems to be working. I am trying to write a method in class A to retrieve the entire dictionary.

public class_A
{
  public static Dictionary<string, string> Dict_1 = new Dictionary();
  public add_dict(string name, string add)
  {
     Dict_1.Add(name, add);
  }

  public dict return_dictionary()
  {
    return (Dict_1); // I want to have a proper code to return this dictionary.
  }
}
  • 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-01T05:53:56+00:00Added an answer on June 1, 2026 at 5:53 am

    If this dictionary is a private member in your class. You can access this Dictionary by an encapuslated method.

    public class A
    {
        private Dictionary<string, int> _items { set;get;}
        public Dictionary<string, int> GetItems()
        {
            return _items 
        }
    }
    

    and in class B, Create an object and access it

    A objA = new A();
    Dictionary<string, int> thatItems=objA.GetItems();
    

    EDIT : As per the edit in the question
    You have some propblems in your code.

    You can not write a class like this public class_A. It should be public class A

    Your Dictionary declaration should be like this

    public static Dictionary<string, string> Dict_1 = new Dictionary<string, string>();
    

    Your add_dict method needs a return type. ( I am just adding void, you can have a bool or whatever you wish)

    public void add_dict(string name, string add)
    {
        Dict_1.Add(name, add);
    }
    

    Your method return type should be Dictionary<string, string>. They you are good.

    public Dictionary<string, string> return_dictionary()
    {
        return Dict_1;
    }
    

    But look into your code, Your Dict_1 property is a public property. So you another class dont even need your return_dictionary method to get the data. It can simply access the data like

    A objA = new A();
    Dictionary<string, string> stolenData = objA.Dict_1;
    

    So Encapsulation concept is broken !

    Better you should keep your member as private like this. I just changed your method names and variable names to follow some coding/naming conventions for better readability.

    public class A
    {
        private Dictionary<string, string> _dict = new Dictionary<string, string>();
        public void Add_dict(string name, string add)
        {
            _dict.Add(name, add);
        }
        public Dictionary<string, string> Return_dictionary()
        {
            return _dict; 
        }
    }
    

    This is a good reading to understand Encapsulation http://www.csharp-station.com/Tutorials/lesson19.aspx

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

Sidebar

Related Questions

I have a class like the following: public class DropDownControl<T, Key, Value> : BaseControl
I have defined a class Listener and created a dictionary of Listener objects. Each
I would like a method to return a Dictionary of Dictionary from an IQueryable
I would like to have a dictionary object contains string keys and values, which
I would like to register a class (not an instance) when it's created... but
I have a mutable HashMap and would like to use it like a default-dictionary.
I am trying to implement vectorization of a text file...I have created a dictionary
I have a Dictionary of objects I have created in smalltalk, which I am
I have a simple class in asp.net mvc that looks like this: public class
I have a class, which holds a static dictionary of all existing instances, 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.