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
DynotoeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.