Thread: a strange thing

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    a strange thing

    Hi;

    I create a namespace and inside the class there is one function:

    namespace mynamespace
    {
    public class TEST
    {
    public void test(int i)
    {
    i = 1;
    }

    }

    }
    made it as *.dll, and loaded it into another project:
    .........
    public class Mytest
    {
    public static void Main()
    {
    Mytest mytest = new Mytest();
    mytest.test1();

    }

    public void test1()
    {
    int t=0;
    mynamespace.TEST Test = new mynamespace.TEST();
    Test.test(t);
    Console.WriteLine(t);

    }

    so the output should be 1, wasn't it?
    but it was 0, what was wrong?
    this was weird
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    There is a basic difference in all programming languages by passing a variable by value ( as you did ) and by reference ( what you wanted to do ).

    Passing it by value means your local variable i in the function is a copy of what you gave as parameter. So modifying i does not change the variable given as parameter. By reference means no variable is copied, only a reference to the original variable is given. Changing the reference changes the original variable as you want it in this case.

    Look up the in, out and ref keywords in C# for a better understanding of passing by value or by reference.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    I got it, thank you
    Don't laugh at me,I am just a SuperNewbie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange struct syntax; type name:number
    By OnionKnight in forum C Programming
    Replies: 1
    Last Post: 11-19-2006, 08:23 PM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  3. "new" is strange
    By The Wazaa in forum C++ Programming
    Replies: 26
    Last Post: 03-06-2006, 12:32 PM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. Very strange linker error
    By hajohoff in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 07:27 AM