Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-14
6
Medium Priority
?
232 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 900 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 900 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 1100 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

670 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