Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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