❔ Bug
My code isn't doing what it's supposed to do.. i want to make more than 1 user in my ATM system to each of their own balance and all. and i tried doing that with an idea i had but its not working properly. for example when i enter a password for user 2 it access it as user 1 instead of 2
code is here:https://paste.mod.gg/cjikmtzdtzxw/1
BlazeBin - cjikmtzdtzxw
A tool for sharing your source code with the world!
234 Replies
u mean it access it as user 0?
yeah instead of the user that acc has the password
in this case its user 1 since the array is starting from 0
try debugging $debug
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
debugger shows nothing since there are no errors
thats not the point
u arent suppose to look for compiler errors
u are suppose to pause the program at certain lines to see the values of the variables
the link pretty much explains what debugging is
i cant see anything on my screen lmao
what do u mean?
like the value of the variable userC
where does it appear
the link should tell u where does it appear
What IDE are you using?
ohh wait let me try
visual studio
Okay keep the breakpoint on the line next to the line that has the variable whose value you wanna see
code or community?
oh we have to ask that question too these days
anyway i found a bug
it reaches value 0 and then continues
im trying to check if the pass already exists and if it doesnt then it goes back to the beginning
u are only checking the password of the current UserC user
mm so i have to check all
yes
okay but that doesnt fix the bigger problem im having rn :d thank you for pointing it out tho
for some reason. userC gets to value 0
and just exits the loop
it shouldnt even access that IF statement if the password doesnt match
look
put a breakpoint at
break
ok
i
isnt assigned to userC
yet when ur paused on itoh
the program should pause and the line should glow
yellow
yes it did
it says i is 0 and userC is 0
and it just pauses there
which means that the passcode u entered is user 0's passcode right?
yes
is there smth im supposed to do after it glows yellow
to continue the code or smth
the link should tell you, its F10
F10 to go to the next line
F5 to continue executing the program until the next breakpoint is hit
yeah it does access it as user 0 eventhough i want user 1
then you have to question urself
why .PassCheck returned true
it should only return true if it matches the pasword variable in the ATM class
i might be mistaken but.. shouldnt the variable passcode in ATM class have different values depending on which object im accessing ?
i entered passcode 1 for user 0
but when i debugged, its passcode is 3
you can see in ur debugger user 0's passcode
why is that happening
because when the program asks u to enter ur
Please enter your desired passcode:
for user 3, for example
u are actually at user 0
if u breakpoint at this messagedid i forget to update it
you should see at what user you're on
i should have incremented the userC right
yeah
i think i fixed it
i just made it that way
that should fix it and it doesnt matter if its incremented the last time since ill zero it
please do pass checking method in the ATM class, you're code is really a mess
oh like make the method that checks for the password in the atm class ? but i alrdy did
i mean one that checks if pass exists in the array
ohh okay
see how readable it is
yeah much better
thank you
wait let me try
and u just replace
with a single line
very readable, right?
i personally would initialize the ATM Array in the ATM class itself
as a static field/property
yes
idk why i never thought of that, just thought i have to initialize it in the other class im using it in
can u show me an example
and then i can just call it with the object ill make inside the other class right ?
and id define a method called
AddUser
in the class too
which would add a user to this arrayokay i will try to make the code more clean
i dont know if you learned
List<T>
yetno i haven't
also you dont really have to define a variable called
int userC
you can just ATM currentUser = null;
also something doesnt make sense
are u suppose to have 3 users or 3 ATMs?well 3 users and for each user they have their own object so the balance is different and all
think of it as 3 different accounts at a bank
so a single ATM?
thats what i was going for
yeah it should be a single atm but different objects of that atm
well you should have a class called Bank atleast
the bank is the one who has the user's accounts and their pass
and the atm holds the balance and deposit and all ?
yes
okie hold on
basically u will have a bank property/field in class atm that will point to the bank that you specify
so like that
doesnt make sense to create a new bank for each atm
yeah it should be the opposite
u want many atms for a single bank
yeah
so i can keep the array of ATM inside the bank ?
no, bank should not know anything about ATMs
do you have any tips for making user accounts ?
i thought of creating an array and storing their names/passwords
you can make a class called UserAccount yeah
it would make sense for the Bank class to have an array of UserAccounts
the Bank class should have methods like
CreateAccount
, GetAccount
, HasAccount
ill keep the password in the ATM class then
doesnt make sense
a UserAccount has the password, not the ATM
should the currentUser be an integer ?
ill try to access it that way
u can do that but better practice is to get the instance of the user directly
im not sure how to do that
let me try
by instance of the user what do u mean
UserAccount currentUser = null;
passcode should be stored in UserAccount, not bank
oh so another class
solely for the useracc
yes, that class will have the passcode of the user, and their balance
why did u decide to null the currentUser
because at the beginning of the program, you havent selected a user yet, right?
yes
+ you have not created any user yet
thats why
im not sure how to efficiently use the userACcount thing
UserAccount currentUser = null;
ass u can see the add method needs somewhat of an index to access the list of users
currentUser should be in Program.cs
ohh
or ATM Atleast
you'd want the ATM to interact with the user's account
okay ill try to think of some ways
it doesnt make sense to have an array of string in UserAccount
a UserAccount cannot have multiple passcodes
i get waht you mean now
userAcc is like 1 acc for each user
a class is basically a group of other classes
you'd want to group a user's passcode, and their balance into a single class
think of how you can convert real world materials into classes
like
oh, an ATM is a machine, that interacts with the bank
a user account has a passcode, and a balance, so i will make a user account class, with passcode and balance variables stored inside it
ill try my best to interpret real world activities into programming
programming is a really great thing
was that the way u practiced ?
yes, i think so
it sounds like it worked :d
how do i "select" a user with CurrentUser ?
i have it in ATM how do i access it
oh i can just make it public
so after that how do i make it a current user that is for example entering his username and password
is this a good idea
good practice is to make fields private, and properties public
how will the ATM know which bank to interact with?
ATM is basically a remote control for bank
i only made the passcode property private
welll there is only 1 bank but multiple userAccounts right ?
they are called fields, not properties
then what is the property
like this
$propvsfield
Why use properties over a public field?
- Properties can individually specify access modifiers for get and set
- With Visual Studio, you can type prop, press tab and it will auto complete for you
- XAML requires properties for bindings
- Field exposure is really only done in readonly structs
- Using properties allows for future changes to the getter or setter without breaking your API/external programs (binary compatibility)
Example of an auto property:
Example of a property with backing field:
public string Name { get; set; } is a property
private string _name; is a field
what the property does is it takes the stored value from a field
they are actually methods under the hood
ohh
i still havent studied them
no wonder i got no clue what it is
yes
only 1 bank, but multiple atms
yes
atm needs to connect to the bank to extract information, like the user's passcode, and their balance
yes i did that
how? show me
well i made it interact with the userAccount
directly
well that works but, what if you withdraw money from the user account?
you'd also need to subtract from the bank's total money
yeah
unless you don't care about how many money the bank currently has
well you can get a reference of the bank from the user account anyway
hold on let me trry something
UserAccount.userName
is badwhy
its not suppose to be static
right
yeah i fixed it
instead i made an instance in the program.cs
show screenshot
okay
sorry hold on
this is program.cs
this is atm
yk what ill just link it
BlazeBin - gxawqjcfgtwz
A tool for sharing your source code with the world!
ik the logic in the program.cs makes no sense
ill edit it rn
So something really weird happens
The function is executed without calculating the parameter
u can ping me
share ur updated code
and idk what function are u talking about
Sorry my Internet is playing games with me
I'll send it when I can
BlazeBin - pzoeomvsquws
A tool for sharing your source code with the world!
@TheRanger here
The part where I register a new acc. The username field is bugged
hmm?
what do u mean?
any reason ur doing
atm.currentUser = new UserAccount();
?
before u select an account from the bank or even create one?
creating a new account before accessing the atm doesn't make sense
if you use the debugger, you would see your values
for example
when you do atm.currentUser = new UserAccount();
all of the fields are by its default values
for example passcode is null, username is null, balance is 0
now, when ur program calls if (atm.NameCheck(Console.ReadLine()))
where the NameCheck method calls this piece of code
currentUser's userName is null
and the program will see it like this when you reach this line
assuming you input 1234
after ur program asks u to Please enter your username:
Oh
So basically this is like creating an account and I'm doing it before I even have to
That's what's messing it up
I thought ab it but didn't pay it much attention
Thank you for pointing it out I'll try to create that instance when I access the account instead
no?
After I access the atm
you should only create an instance when the atm asks u to create an account
Oh
So wait I must be missing something. What exactly does the line mean then
what line
Like what am I doing behind the scenes I thought I'm just creating an object from thr class UserAccount
And I create it to access it later on in the code right ?
right now you created an account that has no username and password
Yes okay
do you know why there are access modifiers like public and private?
So that's what this does. It basically creates a new acc object
I think it's for safety
for private, to prevent the programmer from accessing the private members outside the class
Okay yeah
so for creating an account
you should force the programmer to create an account with a username and password
because right now a programmer can create an account without a username and password
which doesnt make sense
So it has to be declared after writing a username and password ?
this is where
constructor
comes
you do something like new UserAccount("foo","1234");
I haven't learnt constructors yet 😕
I'm just trying to manage with what I've learnt and step by step with every new thing I learn I adjust my code
you should not allow the programmer to write
new UserAccount();
if they tried to, a compiler error would appearWell how can I avoid it. if I need to create a new instance for each acc so each username and password are different for each user
That's my goal
you define that in the UserAccount class
this is a constructor
takes the arguments of username and password, and assigns their values to the following fields
So for now my problem is with creating an instance of UserAccount before the user name and password are sent ?
Why ? I'm confused what you mean
Also sorry i feel like I am taking too much of ur time
like, the program would give you an error before you run it
Ah
telling you something like
the constructor requires 2 arguments
Okay I will continue learning and when I learn smth new ill adjust the code
From what I understood the reason my code isn't working is because of the way I declared the instance before I should have
from the latest code you posted
you created a new UserAccount, you never assign username and password to that account
Wait
then the atm logged into that account, which has no username and password
When I create a new user account I'm creating it with what's stored inside it ?
Not create it THEN store smth inside it ?
?
u created an account and you didnt store anything inside it
no username, no password
then the atm logged into that account, which makes no sense
then your program asks to enter a username
So I store first then create an instance which will have what was stored inside it saved
So the object of the class is basically taking it with what's inside it and creating for example a new slice with that information saved and not touched
then the program to check if username exists by comparing the username you entered WITH the current logged in user's username, which is null
Ah
which makes no sense at all
you are not suppose to login to any username yet before the program asks u to enter the username and password
So object created with what was stored in the class then the information in the class I stored are deleted waiting for a new entry? Is what I understood
Fair enough
Thank you bro
no
what class, the useraccount?
Yeah
what is stored in the class?
Like for example I enter username and password then create an instance of that class with what's stored
+ atm's currentUser should be private
Then the username and password in the class not the instance. Is nulled again
Then the username and password in the class
that doesnt make sense
why would the username and password get nulled?
each instance of each account has their own username and passwordOkay I got it
Thank you again
new UserAccount();
basically creates an instance of that class and stores it somewhere in the memory
atm.currentUser = new UserAccount();
creates an instance of that class and stores it somewhere in the memory and lets atm.currentUser
point to that instance
you can create many accounts, and let atm's currentUser only point to one of themokayy i got it
makes sense yeha
in other words,
new
here in ur case means creating a new accounteven though i created an instance it still says its null
now it does this
something is wrong with the nameCheck method
atm.cs line 21
it helps if you showed ur updated code
if (currentUser.userName == name) return true;
that yeah
okayynot enough context, have to see ur whole code
if that is atm.cs line 21
the error here means ur trying to access a member of a variable that is not pointing to an instance
in your case, currentUser isn't pointing to an instance of a
UserAccount
yeah its null
what is even the point of that line?
yeah its currently pointing to nothing and im trying to figure out how to make it point to the Instance i created in Program.cs
i wanted to create a method to check if that username exists in any of the instances
im trying to figure out how to make it point to the Instance i created in Program.cs
but why?i think i did that wrong
yeah, but do u know how to do that?
i thought of passing the instance of the acc i created to the method and access the username of THAT instance
but that will only check 1 instance i think
yes, what if there is 100,000 accounts
i guess i need to iterate somehow
how are u gonna check every account
correct
okay instead, ill create an array of instances
UserAccount[] User = new UserAccount[];
correct
do u know where to store that?
this is going to be in Program.cs ?
doesnt make sense
choose another class
then if im basically creating user accounts i guess it should be in bank
yes
but do i create an instance of bank in program.cs ? or access it from the ATM
well if you need to initialize like 10 ATMs
you need to tell them which bank it needs to access
is that a good idea
i wouldnt recommend it if it wasn't
well since i created an array now
how am i gonna access each index
right now ur atm creates its own bank, so if you created 10 atm, u will have 10 banks
which doesnt make sense
lmao
yeah it doesnt
what did u try so far?
i had it for those
i wanted to create a CurrentUser iterator that depending on the username it chooses a number like 0 1 or 2
what does the error message say?
its there bczi removed the instance of the bank in ATM.cs
uh no?
please read the error message
what you said doesnt make sense
do u know what instance means?
Bank bank = new Bank(); thats an instance right ?
new Bank(); creates a new instance yes
variable
bank
points to that instance
many variables can point to the same instanceohh okay
got it
say in real life
you have 3 nick names, and a real name
a,b,c, and mazen
when someone says
a
, they are refering to you, right?yea
or when someone says
b
, they are refering to you, right?yes
same thing with the variables
i see
you can have an empty field
Bank bank;
for now its null
it doesnt refer to any bank yetoh right
yeah that works too
okay i have to go sleep now i will continue this later
thank you
Tomorrow ill try to fix those bugs i have
ur gonna make it refer to the single instance of the bank that u created
same with the other atms, they will all refer to the same bank
in program.cs for example
i just need to know how to refer to one only
oh just like that ?
yes
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.