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

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
LVL 1
BruceAsked:
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.

käµfm³d 👽Commented:
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
BruceAuthor Commented:
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
käµfm³d 👽Commented:
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

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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

it_saigeDeveloperCommented:
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
BruceAuthor Commented:
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
it_saigeDeveloperCommented:
Yeah anyone from a C++ background (myself included) get's bitten by that one at one time or another.

-saige-
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.