Solved

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

Posted on 2014-10-01
2
122 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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