Solved

C# accessing correct instance of an object

Posted on 2014-10-19
5
97 Views
Last Modified: 2014-10-20
Hello,
I am new to C#. I have an application which calls a DataBaseIO class method  to read through an SQL database table. This method creates a new instance of a User object and populates the instance with data read from the input table. In debug I can see that the instance is updated with the correct data.

However when control returns from this DataBaseIO method to the calling method ,the object is null. The calling method is in source Transaction.cs and the object is created in DataBaseIO.cs. How can I pass the object back to the calling method in Transaction.cs ?

regards
Pat
0
Comment
Question by:pclarke7
  • 3
  • 2
5 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40390864
Without seeing specific code, it's difficult to know.

First thoughts are that your User object is created with a local scope inside your DataBaseIO method, so only exists in there. Once that method is complete, it's disposed of. You would need to return that User object to the calling method:

For example, in Transaction.cs:

User myUser = DataBaseIO.GetUser();

Open in new window


and in DatabaseIO:

public User GetUser()
{
    User newUser = new User();
    // populate the newUser object by accessing your database

    //now return the newUser object to the calling method:
    return newUser;
}

Open in new window

0
 

Author Comment

by:pclarke7
ID: 40392703
Hi Chris,
thanks for you input.

In my Transaction.cs I have the following code:

MyDataBaseIO.NavigateRecordsFlex(connStringKey, connString, sqlStmt, tableName);
MyUser=MyDataBaseIO.ReturnUser;


and in DataBaseIO.cs I have the following code:
       
public void NavigateRecordsFlex(string connStringKey, string connString, string sqlStmt, string tableName)
 {
   // Read various  tables

   // If tableName="User_Master" then create instance of MyUser
   // if tableName="Tran_Master" then create instance of MyTrans
       ..
       ..
 }

     
public UserMaster ReturnUser()
        {
            return MyUser;
        }


The problem I have is that NavigateRecordsFlex is used to read all tables. If the table name being read is one of a selected number (ie. UserMaster, TransactionMaster etc..) then I create an instance of the class within NavigateRecordsFlex. I then need to return this instance to the calling program. Depending on the table name the object created will vary and therefore I cannot use a return statement from NavigateRecordsFlex. If I call ReturnUser() after NavigateRecordsFlex then MyUser is null.

regards
Pat
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 40392753
OK. In your NavigateRecordsFlex, you say you create an instance of MyUser if the tableName argument is User_Master, but it's not clear from your code what you're assigning that instance to. Also in ReturnUser(), you're simply returning MyUser. This implies that the MyUser variable is defined as a class level variable or property somewhere (which you've not shown), and therefore when you create the instance in your NavigateRecordsFlex, you must assign your newly created instance to the class level variable.

Also in your Transaction code, I'm guessing that you've defined the type MyUser as UserMaster. Have a look at this:

// Define MyUser as a UserMaster type
private UserMaster MyUser = null; 

public void NavigateRecordsFlex(string connStringKey, string connString, string sqlStmt, string tableName)
{
    // Read various  tables

    // If tableName="User_Master" then create instance of MyUser
    // if tableName="Tran_Master" then create instance of MyTrans
    MyUser = new UserMaster();
    // now do something with MyUser
    MyUser.SomeProperty = "Some Value";
    ..
    ..
}
     
public UserMaster ReturnUser()
{
    return MyUser;
}

Open in new window

And in Transaction.cs:

// Define MyUser as a UserMaster type
UserMaster MyUser;
MyDataBaseIO.NavigateRecordsFlex(connStringKey, connString, sqlStmt, tableName);
MyUser=MyDataBaseIO.ReturnUser;

Open in new window


This all comes down to scope, which is difficult to identify from only seeing snippets of code, but hopefully I'm making some sense :)
0
 

Author Comment

by:pclarke7
ID: 40392849
Hi Chris,
you were spot on - it was a scoping issue.

At the top of DataBaseIO.cs I has MyUser defined as
UserMaster MyUser;

Further down in another Method where I create an instance of UserMaster I had the following:
UserMaster MyUser = new UserMaster();

This was going out of scope at the end of the method. Once I changed it to:
MyUser = new UserMaster();

then it remained within scope and ReturnUser() was able to return it. Thanks so much for walking me through this.

regards
Pat
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40392869
No worries. Pleased you got it working :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

28 Experts available now in Live!

Get 1:1 Help Now