Solved

C# accessing correct instance of an object

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

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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 42

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
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 42

Accepted Solution

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

6 Experts available now in Live!

Get 1:1 Help Now