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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:14:18+00:00 2026-06-03T17:14:18+00:00

Two classes in the same assembly: Class A and Static Class B In a

  • 0

Two classes in the same assembly: Class A and Static Class B

In a method of ClassA, I am calling a method in ClassB,… but I want that to be called only one time and not every single time that I am caling that method of ClassA …
currently I am setting a global property -Ran – to see if that method has been ran before or not…well it works but I feel that this is not the best design. I was wondering if there are better ways to do the same?

Thanks.

ClassA.MyMethod(...)
{
....
//.... 
if (ClassB.Ran != 1)
    ClassB.Foo();
....
//...
}
  • 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-03T17:14:19+00:00Added an answer on June 3, 2026 at 5:14 pm

    You need to be careful on how strictly the “only one time” restriction is to be interpreted. Static methods are generally assumed to be thread-safe; if you really want to ensure that your method is only run once, even in the case of racing threads, then you need to use a synchronization mechanism, the simplest example being a lock:

    private static bool isRun = false;
    private static readonly object syncLock = new object();
    
    public void MyMethod()
    {
        lock (syncLock)
        {
            if (!isRun)
            {
                Foo();            
                isRun = true;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes that both contain a static variable, XmlTag. The second
I have two classes that refer to each other, but obviously the compiler complains.
Why is there an error in my code? I'm calling two classes in same
There are two base classes have same function name. I want to inherit both
I have a virtual base class and two classes that implement the various methods.
My class should extend two classes at the same time: public class Preferences extends
Summary: I want to save two classes of the same name and different namespaces
How can I access two classes with the same name in different packages? foo.bar.myClass.class
The same problem as NHibernate DuplicateMappingException when two classes have the same name but
If I have those two classes that have two different properties but with the

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.