.Net ramblings
# Tuesday, 10 October 2006
A generic Swap function
if i ever forget how to write this function then... well, let's hope i never do.  i was really surprised not to be able to find it in the SDK.  i'm posting it here for reference anyway.  note: this can only be used in .Net 2.0 which supports generics.
/// <summary>
/// Swap 2 objects
/// </summary>
public static void Swap<T>(ref T first, ref T second)
{
T tmp = first;
first = second;
second = tmp;
}


Tuesday, 10 October 2006 16:55:31 (GMT Daylight Time, UTC+01:00)  #    Comments [3]  .Net General

Sunday, 15 October 2006 15:35:09 (GMT Daylight Time, UTC+01:00)
You dont need to use the temp variable.

assume we have

int x = 5;

int y =10;

x = (x+y) i.e (10+5) x = 15
y = (x-y) i.e (15-10) y = 5

x = (x-y) i.e (15-5) x = 10

end result
x=10
y = 5
Gurpreet
Sunday, 15 October 2006 16:12:42 (GMT Daylight Time, UTC+01:00)
yes that works for numbers, but the templated version works for any data type.
also you're still doing 3 assignments so not much difference there :)
tim
Sunday, 26 November 2006 19:22:59 (GMT Standard Time, UTC+00:00)
Tim,

I just replied to an old forum post you made about how to post code blocks in DasBlog... here it is

You can just use the "Insert Code" button in the FreeTextBox.

For me, it is in the lower right hand corner of the FTB toolbar.

If you want to see the result, just poke around my site...
[url]http://www.chrismay.org[/url]

I have made some changes to the code that does the formatting to solve the problem I found of indents being lost when coverting vb.net code, and I also updated my def file to include JScript/JavaScript as an option for including.

Feel free to email me at chris AT christophermayinc DOT COM with any questions.

Chris
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview