Solved

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

Posted on 2014-11-14
6
223 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 74

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 74

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 32

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 32

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

13 Experts available now in Live!

Get 1:1 Help Now