did you try
public void give (People person, decimal amount)
{
person.Cash += amount; //public accessor
this.cash -= amount; //this private variable
}
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.
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.
--
Laslod (02-07-2013)
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?
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.
Functionally there is no difference between "this.cash" and "this.Cash", butCode:{ //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; }
'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.
Maybe that's what you want?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 !!! }
--------------------
something different (maybe not related: just throwing it out there)
^^Can also be done using out parameters (pass by reference vs pass by value).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 }
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.
--
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.
--
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.
--