Solved

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

Posted on 2014-10-01
2
124 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
[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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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