Solved

C# accessing correct instance of an object

Posted on 2014-10-19
5
100 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

Technology Partners: 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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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