Re: How to swap two variable later?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: James Curran (JamesCurran_at_mvps.org)
Date: 09/30/04


Date: Thu, 30 Sep 2004 16:16:02 -0400


    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" <anonymous@discussions.microsoft.com> wrote in message
news:1a0001c4a714$6b39a160$a401280a@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;
>       }
>    }
> }


Relevant Pages

  • Re: Using ConfigurationManager.AppSettings.Get() value as a constant string
    ... I must pass it as a constant string value to an attribute. ... public class SomeAttribute: Attribute ... public static void Main ... the license of the library allows modification ...
    (microsoft.public.dotnet.languages.csharp)
  • help...
    ... public class Main { ... public static void main{ ... String name = JOptionPane.showMessageDialog(null, "Hello world: ...
    (comp.lang.java.beans)
  • Date different
    ... How to Calculate Date diff? ... public class Dir_t { ... Dir_t (String path) { ... public static void main { ...
    (comp.lang.java.programmer)
  • Dir filter how to set ?
    ... public class Dir { ... Dir (String path) { ... public static void main { ...
    (comp.unix.programmer)
  • Re: very new to java
    ... public class Welcome ... public static void main ... The simple answer: you are missing quotes around your string: ... where name and birthPlace are variables pointing to your name and birth ...
    (comp.lang.java.help)