Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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 discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now