Link to home
Start Free TrialLog in
Avatar of pclarke7
pclarke7

asked on

C# accessing correct instance of an object

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
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of pclarke7
pclarke7

ASKER

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
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 :)
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
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial