rusoaica's Profile User Rating: *****

Reputation: 212 Stalwart
Group:
Authors
Active Posts:
675 (0.6 per day)
Joined:
10-March 12
Profile Views:
7,701
Last Active:
User is offline Mar 27 2015 04:57 AM
Currently:
Offline

Previous Fields

Country:
RO
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
AMD
Favorite Gaming Platform:
PC
Your Car:
Who Cares
Dream Kudos:
250

Latest Visitors

Icon   rusoaica I'd love to change the World, but they won't give me the Source Code

Posts I've Made

  1. In Topic: Interfaces

    Posted 12 Feb 2015

    You are welcome!
  2. In Topic: passing an object as a parameter creates a copy of it, how to avoid it

    Posted 17 Dec 2014

    i'm glad i could help :)
  3. In Topic: passing an object as a parameter creates a copy of it, how to avoid it

    Posted 17 Dec 2014

    i see two options here:
    you either use a method with a reference object parameter, and you apply the action on both objects, like this:
    static void StopPlayer(ref MyPlayer player)
    {
        player.Stop();
    }
    
    static void Main()
    {
        MyPlayer OriginalPlayer = new MyPlayer();
        StopPlayer(ref OriginalPlayer);   
    }
    
    

    or, you make the player a class-level field and apply the actions in a non-parameters method directly on the original variable. Like so:
    MyPlayer OriginalPlayer = new MyPlayer();
    
    static void StopPlayer()
    {
        OriginalPlayer.Stop();
    }
    
    static void Main()
    {
        StopPlayer(); 
    }
    
    
  4. In Topic: passing an object as a parameter creates a copy of it, how to avoid it

    Posted 17 Dec 2014

    when you pass a parameter by value, whatever changes you make to that parameter are not reflected on the original parameter (but over an instance, a copy of it). On the other hand, when you pass a parameter by reference, any action you apply on that parameter is reflected on the original parameter. So, for example:

    static void Method(ref int i)
    {
        i = i + 44;
    }
    
    static void Main()
    {
        int val = 1;
        Method(ref val);
        Console.WriteLine(val);
        // Output: 45
    }
    
    


    So, the initial integer "val" has a value of 1. After we pass it to a method by reference, the method is not only changing the local parameter ("i" in our case), but also the calling integer "val". So, after passing it by reference, it's value won't be 1 anymore, but 45, because modifying the reference parameter value it also means modifying the original variable. In your case, i assume that this would translate like this: if you pass the player as a value object, whatever action you apply on it will only be reflected on the local player, the value one (a copy of the original player). When you pass the player object by reference, whatever action you apply on the reference object will ALSO be reflected in the original player object.
    I hope this helps.

    There is no way to not create another object when you call a method with that object as parameter. That's the whole point of adding parameters to the method. In your case, you can simply ignore the local player objects and use them as means to modify the original player objects.
  5. In Topic: passing an object as a parameter creates a copy of it, how to avoid it

    Posted 17 Dec 2014

    i'm not sure if this is the answer, but this might help you.

My Information

Member Title:
They're watching you, Neo!
Age:
30 years old
Birthday:
August 22, 1984
Gender:
Location:
Bucharest, Romania
Interests:
Music, Astronomy, Programming, Artificial Intelligence, Electronics, SF, pencil drawing and gothic.
Full Name:
Юлия Данилова (Yulia Danilova)
Years Programming:
5
Programming Languages:
C, C++, C#, VB.NET

Contact Information

E-mail:
Click here to e-mail me
Website URL:
Website URL  http://www.rusoaica.com
Yahoo:
Yahoo  rusoaica3@yahoo.com
Facebook:
https://www.facebook.com/yulia.danilova.587

Comments

Page 1 of 1
  1. Photo

    rusoaica Icon

    19 Oct 2014 - 04:35
    Binars: you're welcome!
  2. Photo

    binars Icon

    19 Oct 2014 - 04:11
    Thank you for the nice welcome!
  3. Photo

    rusoaica Icon

    16 Oct 2014 - 12:20
    thanks... i'm a big fan of Star Trek and LCARS ;)
  4. Photo

    dyo Icon

    16 Oct 2014 - 12:16
    Nice website :-)
Page 1 of 1