Solved

Where is my object

Posted on 2014-10-27
5
134 Views
Last Modified: 2014-10-29
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.
0
Comment
Question by:pclarke7
  • 2
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 40407355
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
 
LVL 14

Assisted Solution

by:Tchuki
Tchuki earned 250 total points
ID: 40407482
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
 

Author Comment

by:pclarke7
ID: 40411888
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
 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 40412013
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
 
LVL 14

Assisted Solution

by:Tchuki
Tchuki earned 250 total points
ID: 40412058
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

18 Experts available now in Live!

Get 1:1 Help Now