Solved

How to check for existance of an object without having to create a new object each time

Posted on 2014-10-01
2
123 Views
Last Modified: 2014-10-02
Hello,
I'm new to C# and I am a little confused about how to correctly check for an existing instance of an object before deciding to create a new object.  In the code below I want to check if the object already exists (SavedTransaction !=null) and if it does exist then I want to set "MyTrans" to "SavedTransaction". However this code results in the return value "MyTrans" not existing in the current context.
       
public Transaction GetTransaction(string selectedCompany, string selectedTransId)
{

    List<TransactionSeq> TransRecord = new List<TransactionSeq>();  

    // Check if an instance already exists 
    if (SavedTransaction == null)
    {
        Transaction MyTrans = new Transaction();
        SavedTransaction = MyTrans;
    }
    else

    {Transaction MyTrans = SavedTransaction; }

    // If company or transaction is null then Load "LOGIN"
    if (selectedCompany == null || selectedTransId == null)
    {
        selectedCompany = "99999";
        selectedTransId = "LOGIN";
    }

    LoadUserDtls(selectedCompany,  selectedTransId);

    return MyTrans;
}

Open in new window


If I pull the object creation up  as follows then this resolves the issue of scope
       
public Transaction GetTransaction(string selectedCompany, string selectedTransId)
{
    Transaction MyTrans = new Transaction();
    List<TransactionSeq> TransRecord = new List<TransactionSeq>();  

    // Check if an instance already exists 
    if (SavedTransaction == null)
    {
         MyTrans = new Transaction();
        SavedTransaction = MyTrans;
    }
    else

    {MyTrans = SavedTransaction; }

    // If company or transaction is null then Load "LOGIN"
    if (selectedCompany == null || selectedTransId == null)
    {
        selectedCompany = "99999";
        selectedTransId = "LOGIN";
    }

    LoadUserDtls(selectedCompany,  selectedTransId);

    return MyTrans;
}

Open in new window


However this means that I always create a new object even when the object already exists and I want to use the existing instance. Is there any way around this ?

regards
Pat
0
Comment
Question by:pclarke7
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40356329
public Transaction GetTransaction(string selectedCompany, string selectedTransId)
        {
            Transaction MyTrans;  //just declare the object and don't create a new instance here               = new Transaction();
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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