C
C#3y ago
Squirtle

How do i update a value and + add int values constantly

I have an int value and i wanna add numbers to it and i want it to update. for example if i add 10 to the int i dont want the int to be set as onlly 10 i want it so if i add another 10 it gets to 20 if you know what i mean
40 Replies
Angius
Angius3y ago
Do just that Instead of setting your value to 10, set it to the current value plus 10
Ezlanding
Ezlanding3y ago
So like n +=10?
Squirtle
SquirtleOP3y ago
example int balance int deposit deposit // sets by the user typing it and then balance += deposit so something like that
Angius
Angius3y ago
Yes
Squirtle
SquirtleOP3y ago
but then when i type in a new deposit the balance sets to the current deposit i typed my deposit beforehand doesnt exist
Angius
Angius3y ago
Show $code
MODiX
MODiX3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Squirtle
SquirtleOP3y ago
if you know what i mean
Ezlanding
Ezlanding3y ago
Are you restarting the program every time?
Squirtle
SquirtleOP3y ago
BlazeBin - mgtkujoglbtm
A tool for sharing your source code with the world!
Squirtle
SquirtleOP3y ago
no it is in another language but hope you get it
Angius
Angius3y ago
Your instatdgausding method is said to be returning something But you don't do anything with that return
Squirtle
SquirtleOP3y ago
tbh i dont know what return really does haha
Angius
Angius3y ago
It returns the value from the inside of the method, to the outside of it
Squirtle
SquirtleOP3y ago
ah should i remove the return value and add a void instead?
MODiX
MODiX3y ago
Angius#1586
REPL Result: Success
int Sum(int a, int b)
{
return a + b;
}

int c = Sum(10, 5);
int d = Sum(c, 62);
int e = Sum(c, d);

$"C: {c}\nD: {d}\nE: {e}"
int Sum(int a, int b)
{
return a + b;
}

int c = Sum(10, 5);
int d = Sum(c, 62);
int e = Sum(c, d);

$"C: {c}\nD: {d}\nE: {e}"
Result: string
C: 15
D: 77
E: 92
C: 15
D: 77
E: 92
Compile: 537.812ms | Execution: 39.386ms | React with ❌ to remove this embed.
Ezlanding
Ezlanding3y ago
Assuming saldo is the one you want Keep the saldo Int at the top because you make a new one every time the function is called $scope
MODiX
MODiX3y ago
you probably want $scopes
Ezlanding
Ezlanding3y ago
$scopes
MODiX
MODiX3y ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Angius
Angius3y ago
You should be using this value This method returns your sedlar But since you don't assign the return to anything when the method is called, it's discarded Then your saldo method has an inner variable saldo that exists only within that method
Squirtle
SquirtleOP3y ago
will this work for me ?
Angius
Angius3y ago
Do you need to sum two numbers? Then no It's just an example demonstrating what return does
Squirtle
SquirtleOP3y ago
ah
Ezlanding
Ezlanding3y ago
Is saldo or sedlar the “balance”
Squirtle
SquirtleOP3y ago
saldo is the blaance balance* insättning is withdraw sedlar is money
Angius
Angius3y ago
(would've been a whole lot easier if your code was in English 😛 )
Squirtle
SquirtleOP3y ago
hahahahahahahaha can rewrite it fast in english
Ezlanding
Ezlanding3y ago
I think you should keep saldo at the top because the saldo method creates a new saldo every time it’s called
Angius
Angius3y ago
Yeah
public class Program
{
private static int _balance;

public static void Main()
{

}

private static int TakeDeposit()
{

}

public static void DisplayBalance()
{

}
}
public class Program
{
private static int _balance;

public static void Main()
{

}

private static int TakeDeposit()
{

}

public static void DisplayBalance()
{

}
}
That should be just about the structure
Squirtle
SquirtleOP3y ago
BlazeBin - suwnqmfoflxp
A tool for sharing your source code with the world!
Squirtle
SquirtleOP3y ago
here
Ezlanding
Ezlanding3y ago
@Squirtle try this at the top. Does it work?
namespace switch_övning_5_bankkomat
{
internal class Program
{
static int money;
static int balance;
static void Main(string[] args)
{
bool sant = true;


namespace switch_övning_5_bankkomat
{
internal class Program
{
static int money;
static int balance;
static void Main(string[] args)
{
bool sant = true;


Note that the only change I made was adding a balance at the top (Also make sure to change the
int balance += money
int balance += money
to
balance += money
balance += money
Squirtle
SquirtleOP3y ago
yeah did it doesnt work S; still same issue when it gets the money amount but then when i try to put in a new value it doesnt change it changes to the new only it doesnt add the old one
Angius
Angius3y ago
The balance needs to be in the scope of the class Then, the withdraw/deposit method needs to modify that balance Then, the display method has to display that balance
Ezlanding
Ezlanding3y ago
@Squirtle found the problem. Your code says =+ when it should be +=
Squirtle
SquirtleOP3y ago
lmao that fixed it hahahahahahhahahaah
Ezlanding
Ezlanding3y ago
(On a side note it’s interesting how c# has a + unary operator when I can’t think of a use for it)
MODiX
MODiX3y ago
Angius#1586
REPL Result: Success
var x = -82; +x
var x = -82; +x
Result: int
-82
-82
Compile: 407.660ms | Execution: 22.447ms | React with ❌ to remove this embed.
Angius
Angius3y ago
Huh, you're right
Want results from more Discord servers?
Add your server