Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Swapping two variables

Started by quasar3d Nov 29, 2004 at 3:59 AM 4 replies 1.3k views
Original Post
quasar3d
quasar3d
I just started with C#, and I need two swap to variables. I can create my own swap function, but I'm sure there must be one in the standard library already, but I couldn't find it. Does .net have a swap function, or do I have to make one myself? [Edited by - quasar3d on December 11, 2004 10:43:57 AM]
SumDude
SumDude
Why look for one if you can make one just as easy instead of hunting one down.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are inte
grekster
grekster
Yeah just write it yourself. Is there some reason you want one from the standard library?
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
quasar3d
quasar3d
I tried to make my own swap function, but it doesn't work.

this is the function:
public static void Swap(ref object a, ref object b)    {        object swap = a;        a = b;        b = swap;    }


and then I try to call it like this:

App.Swap(ref pen1,ref pen2);

but it doesn't work, it only works with objects, but not with other classes, like Pen in this case. Pen is derived from object though. I get this error:
Argument '2': cannot convert from 'ref System.Drawing.Pen' to 'ref object'
kSquared
kSquared
Quote:
it only works with objects, but not with other classes, like Pen in this case. Pen is derived from object though. I get this error:
Argument '2': cannot convert from 'ref System.Drawing.Pen' to 'ref object'


That's right. ref or out parameters in methods must exactly match the types in their declarations, so there isn't a generic solution to your problem in the 1.0 or 1.1 versions of the Framework and C# compiler. You'll therefore need to make a specific Swap method for each object type you want to swap.

Thankfully, version 2.0 of the Framework makes this much easier, because it has generics. If you're using that version, then what you want is:

void Swap<T>(ref T item1, ref T item2){   T swap = item1;   item1 = item2;   item2 = swap;}
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<
Rattenhirn
Rattenhirn
The static methods
System.Threading.Interlocked.Exchange( ... );
swap two variables in a threadsafe manner.

However, the limitation of the ref parameters still apply...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.