Where is my object

Hello,
I am new to c#. In my c# WCF application I have two endpoints Login() and GetTransaction(). In my TransactionService.cs I call a method in DataBaseIO.cs to create a user object and then call  ReturnUser() in DataBaseIO.cs    to return the object to TransactionService. This work fine.    

 // Return User object to calling program
        public UserMaster ReturnUser()
        {
            return MyUser;
        }

Next I want to create a connection string . Again In my TransactionService.cs I call a method in DataBaseIO.cs to create a connection string object and then call  ReturnConnString() in DataBaseIO.cs    to return the object to TransactionService. This also work fine.  
       
// Return connection strings object to calling program
        public ConnectionStrings ReturnConnString()
        {
            return MyConnString;
        }
 
Both objects are created in DataBaseIO but when I create my second object I note that the first object is null within DataBaseIO. Why is it not still there when I call DataBaseIO the 2nd time ?

I need to access the user object data when creating the connection string. Do I need to pass the objects I need back into DataBaseIO each time I make a call to one of its methods ?

regards
Pat

PS
I also noticed that in TransactionServices, if I call the GetTransaction() from Login() then even though within Login() my user object is populated with data when I call GetTransaction() method the object is null.
LVL 1
pclarke7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
It depends on how you're accessing the classes.

If they're in different functions, or if you're calling the new each time, then you're going to have a blank slate.

eg:
private void t1()
{
DataBaseIO io = new DataBaseIO();
io.GetUser();

io = new DataBaseIO() // user will now be null.

}

OR

private void t1()
{
DataBaseIO io = new DataBaseIO();
io.GetUser();

//io is on longer accessible when you exit out of this function.
}

To get around that you can create a static or global variable.

eg:
DataBaseIO io = new DataBaseIO();

private void t1()
{
  io.Getuser();
//io is not destroyed since it exists outside of the function
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vel EousResearch & Development ManagerCommented:
Strictly speaking your
Login()
and
GetTransaction()
methods are just that, methods.  Your
TransactionService
is more in keeping with a WCF Endpoint in that it has an Address, Binding and a Contract.

Terminology aside the behaviour you are describing could be caused by the instance management you have configured your Endpoint to have.  There are three modes:

PerCall
PerSession
Single

These work as you might expect from their names.  PerCall creates a new service instance on the server for each call from the client. PerSession maintains the service instance created per client whilst the connection is active, destroying it when the connection is closed.  Single uses the same service instance for all client connections.

Assuming your objects are not going out of scope, and you're not unintentionally null-ing them somewhere in your code, it could be that you have configured your service Endpoint to use the PerCall instance mode which would exhibit the behaviour you have described.
0
pclarke7Author Commented:
Hi Tchuki & Kyle,
thanks for your comments which have helped my understanding of scoping. I have also change the endpoints to be "PerSession" which is very useful to know.  Originally ,I had hoped to be able to make each of my objects available from anywhere within the application. However as these are not static objects and so I assume that the best way to make the objects available is to create the object in the appropriate place and then  pass the object as a parameter to the various methods requiring them as input and return the objects if they have been modified by the called method. Does this sound like a workable solution ?

regards
Pat

PS. Could either of you recommend any reading material to help with my understanding of creating and sharing class objects ?
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Yes that does sound like a workable solution . . . it's normally the way things are done.  

A good read for Object Oriented Programming (classes)
http://www.c-sharpcorner.com/UploadFile/mkagrahari/introduction-to-object-oriented-programming-concepts-in-C-Sharp/

And starting here (and continuing)
http://www.csharp-station.com/Tutorial/CSharp/lesson07
0
Vel EousResearch & Development ManagerCommented:
Personally I would be cautious of passing an entire object as a parameter on the off-chance that its state might be altered, ideally you don't want to incur the overhead.  It would be preferable to pass only what is required to fulfil a requirement and return (where applicable) an appropriate object.

If you provide further information, and/or an example we could advise better.

Object communication is part of the OOP fundamentals.  There are plenty of resources available, some of which are free:
Introduction to Object Oriented Programming Concepts (OOP) and More
and others not so much:
Object-Oriented Programming Fundamentals in C#

The Pluralsight stuff is pretty good and worth the monthly sub if you're not against such things.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.