How to swap two variable later?

J

Jongmin Lee

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;
}
}
}
 
J

Jongmin

-----Original Message-----
add another variable of the same type

happy swapping!


.

My issue is some different from simple.

I want to swap those variables late.
 
R

Robert Jordan

Jongmin said:
My issue is some different from simple.

I want to swap those variables late.


to you mean something like that?

vois Swap(ref SomeClass a, ref SomeClass b) {
SomeClass t = a;
a = b;
b = t;
}

bye
Rob
 
J

Jongmin

vois Swap(ref SomeClass a, ref SomeClass b) {
SomeClass t = a;
a = b;
b = t;
}

bye
Rob
.

//In this place, I don't swap those variable.
//Just save two variables, and swap those when requesting.
Swap(ref SomeClass a, ref SomeClass b) {
SomeClass t = a;
a = b;
b = t;
}

// Please run my code snippet....
 
R

Robert Jordan

Jongmin said:
//In this place, I don't swap those variable.
//Just save two variables, and swap those when requesting.
Swap(ref SomeClass a, ref SomeClass b) {
SomeClass t = a;
a = b;
b = t;
}

// Please run my code snippet....

You *have* to use something like my method. There is
no other way to do that.

bye
Rob
 
J

James Curran

Reconsider how you access your data:
using System;
using System.Collections;

namespace Test
{
public class MyClass
{
public static void Main()
{
MyData myc = new MyData("A", "B");

Console.WriteLine(myc.First);// Print "A"
Console.WriteLine(myc.Second);// Print "B"

myc.Swap();

Console.WriteLine(myc.First);// Print "B"
Console.WriteLine(myc.Second);// Print "A"
}
}


public class MyData
{
string _A;
string _B;

public MyData(string a, string b)
{
_A = a;
_B = b;
}

public string First
{
get { return _A; }
}

public string Second
{
get { return _B; }
}

public void Swap()
{
string t = _A;
_A = _B;
_B = t;
}

}


--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
G

Guest

Object variables are pointers, the problem is you cannot manipulate the
contents of a string only create a new one. The following wraps the string in
a new object which then behaves the way you expect. It is similar to James
Curran's solution, but perhaps closer to what you want.

namespace Test
{
class MyClass
{
[STAThread]
static void Main(string[] args)
{
StringObj mainA, mainB;
mainA = new StringObj("A");
mainB = new StringObj("B");

Swap swapCommand = new Swap(mainA, mainB);
Console.WriteLine(mainA.ObjString);// Print "A"
Console.WriteLine(mainB.ObjString);// Print "B"

swapCommand.Do(); // Swap take place here...
Console.WriteLine(mainA.ObjString);//want to Print "B" not A
Console.WriteLine(mainB.ObjString);//want to print "A" not B
}
}
public class Swap
{
StringObj _A;
StringObj _B;

public Swap(StringObj a, StringObj b)
{
_A = a;
_B = b;
}

public void Do()
{
string temp;
temp = _A.ObjString;
_A.ObjString = _B.ObjString;
_B.ObjString = temp;
}
}
public class StringObj
{
string _A;
public StringObj(string a)
{
_A = a;
}
public string ObjString
{
get{ return _A;}
set{ _A = value;}
}
}
}
 
G

Guest

Hi,

It looks impossible to do this in "safe" code,
but there is a simple trick:

<changes in your code>

using System;
using System.Collections;

namespace Test
{
public class MyClass
{
public static void Main()
{
string[] mainA=new string[1];
string[] mainB=new string[1];
mainA[0]="A";
mainB[0]="B";

Swap swapCommand = new Swap(mainA, mainB);
Console.WriteLine(mainA[0]);// Print "A"
// i fix your bug here
Console.WriteLine(mainB[0]);// Print "B"

swapCommand.Do(); // Swap take plase here...
Console.WriteLine(mainA[0]);//want to Print "B" not A
// i fix your bug here
Console.WriteLine(mainB[0]);//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[0];

_A[0] = _B[0];
_B[0] = temp;
}
}
}

HTH

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top