<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wikidot="http://www.wikidot.com/rss-namespace">

	<channel>
		<title>Returning 2 Variables with Methods</title>
		<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods</link>
		<description>Posts in the discussion thread &quot;Returning 2 Variables with Methods&quot;</description>
				<copyright></copyright>
		<lastBuildDate>Fri, 06 Mar 2026 11:28:25 +0000</lastBuildDate>
		
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-2374158</guid>
				<title>(no title)</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-2374158</link>
				<description></description>
				<pubDate>Sat, 12 Sep 2015 10:16:41 +0000</pubDate>
				<wikidot:authorName>pepote</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>I haven't read all the post, too much&#8230;</p> <p>Basicly C or any derivate DOES NOT support returning more than 1 variable. So you cannot return 2 or more values.<br /> The options are:</p> <p>1-encapsulate the returning values in an kind of object<br /> 2-using in/out parameters</p> <p>For 1:<br /> Old C hadn't objects so, the only way to do this was using STRUCTS. Well you could use arrays instead, but a struct makes more sense from a semantic point of view and less prone to errors. So, my preference (because my background is C hehe) is using first structs, then objects (objects add a significative amoount of overhead), then anykind of collection (arrays, lists, dictionarys -why not? ;) - ).</p> <p>For 2:<br /> If you have to use OUT parameters&#8230; well.. out types are designed for values that CAN change inside a method, not to workaround the limitations of the language. What if your method creates 2 objects, who would be the parameters? empty obviously because the user should never give any because is the method itself which creates the objects.</p> <p>The key is OUT parameters are essentially related to the meaning of the variables.If the parameter needs to be changed in the method, then it's ok being out&#8230; if you are using it like a mechanism to return 2 values, then it's not a good method, but many times the language limitations imposes you the way to follow.</p> <p>Use Structs.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1851068</guid>
				<title>(no title)</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1851068</link>
				<description></description>
				<pubDate>Fri, 13 Sep 2013 07:02:46 +0000</pubDate>
				<wikidot:authorName>BenStark</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Holy Cow, that was a lot more information than I expected. I consider myself a beginner, but I understood most of what you posted. I think I am getting more comfortable with C#. I am on tutorial #22. Maybe I'll finish this weekend.</p> <p>Thank you very much.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1850698</guid>
				<title>(no title)</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1850698</link>
				<description></description>
				<pubDate>Thu, 12 Sep 2013 17:41:59 +0000</pubDate>
				<wikidot:authorName>rbwhitaker</wikidot:authorName>				<wikidot:authorUserId>88099</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>By the way, PiscesMike, you can get the code snippets with something like this:</p> <p><span style="white-space: pre-wrap;">[[code]]</span><br /> <span style="white-space: pre-wrap;">public&#32;static&#32;void&#32;Main(string[]&#32;args)</span><br /> <span style="white-space: pre-wrap;">{</span><br /> <span style="white-space: pre-wrap;">&#32;&#32;&#32;&#32;Console.WriteLine(&quot;Hello&#32;World!&quot;);</span><br /> <span style="white-space: pre-wrap;">}</span><br /> <span style="white-space: pre-wrap;">[[/code]]</span></p> <p>This turns out like this:</p> <div class="code"> <pre><code>public static void Main(string[] args) { Console.WriteLine(&quot;Hello World!&quot;); }</code></pre></div> <p>You can specify a language like this:</p> <p><span style="white-space: pre-wrap;">[[code&#32;type=&quot;java&quot;]]</span><br /> <span style="white-space: pre-wrap;">public&#32;static&#32;void&#32;Main(string[]&#32;args)</span><br /> <span style="white-space: pre-wrap;">{</span><br /> <span style="white-space: pre-wrap;">&#32;&#32;&#32;&#32;Console.WriteLine(&quot;Hello&#32;World!&quot;);</span><br /> <span style="white-space: pre-wrap;">}</span><br /> <span style="white-space: pre-wrap;">[[/code]]</span></p> <p>This turns out like this:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">Main</span><span class="hl-brackets">(</span><span class="hl-identifier">string</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Console</span><span class="hl-code">.</span><span class="hl-identifier">WriteLine</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello World!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <p>Unfortunately, there's no C#. So I'm forced to use Java. That gets you syntax highlighting that's fairly close at least. I've talked with the Wikidot people about adding C# in, but they can't do it. They're using a PHP module to do this (PEAR::TextHighlighter or something) so the only way to get it would be for me to go add a C# syntax highlighter to that project. I just don't have time for that.</p> <p>There shouldn't be any restrictions on new users for this. I know some people have ran into problems trying to post links, and I usually try to go in and edit their posts so the link works (if it's not a spam link). I often do the same when people post code, but I'll let you go ahead and edit your own posts, now that you know how it works!</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1850684</guid>
				<title>(no title)</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1850684</link>
				<description></description>
				<pubDate>Thu, 12 Sep 2013 17:33:01 +0000</pubDate>
				<wikidot:authorName>rbwhitaker</wikidot:authorName>				<wikidot:authorUserId>88099</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Your comments made me laugh, PiscesMike! I have to correct you though. My wisdom is bounded. It's just over 9000! (Hee hee hee&#8230; It's funny because it's a meme&#8230;.)</p> <p>What PiscesMike said is the same reason why I like option #2 the best in most cases as well. You've got named properties (or instance variables, depending on how you implement it) bundled up in a reusable package. Option #1 and #3 don't have intelligent names associated with what you get back, while option #4 doesn't bundle them together into a reusable package.</p> <p>In fact, if the mental model that you have is &quot;I have a trajectory that has two pieces of information that define it (angle and power),&quot; then to be true to object-oriented principles, you should definitely make a type out of it. Object-orientation tells you that you build reusable types out of the concepts that your program has or the mental model of the programmer or user.</p> <p>The difference between a class and a struct is subtle but significant. I know I cover it in my tutorials, but I also know I cover it much better in my book. I won't go into the details about it here and now unless you're super interested in it. Either one could be used here though.</p> <p>Generally speaking, people put new types (classes, structs, enumerations&#8230;) in their own file, with a matching name. If your <em>Main</em> method is in a <em>Program</em> class (that's the default, right?) then it is probably also contained in a Program.cs file. Unlike some other languages, you're not required to make the file name and type name match, nor are you limited to just putting one type in each file.</p> <p>What PiscesMike was describing would have you put the class or struct in the same Program.cs file, which will work just fine. I have a personal preference (perhaps its the Java programming that I've done) to try to stick to the 1 type per file, and keep the file name and type name sync'd. It just makes it easier for me to find the code I'm looking for.</p> <p>There are, of course, exceptions to this. Not the least of which is if you've got a class that's used by the world, and another that is very closely associated with that class. As in, it's never used except in conjunction with that class. Then I might consider putting this secondary/helper/utility class in the same file as the main class.</p> <p>I believe in my tutorial on creating classes, I describe how to actually create a class in a second file.</p> <p>The one thing you have to watch out for is the namespace that the different types (classes/structs) are in. A type can use anything in the namespace that it is contained in (assuming it's accessibility (the public/private/protected/internal junk) says it can). It can also use things in other namespaces, it just has to add a <em>using</em> directive at the top of the file (there are already a bunch there that you can mimic) or a fully qualified name. (That is, instead of just using the type name, you preface it with the namespace name that the type belongs to.)</p> <p>And by the way, when you start getting lots of files and lots of types in your project, you'll usually want to start grouping things into folders. Generally speaking, each folder will get its own namespace, and the name of the namespace is whatever you had plus the folder name. (If you have a game that has a UI folder with some files in it, plus a subfolder containing a Controls folder, your namespace for things in the UI folder might be <em>AwesomeCompanyName.Game9FromOuterSpace.UI</em>, while things in the subfolder would be in the namespace <em>AwesomeCompanyName.Game9FromOuterSpace.UI.Controls</em>.</p> <p>Once again, though, that's the standard practice, but there's no requirement on folder structures and namespaces. You can break with tradition if you want.</p> <p>Once again&#8230; I've gone on and on. I need a cork.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1850668</guid>
				<title>Trajectory OutgoingTrajectory=IncomingTrajectory;</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1850668</link>
				<description></description>
				<pubDate>Thu, 12 Sep 2013 17:06:12 +0000</pubDate>
				<wikidot:authorName>PiscesMike</wikidot:authorName>				<wikidot:authorUserId>1721619</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>Looking through this code example I saw this line and realized it might be a little confusing. You normally will NOT want to set the output to the input, however since I have no code that does the manipulation to the variables, I had to put something in so it would compile (can't return an empty variable, in this case the Trajectory struct). In most scenarios, you will want something that looks like the following:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">namespace</span><span class="hl-code"> </span><span class="hl-identifier">WindowsGame1</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">TestClass</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Trajectory</span><span class="hl-code"> </span><span class="hl-identifier">TestFunction</span><span class="hl-brackets">(</span><span class="hl-identifier">Trajectory</span><span class="hl-code"> </span><span class="hl-identifier">IncomingTrajectory</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">// Code to manipulate IncomingTrajectory here</span><span class="hl-code"> </span><span class="hl-identifier">Trajectory</span><span class="hl-code"> </span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Trajectory</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-comment">//create a new temporary instance of the Trajectory struct</span><span class="hl-code"> </span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">Direction</span><span class="hl-code"> = </span><span class="hl-identifier">IncomingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">Direction</span><span class="hl-code">; </span><span class="hl-comment">//set our new temporary direction instance to the one passed in</span><span class="hl-code"> </span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">speed</span><span class="hl-code"> = </span><span class="hl-identifier">IncomingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">speed</span><span class="hl-code">; </span><span class="hl-comment">//set our new temporary speed instance to the one passed in</span><span class="hl-code"> </span><span class="hl-comment">//do whatever manipulation to the variables you want/need to (or retrieve input from the user); in this case I just added some random values to the incoming variables. </span><span class="hl-code"> </span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">Direction</span><span class="hl-code"> = </span><span class="hl-identifier">Vector3</span><span class="hl-code">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">Direction</span><span class="hl-code">, </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Vector3</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">))</span><span class="hl-code">; </span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">speed</span><span class="hl-code"> = </span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code">.</span><span class="hl-identifier">speed</span><span class="hl-code">+</span><span class="hl-number">1</span><span class="hl-code">; </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">OutgoingTrajectory</span><span class="hl-code">; </span><span class="hl-comment">//return the manipulated figures in a Trajectory struct</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span></pre></div> </div> <p>Sorry if that caused any confusion, I'm still somewhat new and learning myself.</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1850651</guid>
				<title>(no title)</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1850651</link>
				<description></description>
				<pubDate>Thu, 12 Sep 2013 16:44:29 +0000</pubDate>
				<wikidot:authorName>PiscesMike</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>On a side note, is there any way a lowly site newbie can get those neat little code boxes? Is it implemented in a tag? (&lt;code&gt; &lt;/code&gt;)?</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1850640</guid>
				<title>(no title)</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1850640</link>
				<description></description>
				<pubDate>Thu, 12 Sep 2013 16:25:00 +0000</pubDate>
				<wikidot:authorName>PiscesMike</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Greetings BenStark, and hello again RB.</p> <p>For what it's worth Ben, I support using method #2. I've found it to be the easiest to understand and implement. There's also the bonus (correct me if I'm wrong, Mr. Whitaker), that you can access the struct members by name if needed. For example, with the following code:</p> <p>struct Trajectory<br /> {<br /> public Vector3 Direction;<br /> public float speed;<br /> }</p> <p>Trajectory MyTrajectory;</p> <p>You can manipulate settings in the following manner:</p> <p>TestClass ClassTest=new TestClass(); //sample class</p> <p>MyTrajectory=new Trajectory();<br /> MyTrajectory.Direction = new Vector3(0.0f, 0.0f, 1.0f);<br /> MyTrajectory.speed = 0.0f;</p> <p>ClassTest.TestFunction(MyTrajectory); //function call to manipulate the Trajectory MyTrajectory</p> <p>When calling classes, you can pass the struct in and return it as any other variable.</p> <p>namespace WindowsGame1<br /> {</p> <p>class TestClass<br /> {<br /> public Trajectory TestFunction(Trajectory IncomingTrajectory)<br /> {</p> <p>// Code to manipulate IncomingTrajectory here<br /> Trajectory OutgoingTrajectory=IncomingTrajectory;<br /> return OutgoingTrajectory;<br /> }</p> <p>}<br /> }</p> <p>Of course, all of this working is contingent on placing the struct in the appropriate place (in the main class, directly after the using statements, so all the other classes can inherit it). Hope this is helpful, and if I've gotten anything incorrect, I defer to the unbounded wisdom of Mr. Whitaker. Stick with him, he knows how to put a whooping on some code!</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1850528</guid>
				<title>Re: Returning 2 Variables with Methods</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1850528</link>
				<description></description>
				<pubDate>Thu, 12 Sep 2013 12:41:27 +0000</pubDate>
				<wikidot:authorName>rbwhitaker</wikidot:authorName>				<wikidot:authorUserId>88099</wikidot:authorUserId>				<content:encoded>
					<![CDATA[
						 <p>The short answer is no. C# isn't &quot;snakey&quot; enough for that. That was a feature of Python that I always liked. I wish more languages had it, but C# doesn't. Not directly anyway.</p> <p>Of course, there are plenty of ways to get multiple things back from a method. Let me enumerate them:</p> <h2><span>1. <strong>Return an Array</strong></span></h2> <p>This would look like this:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-types">double</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">FindTrajectory</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">// Do some work...</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-identifier">p</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <p>Then:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-types">double</span><span class="hl-brackets">[]</span><span class="hl-code"> </span><span class="hl-identifier">results</span><span class="hl-code"> = </span><span class="hl-identifier">FindTrajectory</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">angle</span><span class="hl-code"> = </span><span class="hl-identifier">results</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code">; </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">power</span><span class="hl-code"> = </span><span class="hl-identifier">results</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-code">;</span></pre></div> </div> <p>This is probably the simplest of approaches, but it has two serious drawbacks:</p> <ul> <li>It works best if all of the return values are of the same type, otherwise you have to use an array of type <em>object</em> and perform a bunch of casting.</li> <li>You have to manually pack and unpack the array, which means there's the potential for error. (&quot;Was power first or second?&quot; etc.)</li> </ul> <h2><span>2. <strong>Create an Object to Store Them All</strong></span></h2> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Trajectory</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">Angle</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">get</span><span class="hl-code">; </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">set</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">Power</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">get</span><span class="hl-code">; </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">set</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Trajectory</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">angle</span><span class="hl-code">, </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">power</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-identifier">Angle</span><span class="hl-code"> = </span><span class="hl-identifier">angle</span><span class="hl-code">; </span><span class="hl-identifier">Power</span><span class="hl-code"> = </span><span class="hl-identifier">power</span><span class="hl-code">; </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-brackets">}</span></pre></div> </div> <p>(And actually, looking back at it, that is a perfect time for a struct instead of a class. Either one will work, functionally.)</p> <p>Then:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Trajectory</span><span class="hl-code"> </span><span class="hl-identifier">GetTrajectory</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">// Some work here...</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Trajectory</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-identifier">p</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">Trajectory</span><span class="hl-code"> </span><span class="hl-identifier">trajectory</span><span class="hl-code"> = </span><span class="hl-identifier">GetTrajectory</span><span class="hl-brackets">()</span><span class="hl-code">;</span></pre></div> </div> <p>That is a more &quot;object-oriented&quot; approach. It solves some of the problems that the first thing had. Your types can be different, and because the properties that the data is stored in are named (&quot;Angle&quot; and &quot;Power&quot;) is is unlikely that anyone would mess it up.</p> <p>But on the other hand, it means building a full class for use in potentially one location. Which if that's the case, seems like overkill for sure. (If you're using this data all over the place, this is probably what I'd recommend.)</p> <h2><span>3. <strong>Use the Generic <em>Tuple</em> Class</strong></span></h2> <p>If you like the above approach, but you don't like building a whole new class for a single use, you might consider using the <em>Tuple</em> class. It's generic, so you have to understand <a href="http://rbwhitaker.wikidot.com/c-sharp-generics">C# generics</a> for it to be useful. I don't know what level you see yourself at, but generics aren't exactly a beginner level feature. I'm not going to explain generics here, but see that link for more information.</p> <p>This would look more like this:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Tuple</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">, </span><span class="hl-types">double</span><span class="hl-code">&gt; </span><span class="hl-identifier">GetTrajectory</span><span class="hl-brackets">()</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">// Do some work here.</span><span class="hl-code"> </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Tuple</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">, </span><span class="hl-types">double</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-identifier">p</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <div class="code"> <div class="hl-main"> <pre><span class="hl-identifier">Tuple</span><span class="hl-code">&lt;</span><span class="hl-types">double</span><span class="hl-code">, </span><span class="hl-types">double</span><span class="hl-code">&gt; </span><span class="hl-identifier">trajectory</span><span class="hl-code"> = </span><span class="hl-identifier">GetTrajectory</span><span class="hl-brackets">()</span><span class="hl-code">; </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">angle</span><span class="hl-code"> = </span><span class="hl-identifier">trajectory</span><span class="hl-code">.</span><span class="hl-identifier">Item1</span><span class="hl-code">; </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">power</span><span class="hl-code"> = </span><span class="hl-identifier">trajectory</span><span class="hl-code">.</span><span class="hl-identifier">Item2</span><span class="hl-code">;</span></pre></div> </div> <p>The <em>Tuple</em> class lets you package up multiple values to pass around. Because it's generic, you could create one that has any parameter types. This keeps your type safety, so you know what type of data you're working with at all times, and you don't have to build and maintain a new class on your own, but <em>Item1</em> and <em>Item2</em> are terrible names. They're completely meaningless. If you built your own class, that's not a problem.</p> <p>Again, I'd only recommend doing this if you are only going to use this in one place. (Even then, I'd personally still give strong consideration to option #2.)</p> <h2><span>4. <strong>Use Output Parameters</strong></span></h2> <p>The fourth option is to use output parameters. This, again, isn't exactly C# 101. The method would be restructured to look like this:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">GetTrajectory</span><span class="hl-brackets">(</span><span class="hl-identifier">out</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">angle</span><span class="hl-code">, </span><span class="hl-identifier">out</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">power</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-comment">// Do some work...</span><span class="hl-code"> </span><span class="hl-identifier">angle</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code">; </span><span class="hl-identifier">power</span><span class="hl-code"> = </span><span class="hl-identifier">p</span><span class="hl-code">; </span><span class="hl-brackets">}</span></pre></div> </div> <p>And then you'd call it like this:</p> <div class="code"> <div class="hl-main"> <pre><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">angle</span><span class="hl-code">; </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">power</span><span class="hl-code">; </span><span class="hl-identifier">GetTrajectory</span><span class="hl-brackets">(</span><span class="hl-identifier">out</span><span class="hl-code"> </span><span class="hl-identifier">angle</span><span class="hl-code">, </span><span class="hl-identifier">out</span><span class="hl-code"> </span><span class="hl-identifier">power</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">// Use angle and power here however you want.</span></pre></div> </div> <p>Using output parameters means that the two methods are essentially sharing the same memory location, and can both read from it and write to it. With the <em>out</em> keyword, we establish the expectation that the called method will write to the location before it finishes. (If you don't, it won't compile.)</p> <p>Output parameters can be very useful, and very powerful. This exact situation is a big part of why they exist. But it takes a little bit of getting used to. Having two methods share the exact same memory/storage location like this has some gotchas that take some experience to get used to. Output parameters often are overused when people first learn about them. (Don't go make every method use output parameters.)</p> <p>For the sake of being complete (I've typed all this already; I might as well keep going, right?) I should also mention that you can also have <em>reference parameters</em>, which look the same, but use the <em>ref</em> keyword instead of <em>out</em>. These, too, share the same storage location in the calling site and the called method. However, <em>ref</em> established the expectation that the caller will be the one to make sure it's assigned. The called method can assume there's already some data there to work with.</p> <p>Whew. That was a lot. Hopefully I've helped you, not confused you. My personal normal approach is to build a class or struct for it (Option #2). But there is a time and a place for all of the above. You just have to consider the pros and cons of each, and what you're hoping to achieve.</p> <p>Let me know if you have other questions from what I said&#8230;</p> 
				 	]]>
				</content:encoded>							</item>
					<item>
				<guid>http://rbwhitaker.wikidot.com/forum/t-694573#post-1850488</guid>
				<title>Returning 2 Variables with Methods</title>
				<link>http://rbwhitaker.wikidot.com/forum/t-694573/returning-2-variables-with-methods#post-1850488</link>
				<description></description>
				<pubDate>Thu, 12 Sep 2013 10:11:30 +0000</pubDate>
				<wikidot:authorName>BenStark</wikidot:authorName>								<content:encoded>
					<![CDATA[
						 <p>Okay, so I was goofing around, and I tried to make a method that would return two variables (you can do it in a more snakey language). Is it possible in C#?</p> <p>Let's say I am frequently using angle and power. Could I do something like</p> <p>angle, power = FindTrajectory()</p> <p>void FindTrajectory()<br /> {<br /> //ask user for angle and power<br /> return a, p;<br /> }</p> <p>Is that possible somehow?</p> 
				 	]]>
				</content:encoded>							</item>
				</channel>
</rss>