Solved

C# Method Return Structure by Reference

Posted on 2010-11-11
6
896 Views
Last Modified: 2012-05-10
Hi,

Is it possible to return a structure by reference?

Example:
public struct myStruct
{
string foo;
string bar;
}

public myStruct[] Instances;
Instances = new myStruct[1];
Instances[0].foo = "Hello";
Instances[1].bar = "World";

public myStruct getInstance()
{
return Instances[0];
}

myStruct instance = getInstance();

This will return a copy of the structure, I want it to return the reference to it so I can do this:
myStruct instance = getInstance();
instance.foo = "Source";
instance.bar = "Code";

I know I can pass instance as a reference variable:
myStruct instance = new MyStruct();
getInstance(ref instance);

But I don't want to do this if I don't have to.

Many thanks,
Storm
0
Comment
Question by:Cyber-Storm
  • 3
  • 2
6 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 100 total points
ID: 34110227
The only way I can thing of doing it would be to return a pointer, but i don't think you want to be doing that.
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 150 total points
ID: 34110318
The simple answer is no. Are there ways around it? maybe.

http://msdn.microsoft.com/en-us/library/dd183752.aspx

Notice instead of using MyStruct for the array construction I am using object. I am boxing the structure inside and object therefore the object are moved around by reference.

DaTribe
class Program
{
	public struct myStruct
	{
		public string foo;
		public string bar;
	}

	public static object[] Instances;

	public static object getInstance()
	{
		return Instances[0];
	}

	static void Main(string[] args)
	{
		Instances = new object[2];

		Instances[0] = new myStruct {foo = "Hello"};
		Instances[1] = new myStruct {bar = "World"};

		var instance = getInstance();
		Console.WriteLine(object.ReferenceEquals(instance, Instances[0]));
		Console.ReadKey();
	}
}

Open in new window

0
 

Author Comment

by:Cyber-Storm
ID: 34110321
Yeah, wouldn't want to go the unsafe route, how would I fix the below then?
public struct myStruct
{
string foo;
string bar;
}

public myStruct[] Instances;
Instances = new myStruct[1];
Instances[0].foo = "Hello";
Instances[1].bar = "World";

public int getInstanceID()
{
return 0;
}

Now am forced to do this
Instances[getInstanceID()].foo = "Source";
Instances[getInstanceID()].bar = "Code";
but I want to do something like this instead
myStruct instance = Instances[getInstanceID()];

so that I can do this:
instance.foo = "Source";
instance.bar = "Code";
Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Cyber-Storm
ID: 34110382
Thanks DaTribe,
We posted at the same time so I didn't see yours.  I wouldn't want to go the object route as it's concidered a "dirty" solution.  But I fully understand your logic and while I know doing it this way would work, I was hoping for a "cleaner" solution and was reserving that as my last resort if I didn't come right here.
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34110449
The link I provided explains that it is not possible for two structs to have the same address so I do not think the approach you require is possible.

http://msdn.microsoft.com/en-us/library/dd183752.aspx

DaTribe
0
 

Author Closing Comment

by:Cyber-Storm
ID: 34111629
I implemented a class instead of a structure and exposed the variables as automatic properties, I think this is the cleanest and most elegant solution in this case.

Both good options you guys put forth as well, thanks guys.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

21 Experts available now in Live!

Get 1:1 Help Now