Solved

How do I return a reference to a newly instantiated class/object from a singleton object?

Posted on 2014-11-14
6
229 Views
Last Modified: 2014-11-14
I have a singleton class that is managing the CRUD of my customer data.  The singleton exposes a GetCustomer which is to return the memory location of the retrieved customer.  What I am getting back from GetCustomer is an entire customer object not just a reference to the newly created memory location inside the singleton.

How do I get a handle/reference on the memory location created by GetCustomer?


My method...
public Customer GetCustomer(int customerID)
{
  //if customer exists, return referenceexisting customer
  // else
  //if customer doesn't exist in Singleton, then retrieve customer data from database and put in Singleton's list of   // customers
  Customer newcust = new Customer();
     MasterList.Add(newcust);

  return newcust;
}


Calling code
{
...
  private Customer localCust;
  localCust = GetCustomer(int custID);

 
}

When I make changes to the memory space for the customer object created by my singleton, those changes are not reflected in my localCust.

TIA,
Bruce
0
Comment
Question by:Bruce
[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
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 225 total points
ID: 40443230
Terms like "handle", "memory location" and "memory space" are usually mentioned in languages like C/C++, not really C#/VB.NET. Are you actually doing low-level memory manipulation, or did you just use the term casually?
0
 
LVL 1

Author Comment

by:Bruce
ID: 40443442
I'm old and started programming in C/C++.  Just using the terms casually I guess.  I still need to have a reference to the instantiated object and not the copy that is returned.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 225 total points
ID: 40443454
In .NET, you have reference types and value types. When a value type is returned from a method, you get a copy of the data; when a reference type is returned, you get a copy of the reference, not the data. Anything that is declared as class is a reference type; anything declared as struct is a value type. string is a reference type, but it behaves like a value type in that when you pass a string around you actually pass around a copy. All other primitives are value types.

If Customer is declared as class, then you are getting a reference to the data in memory, not the actual data.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 275 total points
ID: 40443507
Just a quick example of what Kaufmed is telling you:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EE_Q28562295
{
	class ClassCustomer
	{
		public int ID { get; set; }
		public string Name { get; set; }
	}

	struct StructCustomer
	{
		public int ID { get; set; }
		public string Name { get; set; }
	}

	class Program
	{
		static void Main(string[] args)
		{
			ClassCustomer cc = new ClassCustomer() { ID = 1, Name = "Paul" };
			StructCustomer sc = new StructCustomer() { ID = 1, Name = "Paul" };
			Console.WriteLine(string.Format("Class customer before change: ID = {0}; Name = {1}", cc.ID, cc.Name));
			Console.WriteLine(string.Format("Struct customer before change: ID = {0}; Name = {1}", sc.ID, sc.Name));
			ChangeClassCustomer(cc);
			ChangeStructCustomer(sc);
			Console.WriteLine(string.Format("Class customer after change: ID = {0}; Name = {1}", cc.ID, cc.Name));
			Console.WriteLine(string.Format("Struct customer after change: ID = {0}; Name = {1}", sc.ID, sc.Name));
			Console.ReadLine();
		}

		private static void ChangeClassCustomer(ClassCustomer data)
		{
			data.ID = 2;
			data.Name = "Peter";
		}

		private static void ChangeStructCustomer(StructCustomer data)
		{
			data.ID = 2;
			data.Name = "Peter";
		}
	}
}

Open in new window


Produces the following output:Capture.JPG
The class properties change and retain their changes because I have a reference to the instance.  The struct does not because it is a value type.

-saige-
0
 
LVL 1

Author Comment

by:Bruce
ID: 40443520
I found my issue... I was doing a deep clone of the object later on in the process and that was breaking the reference.  My localCust was remaining instantiated but the deep clone pointed my original customer object reference to a different space in the heap.  (More "old" but useful terms).
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40443529
Yeah anyone from a C++ background (myself included) get's bitten by that one at one time or another.

-saige-
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c#, case, if 4 37
C# LINQ 5 45
Timeouts during development 3 29
Run software updates from the website 6 50
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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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