473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to swap two variable later?

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;
}
}
}
Nov 16 '05 #1
9 6672
add another variable of the same type

happy swapping!
Nov 16 '05 #2
-----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.
Nov 16 '05 #3
Jongmin wrote:
-----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.

to you mean something like that?

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

bye
Rob
Nov 16 '05 #4
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....
Nov 16 '05 #5
Jongmin wrote:
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....


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

bye
Rob
Nov 16 '05 #6
What about a deep copy? Any good?
Nov 16 '05 #7
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!)
"Jongmin Lee" <an*******@discussions.microsoft.com> wrote in message
news:1a****************************@phx.gbl...
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;
}
}
}

Nov 16 '05 #8
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;}
}
}
}
"Jongmin Lee" wrote:
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;
}
}
}

Nov 16 '05 #9
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
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Steve | last post by:
How do you rewrite the swap function without using a tmp variable in the swap function???? int main() { int x = 3; int y = 5; // Passing by reference
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
21
by: Abhishek Jha | last post by:
hello all, this has been an interesting topic since long. you can swap two numbers (say a and b) using temp(as third variable). you can also swap without using third variable.likeThis was also...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
14
by: pras.vaidya | last post by:
hi, please help me with this problem : - how to swap two addresses .For eg variable i is located at 2000 and j at 3000 . Now i call swap function . Result should be that i should be now having...
7
by: sachsase | last post by:
//swap of 2 variables void main() { int a=27,b=46; a=1?a-(b=1?((a=1?a+b:0)-b):0):0; printf("a=%db=%d",a,b); } I got output for this problem as a=46;b=27 Same but using scanf to read...
25
by: indrawati.yahya | last post by:
OK, before you all stab me with your OT pitchfork, I just want to mention that this question is indeed related to c.l.c++. My question is, does the well-known method to swap two integers produce...
3
by: NK | last post by:
char *a="nnnnnnn fgjfjgjf"; char *b="ABC jjjjjjjjjhhhh"; to swap these two string...... this swap fn is working template<class T> void Swap( const T *&a,const T *&b)//no need to write...
10
by: Chad | last post by:
I hope this question doesn't appear twice. I tried posting it once, however, I got a stupid 505 internal error on google. When this happens, the post will either post 30 minutes later or won't post...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.