How to swap two variable later?
From: Jongmin Lee (anonymous_at_discussions.microsoft.com)
Date: 09/30/04
- Next message: Michael Earls: "Re: XML Class Deserialization from Class itself"
- Previous message: Michael Earls: "Re: DataGrid column heading"
- Next in thread: Joep: "Re: How to swap two variable later?"
- Reply: Joep: "Re: How to swap two variable later?"
- Reply: James Curran: "Re: How to swap two variable later?"
- Reply: tony lock: "RE: How to swap two variable later?"
- Reply: Marcin Grzębski: "Re: How to swap two variable later?"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 30 Sep 2004 10:39:21 -0700
Hi Everybody,
I have very simple code snippet to explain my problem.
Class "Swap" is construncted in "Main" with two initial
variables.
Later, "Swap" class is going to swap those two variables.
How to implement this Swap Class?
Because C# doesn't have pointer, I can't do.
Please give me any idea....
Thanks,
Jongmin
//
using System;
using System.Collections;
namespace Test
{
public class MyClass
{
public static void Main()
{
string mainA, mainB;
mainA = "A";
mainB = "B";
Swap swapCommand = new Swap(mainA, mainB);
Console.WriteLine(mainA);// Print "A"
Console.WriteLine(mainA);// Print "B"
swapCommand.Do(); // Swap take plase here...
Console.WriteLine(mainA);//want to Print "B" not A
Console.WriteLine(mainA);//wnat to print "A" not B
}
}
public class Swap
{
string _A;
string _B;
public Swap(string a, string b)
{
_A = a;
_B = b;
}
public void Do()
{
string temp;
temp = _A;
_A = _B;
_B = temp;
}
}
}
- Next message: Michael Earls: "Re: XML Class Deserialization from Class itself"
- Previous message: Michael Earls: "Re: DataGrid column heading"
- Next in thread: Joep: "Re: How to swap two variable later?"
- Reply: Joep: "Re: How to swap two variable later?"
- Reply: James Curran: "Re: How to swap two variable later?"
- Reply: tony lock: "RE: How to swap two variable later?"
- Reply: Marcin Grzębski: "Re: How to swap two variable later?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|