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

  • Home
  • SEARCH
  • 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 6254893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:15:52+00:00 2026-05-24T14:15:52+00:00

Consider following code public class City { public string Name { get { return

  • 0

Consider following code

    public class City
    {
        public string Name { get { return "New York"; } }

        Building empEstate;
        Building nyTimes;
        public void Init()
        {
            // I hate passing "this" to all object
            empEstate = new EmpEstate(this); 
            setSomeProperty(empEstate);
            // any one can create new object of some other city 
            // and pass to the building
            nyTimes = new NYTimes(this);
            ...
            other = new OtherBuildings(this)
        }

        public void PrintAddresses()
        {
            empEstate.Print();
            nyTimes.Print();
            ...
            other.Print();
        }
    }

    public abstract class Building {
        City _city;
        public Building(City city){
            this._city = city;
        }
        public abstract string Name { get;}
        public void Print(){
            Console.WriteLine(this.Name);
            Console.Write(",");
            Console.WriteLine(this._city.Name);
        }
    }
  1. First thing I want better solution to this approach. Print is just an example. Actually each building object raise some event to City object. I don’t want to add handler to each building as there could be several buildings in city. Also I do not want to add each of them into list, as it is two task for each building (one initialization and second add to list, one forget to add to list when writing new building). For this, I want caller to be automatically available to callee, like Parent property of control (though it was added to this.Controls)

  2. Using memory, can we know who is the parent of current object. How does GC knows that object is not being referenced (including creator). Can’t we create a method (safe or unsafe) using memory to identify the caller object. I see we can use StackTrace to see the call hirarchy, can we intercept here when a new object is being created.

  • 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-24T14:15:53+00:00Added an answer on May 24, 2026 at 2:15 pm

    Building factory on city solved my problem of passing this to each object

    public interface ICity
        {
            string Name { get; }
        }
        public abstract class City : ICity
        {
            public T CreateBuilding<T>()
            {
                T buildingInstance = Activator.CreateInstance<T>();
                ((IBuilding)buildingInstance).SetCity(this);
                return buildingInstance;
            }
    
            public abstract string Name { get; }
        }
    
        interface IBuilding
        {
            ICity City { get; }
            void SetCity(ICity city);
        }
        public abstract class Building : IBuilding
        {
            private ICity _city;
            public ICity City { get { return _city; } }
            public void IBuilding.SetCity(ICity city)
            {
                this._city = city;
            }
            public abstract string Name { get; }
            public void Print()
            {
                Console.WriteLine(this.Name);
                Console.Write(",");
                Console.WriteLine(this._city.Name);
            }
        }
        public class EmpEstate : Building
        {
            public override string Name { get { return "Emp State"; } }
        }
        public class NYTimes : Building
        {
            public override string Name { get { return "NY Times"; } }
        }
        public class NewYorkCity : City
        {
            public override string Name { get { return "New York"; } }
    
            EmpEstate empEstate;
            NYTimes nyTimes;
            public void Init()
            {
                // Now I dont need to pass this
                empEstate = this.CreateBuilding<EmpEstate>();
                 setSomeProperty(empEstate);
                // now any one cannot create building in new your and 
                // say it belongs to Philedelphia :)
                nyTimes = this.CreateBuilding<NYTimes>();
            }
    
            public void PrintAddresses()
            {
                empEstate.Print();
                nyTimes.Print();
            }
        }
    

    Problem was there were several classes already created and for new functionality we needed the creator object in the base class of Building Object. We did not wanted to modify the constructor of each class and pass this object to each. And City class (in example) was basically code on plugin side, so allowing them to pass city (if plugin developer pass wrong city) may disturb the functionality of entire app. So modifying the plugin base solved my purpose. Suggestions are welcome.

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

Sidebar

Related Questions

Please consider the following code: public class Person ( public string FirstName {get; set;}
Consider the following code snippet below. class X { public String toString() { return
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code: public class TextType { public TextType(String text) { underlyingString =
Consider the following code: public class MyClass { public delegate string PrintHelloType(string greeting); public
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
Consider the following code : public class RandomClass { private readonly string randomString; public
Consider the following code: public class ReadingTest { public void readAndPrint(String usingEncoding) throws Exception
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider the following code : class TextMessage{ public : TextMessage(){}; TextMessage(std::string _text):text(_text){} std::string text;

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.