RE: How to swap two variable later?

From: tony lock (tonylock_at_discussions.microsoft.com)
Date: 10/01/04


Date: Fri, 1 Oct 2004 02:49:10 -0700

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



Relevant Pages

  • Re: XmlSerializer Collection with Collections
    ... > - a bunch of Option objects are contained within an OptionList ... > It is the extra level of object containment in the original version that ... >> public class TestSerializer ... >> public Question(string QuestionText, string Type, int Score, bool ...
    (microsoft.public.dotnet.xml)
  • Re: How to OPEN native PRINTER DIALOG -- Please HELP !!
    ... > function IsNetworkPrinter(PrinterName as string) as boolean ... Public Class YourPrintDialog ... Dim infos As New ArrayList ... ByRef cchBuffer As Int32) As Int32 ...
    (microsoft.public.dotnet.languages.vb)
  • Re: constructors/static methods and inheritance query
    ... public class BaseLine: IBaseLine { ... public static BaseLine Initialize(string x1, string y1, string ... public int keyLength; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Error : Complex DataBinding accepts as a data source either an IList or an IListSource
    ... > Public Class responseHistoricalData ... > Public equipmentID As String ... > Public proximity() As proximity ... > Private _serviceMeterHours As String ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: XmlSerializer Collection with Collections
    ... What you can do here is hide the OptionList from serialization as follows: ... > public class TestSerializer ... > public int AddQuestion(Question question) ... > public Question(string QuestionText, string Type, int Score, bool ...
    (microsoft.public.dotnet.xml)