Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

C# --- Checking the existance of an object that may or may not exist at a certain point in the runtime application

Hi everyone,

I have a question I neeed help with...


On my form1 I have a function/method that creates an instance of something (not sure whether to call it an object or not).  It is basically an API connection to a dll so that I can call methods exposed by another software application via the API dll.

the line looks like this...

     public void CreateAPI()
     {
     TL = new API2Class();
     }

there is also a using statement so I can do this...
using TL;  etc...

Anyway I was wondering how could I check from other forms or classes if this TL exists.  For example at different stages in my program I want to check if an instance of TL was created via the line TL = new APIClass(); and if it doesn't exist call the function Form1.CreateAPI()that will then create it if it doesn't exist already?

How do you check if something exists in code if it doesn't?  lol  :)   Any advice?

Please allow me to express my thanks in advance.

Regards,

Dynotoe
0
Dynotoe
Asked:
Dynotoe
  • 3
  • 2
2 Solutions
 
heintalusCommented:
Why not change your CreateAPI method to this

public void CreateAPI()
     {
     if (TL = null)
          TL = new API2Class();
     }

That way you can just call this & it will be create if it doesn't already exist

HTH
Andy
0
 
heintalusCommented:
That should be

public void CreateAPI()
     {
     if (TL == null)
          TL = new API2Class();
     }
0
 
DynotoeAuthor Commented:
Hi helintalus,

I see and totally understand you answer.....but remember I want to check the state of it from other methods, forms/classes etc.  I don't think I'll be able to call this method from everywhere.

Any ideas?

Thanks for your help.

Best,

Dynotoe
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
brckoslavCommented:
if form1 is main form => one instance of it is present at same time, then
define TL as public static and then you can check it with

if (Form1.TL != null) {
do something
}
0
 
heintalusCommented:
Hi Dynotoe

As per brckoslav's suggestion, just declare both you variable & your method as     public static    then you'll be able to use them globally just by using the Forms name rather than an instance of it.

HTH
Andy
0
 
DynotoeAuthor Commented:

Hi guys,


Sorry for the delay.....got a bit sick.

Thanks again for all your generous help and best of luck with everything.

Best,

Dynotoe
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now