Solved

Where is my object

Posted on 2014-10-27
5
143 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
[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
  • 2
  • 2
5 Comments
 
LVL 40

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:Vel Eous
Vel Eous 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 40

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:Vel Eous
Vel Eous 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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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