Solved

C# accessing correct instance of an object

Posted on 2014-10-19
5
101 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
[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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 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