Solved

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

Posted on 2014-11-14
6
228 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 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 33

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 33

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 42
location of a form 2 25
How to read text with RegEx... 2 26
C#.net split string and then check appropriate checkboxlist 4 27
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

820 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