?
Solved

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

Posted on 2005-03-19
6
Medium Priority
?
215 Views
Last Modified: 2010-04-16
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
Comment
Question by:Dynotoe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:heintalus
ID: 13584924
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
 
LVL 10

Assisted Solution

by:heintalus
heintalus earned 400 total points
ID: 13584927
That should be

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

Author Comment

by:Dynotoe
ID: 13588548
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.

 
LVL 3

Accepted Solution

by:
brckoslav earned 1600 total points
ID: 13589098
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
 
LVL 10

Expert Comment

by:heintalus
ID: 13594882
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
 

Author Comment

by:Dynotoe
ID: 13633026

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question