Hello, I am trying to make BlackJack game but I have one problem where I want to call a method to generate random card and then add it to existing integer (Player Number or Dealer Number) but for some reason, method reset existing number on integer (Player Number or Dealer Number) in the end of method.
Value types (including all the primitives, int float bool string and so on) are passed by value, which is to say that when you call RandomCard(x, y), RandomCard’s “person” is a copy of x, but does not refer to it. So when you change the value of “person”, that change is not persisting anywhere - you’re modifying person, but not modifying x.
You can use the “ref” keyword, and it will modify the original variable passed into it:
public void RandomCard(ref int person, int ace) {
....
}
//when calling it
RandomCard(ref x, y);