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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:48:42+00:00 2026-05-13T20:48:42+00:00

Say I have two separate classes, A and B. I also have Repository class

  • 0

Say I have two separate classes, A and B.

I also have Repository class C which loads some information from a textfile. E.g. It has methods loadLines(), addLine(), deleteLine().

Ignoring databinding, how can I make A and B both work on the same class C. Is it possible?

For example at the moment, in class A and B formload, I have:

var classC = new C();

This causes repeated execution. It would be much better if I could have one copy of class c to work on from either A or B.

edit: so with using the singleton, when does class C’s constructor execute? – whichever class first creates it? and it only happens the once I take it?

edit1: does using the singleton pattern imply that you should only have one of them in your project solution? could I have multiple?

  • 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-13T20:48:43+00:00Added an answer on May 13, 2026 at 8:48 pm

    You can use the Singleton pattern or if you want to go the route of a Dependency Injection framework (which is about much more than just creating singletons but well worth looking into) most DI containers have the ability to create only a single instance of an object.

    class C {
    
        public static readonly C Instance = new C();
    
        private C() {
        }
    
    }
    

    Then you would use it like:

    private void A_Load(object sender, EventArgs e) {
        var classC = C.Instance;
    }
    
    private void B_Load(object sender, EventArgs e) {
        var classC = C.Instance;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view table which is a union of two separate tables (say
Let's say you have two classes, Person and Address. Person has a reference to
Say If i have two classes, in each class is a different JFrame, e.g
We have two separate web-apps, say 'retailUI' and 'bulkUI'. These two are basically two
Let's say I have two SQL Server databases running on separate machines, call them
Say I have two classes and have a requirement that the primary key property
Let’s say you wish to have two classes like so, each declaring an instance
Lets say I have two separate databases, X and Y, on the same physical
Here's the situation. Let's say we have two separate object types, and we want
Say you have an abstract base class Task which is a task a user

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.