Results 1 to 8 of 8
  1. #1
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy

    How do you make two instances communicate with echotor through their same class?

    Let's say I have a people class. I'd have a cash property and a name property in that people class which when it gets initialized I can set the amount of cash and the name I want.
    Let's say I want to make a give method where it takes cash from the people object and gives it to the other people object and takes it away from the people object that gave it. Now I know I can just put it outside of the class and let (x) object give to (y) object AFTER they get initialized, but that's not what I want.
    I want a method that can take variables and it before I actually initialized the object so after I do initialize it all I gotta do is fill in the parameters.

    I know this might be confusing so I'd do some examples to better elaborate what i'm saying.

    So lets say my people class is like this:

    class People
    {
    private decimal cash;
    private string name = "";
    public string Name
    {
    get { return name; }
    set { name = value; }
    }
    public decimal Cash
    {
    get { return cash; }
    set { cash = value; }
    }
    }

    Is there a way to set in that same class for instance 1 of the class to give cash to instance 2 of the class?

    I tried making another class named bet and putting a method in there. But I get the same problem as when I try putting it in the people class.
    I can't do public void give (People person, decimal amount)
    {
    person.cash += amount
    person.cash -= amount
    }
    Because i'm working with the same variable and it would just give the amount and take the amount for the SAME object.

    Do I HAVE to make one method to give and one to take? How would that work if I want it to be dynamic and want only methods to call and not manually have to + and - amounts from the objects I initialize in the form?

    I know this kind of doesn't make sense so if you have a question on what I actually mean just ask.

  2. #2
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    did you try

    public void give (People person, decimal amount)
    {
    person.Cash += amount; //public accessor
    this.cash -= amount; //this private variable
    }
    Last edited by abuckau907; 02-07-2013 at 11:34 PM. Reason: ;;'s
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  3. The Following User Says Thank You to abuckau907 For This Useful Post:

    Laslod (02-07-2013)

  4. #3
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    Thanks for helping! But I already went to another website and they told me to use this
    public void givecash(People target, decimal amount)
    {
    cash -= amount;
    target.Cash += amount;
    }

    Whitch makes me wonder , how much use does using the "this" keyword have if your field and argument variables aren't the same name?
    I mean in this situation using this.cash is the exact same as just using cash isn't it?

  5. #4
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    You don't have to use 'this' - it's optional: you use it when a parameter name is the same/similar to a class variable.
    (you have to clearly understand 'definition of class' vs 'object of a class', then learn about 'this' keyword)

    Because the names are 'cash' and 'Cash' I used 'this' to make the code more clear. 'This' = "local variable of current class"(kind of)

    yes, they're exactly the same.
    Code:
    {
    //website
    cash -= amount;
    target.Cash += amount;
    
    //me
    this.cash -= amount;  // erase "this." , but be careful using names that only differ by lEtTeR CaSE
    person.Cash += amount; 
    }
    Functionally there is no difference between "this.cash" and "this.Cash", but
    'cash' directly sets the local variable
    'Cash' calls the set function to set the local variable.
    The difference is slight, and will probably get optimized away by compiler, but it's worth a mention maybe. Just adding one extra function call when you don't have to by using Cash instead of cash.

    edit:
    "I want a method that can take variables and it before I actually initialized the object so after I do initialize it all I gotta do is fill in the parameters."
    Not sure what you mean? You need the 'new' keyword ? Not sure what you're trying to do with 'instantiate' (?)
    ------------------
    Code:
    public void givecash(People target, decimal amount)
    {
    cash -= amount;
    //create new People object
    target = new People(); // new keyword
    target.Cash += amount;
    }
    ?

    but now p1 doesn't refer to the new object!
    And you have no way to return the 'new object' because the function is void.
    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                Person p1 = new Person();
                Person p2 = new Person();
                p1.Cash = 1;
                p2.Cash = 100000;
                p2.GiveCash(p1, 9999);
                //p1 still only has $1 !!!
            }
    Maybe that's what you want?
    --------------------
    something different (maybe not related: just throwing it out there)

    Code:
     public Person GiveCash(Person theFriend, Single amount) // not void
            {
                _cash -= amount;
    
                theFriend = new Person();
                theFriend._cash += amount;
                return theFriend;
            }
    ..
            private void Form1_Load(object sender, EventArgs e)
            {
                Person p1 = new Person();
                Person p2 = new Person();
                p1.Cash = 1;
                p2.Cash = 100000;
                p1 = p2.GiveCash(p1, 9999); //assignment: has return value. is awkward.
                //p1.cash = 10k now
            }
    ^^Can also be done using out parameters (pass by reference vs pass by value).

    Not quite sure what you're trying to do.
    Last edited by abuckau907; 02-08-2013 at 03:42 AM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  6. #5
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    Quote Originally Posted by abuckau907 View Post
    You don't have to use 'this' - it's optional: you use it when a parameter name is the same/similar to a class variable.
    (you have to clearly understand 'definition of class' vs 'object of a class', then learn about 'this' keyword)

    Because the names are 'cash' and 'Cash' I used 'this' to make the code more clear. 'This' = "local variable of current class"(kind of)

    yes, they're exactly the same.
    Code:
    {
    //website
    cash -= amount;
    target.Cash += amount;
    
    //me
    this.cash -= amount;  // erase "this." , but be careful using names that only differ by lEtTeR CaSE
    person.Cash += amount; 
    }
    Functionally there is no difference between "this.cash" and "this.Cash", but
    'cash' directly sets the local variable
    'Cash' calls the set function to set the local variable.
    The difference is slight, and will probably get optimized away by compiler, but it's worth a mention maybe. Just adding one extra function call when you don't have to by using Cash instead of cash.

    edit:
    "I want a method that can take variables and it before I actually initialized the object so after I do initialize it all I gotta do is fill in the parameters."
    Not sure what you mean? You need the 'new' keyword ? Not sure what you're trying to do with 'instantiate' (?)
    ------------------
    Code:
    public void givecash(People target, decimal amount)
    {
    cash -= amount;
    //create new People object
    target = new People(); // new keyword
    target.Cash += amount;
    }
    ?

    but now p1 doesn't refer to the new object!
    And you have no way to return the 'new object' because the function is void.
    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                Person p1 = new Person();
                Person p2 = new Person();
                p1.Cash = 1;
                p2.Cash = 100000;
                p2.GiveCash(p1, 9999);
                //p1 still only has $1 !!!
            }
    Maybe that's what you want?
    --------------------
    something different (maybe not related: just throwing it out there)

    Code:
     public Person GiveCash(Person theFriend, Single amount) // not void
            {
                _cash -= amount;
    
                theFriend = new Person();
                theFriend._cash += amount;
                return theFriend;
            }
    ..
            private void Form1_Load(object sender, EventArgs e)
            {
                Person p1 = new Person();
                Person p2 = new Person();
                p1.Cash = 1;
                p2.Cash = 100000;
                p1 = p2.GiveCash(p1, 9999); //assignment: has return value. is awkward.
                //p1.cash = 10k now
            }
    ^^Can also be done using out parameters (pass by reference vs pass by value).

    Not quite sure what you're trying to do.
    I already did it with the code I mentioned in my post. But just a little clarity, when you have this:

    class lala
    {
    public int number;
    private int Number
    {
    get { return number; }
    set { number = value; }
    }
    }

    number is a field while Number is a property right?

  7. #6
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    Yes.
    (and I think you accidentally switched the public/private in your 'lala' class)

    (plz don't quote my entire post unless directly needed -- it's longer than needed and takes up space )
    Last edited by abuckau907; 02-09-2013 at 07:11 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  8. #7
    Laslod's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Location
    Zimababueewweeee
    Posts
    275
    Reputation
    10
    Thanks
    15
    My Mood
    Sleepy
    Quote Originally Posted by abuckau907 View Post
    Yes.
    (and I think you accidentally switched the public/private in your 'lala' class)

    (plz don't quote my entire post unless directly needed -- it's longer than needed and takes up space )
    Haha yea I make alot of mistakes when i'm sleepy. As for the quoting thing I quoted it so you could see that I replied to you and I didn't feel like reducing the amount of texts so it could take up less space :P

  9. #8
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    It happens. Next time, prefix your message with "@abuckau907" and I'll know you're talking to me Issue resolved?
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

Similar Threads

  1. [Help] How can you make a .Dll with VB?
    By creto98 in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 9
    Last Post: 03-21-2011, 10:04 PM
  2. how do you make a cross mod with textures
    By 420Stoner420 in forum Combat Arms Mod Discussion
    Replies: 2
    Last Post: 05-19-2010, 08:41 AM
  3. how do you make a trainer and or bypass??
    By 123456789987654321 in forum WarRock - International Hacks
    Replies: 11
    Last Post: 06-04-2007, 12:00 PM
  4. What Are Boxes And How Do You Make Stand Alone Trainers?
    By condor01 in forum WarRock - International Hacks
    Replies: 2
    Last Post: 04-19-2007, 02:17 AM
  5. How'd you make these?
    By Twisty in forum Art & Graphic Design
    Replies: 1
    Last Post: 03-13-2007, 03:27 AM