❔ Need help with user account
I'm creating user account using methods, but it seems a bit unclear to me how to invoke and use these methods!
82 Replies
Well, what do you have so far and what don't you understand about it?
Ok! Let me show you what I got so far.
https://paste.mod.gg/ybjlegwnwtnu/1
Do you see where the issue is?
Well you tell me 😄 I thought you were asking about how methods are used here
I am asking about the methods invocation?
What do you understand method invocation to be?
Well! Generally, we use methods to avoid repeated code or when we have too much code in our main program. So basically we move code block inside methods and then we call the those methods in our main program to return the value that we need.
it's good to also include the compile errors if there are any
^ what they said, as you seem to be invoking methods plenty in the code you've provided so not sure what you're stuck on!
Ok! Yeah! I have 3 errors that are missing some arguments.
the actual errors
🤔
"missing some arguments" when you can actually point to the exact place they are missing
the full error text is what's needed, as that shows the full detail for us to understand. Have you actually tried providing the arguments it says are missing?
Well! This is where I am stuck, what arguments should I pass?
What arguments is it telling you to provide? Also, post the error text
what does your IDE say you're missing
(for context, this is faraj, they changed their name)
yes
(not that it's relevant to anything, just thought it might give more detail from previous problems)
Well! The first method invocation requires me to pass an argument for the object param.
Why have you made it require a UserAccount object, when the purpose of that method is to create a UserAccount object?
Since I'm creating user account and I should be returning the value back to the method invocation.
That's a good question
Because one thing ☝️
Since I'm taking user inputs + serializing those inputs into objects + storing the value of serialized objects into the variable
jsonString
I don't think that has anything to do with why you've made it so that a UserAccount object is required to be passed in? You also don't seem to be using jsonString anywhere?
MakeUserAccount
should not be doing any json serialization, thats the job of SaveUserAccount
you are mixing up responsibilities hereSo that means the
variable jsonString
is the one who holds the value not the variable account
🤔make should ONLY create and return a new user account.
You are right actually bro. You opened a new door for me where didn't realize it before. Why the hell my method is UserAccount type, where I'm creating a user account after the serialization 🤦
There are a lot of words in that sentence that don't strictly need to be there
I'm creating a user account after the serializationAlso listen to what Pobiega's said, you shouldn't be serializing anything in this method
Does it make sense for a method that is called "MakeUserAccount" to require an account being passed in?
What, I didn't know that. I mean I knew that we write data to a text file using
WriteAll
method by saving user accountJust think about it for a while
No, actually not at all.
I'm gonna modify my logic
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.I got question for you guys! I have merely basic understanding of methods and try to learn why we would need to use parameters & arguments, therefor I wanna ask you. What's the different between declaring a variable as parameter like in my example and declaring it only inside the body like this
string jsonString = JsonSerializer.Serialize();
?parameters mean the value is passed in by the caller of the method
declaring it locally means the method itself has control of the value
DO NOT use a parameter like you do in the code sample above - ignoring the passed value and simply using it as a declared variable.
Imho, this method signature makes no sense
its returning a string (why?) and taking a string in
imho, it should take a
UserAccount
parameter in and return nothing, aka voidOk! I thought about what you mentioned above. I almost got the idea regarding using parameters or not at least in my code. Because the method should have the control, since I'm serializing inside the method body.
You're right, it does make sense to use as a string data type, simply because I'm taking user inputs + the inputs are strings.
string jsonString = JsonSerializer.Serialize();
but returning string also doesn't make any sense, because basically I'm serializing objects to serialized format, why should they be strings ❌ again which's false.They were saying it should NOT take a string in as a parameter, as you shouldn't have a string of all the user account information available to pass through to it, you should have a UserAccount object available to pass to it
What do you mean by taking a UserAccount as a parameter, you just mentioned that I shouldn't have a parameter?
Thank you for showing up buddy
No they said don't use a parameter in the way you were using it, i.e., you were passing a parameter in but then completely ignoring it
Using a parameter is fine, and necessary
The reason why I failed in applying those methods to create a user account + serialize the objects & write all/save to a text file + loading/reading from a text file is because I'm bad at connecting those methods with each other or making them related to each other, that's why I was using parameters and returning stuff, other wise how the do I use values in a method from another method for ex: creating a user account in a method and serializing the user account from a separated method? These stuff confuse me most of the time.
Aha! Ok! I see what you're saying.
Do you have time for a short call, that will mean a lot to me really.
Yes you just need to make sure you're sending the correct parameters and actually using them
I'm afraid I don't, sorry
Ok! Even for 10 minutes?
Think about your program flow again, you want to:
1) Make an account
2) Save that account
So when you make the account in step 1, what type of object is it you return?
It's totally alright bro. Texting works well as well.
Shouldn't be user account?
Objects don't have a type, what do you mean?
Objects are instantiated from a class, they don't use any type like string, int bool and etc.. but properties & fields do use data type, right?
I mean it is a UserAccount object you're returning, yes
Exactly
So then what should you pass into your SaveUserAccount function?
Nothing then the object that was created as a user account, right?
That's right, when you call MakeUserAccount you should be storing the UserAccount object that is created in a variable, so that you can use it to the save the user account
I mean when we serialize, we usually pass the object name as a parameter to serialize it to a json formatted, right?
Aha
Ok
Look what I did
Hmm you seem to have changed your MakeUserAccount function to return a string instead of a UserAccount?
And you are no longer passing a parameter into your SaveUserAccount function, how will it know what account to save?
Ok
I have created a user account in a different project, but without using a method, so I thought and I could it in this way.
You're right, it won't.
So this is my understanding of parameters too, is that you connect two methods by passing parameters and arguments, other wise how would a method know anything about another method 🤷🏻♂️
Hmm well, that's the right sort of idea, but methods don't need to know about other methods, SaveUserAccount doesn't/shouldn't know anything about MakeUserAccount. All SaveUserAccount needs to know is the UserAccount you want to save
That UserAccount could have come from anywhere, as far as SaveUserAccount is concerned
Aha! Ok! You're right. It makes sense what you mentioned. This is what I meant too, but I definition wasn't very clear. You're awesome at explaining things to me btw 😉
Ok
Let me practice and apply based on what you mentioned
I'll show you my logic in a minute
After all we've written about parameters I don't understand how you can end up with the above code.
It's like you didnt read a single thing we wrote
I'm bad at logic, so this is why I'm practicing.
Could you show me a simple example as an approach to what you just described?
I know that the
MakeUserAccount
method should be returning a user account object, but here where I'm stuck 🤔
Ok 🙆🏻♂️I think you've misunderstood or missed some vital core thing about the whole idea of passing variables between methods
Yeah! I surly do.
Damn, the whole time I thought I needed to return and pass a parameter in the
MakeUserAccount
as a user account and use the parameter as an argument, which doesn't make any sense 😆What does that even mean?
what does "return and pass a parameter" mean?
what does "use the parameter as an argument" mean?
Failing in this logic with params means definitely I'm bad at using params and i should definitely practice them more, this is pretty clear evidence 🙆🏻♂️
Never mind, my thinking of the logic was totally wrong, because I always thought how would I be able use the user account object in the
SaveUserAccount
method 🙆🏻♂️ it seems like all I needed is to just invoke it.?
I feel pretty dam 🤦♂️ sometimes tbh particularly in such logic
I find it very hard to follow your line of thought. A lot of the time when you write its mostly a stream of semi-relevant words in a nonsensical order.
I've been reading this docs to learn about method and still suck in a simple logic like this https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
Methods - C# Programming Guide
A method in C# is a code block that contains a series of statements. A program runs the statements by calling the method and specifying arguments.
because I always thought how would I be able use the user account object in the SaveUserAccount methodwell yes, your SaveUserAccount method does need access to a user account. how else can it save it?
it seems like all I needed is to just invoke it.What does this mean to you? what does "just invoke it" mean here?
I don't see any other way to access it except for invoking it from what I know
WHAT DOES THAT MEAN?
Basically we wrote out logics inside two distinct methods and we invoked the two methods inside our main program. First step is that we are storing the user account object in a variable aka using the invocation. Second step is that we are passing the variable that stores our user account object in order to save it in our method.
okay, yes
that actually makes sense for once.
its a description of the code snippet I posted above
Yep, based on reviewing what you and the other guy wrote I did understood how the logic should be.
but do you understand why?
Of course because we want to create a user account and serialize the user account into json format, correct?
I feel confused about what you mentioned here 🤔 I can not return any object inside the
MakeUserAccount
method, it's an object not a regular variable?Why can't you?
Everything in c# is an object
Ok! You're right. I had to fix the issue at the end bro.
Look
The method signature should be object type, since we're returning a
UserAccount object
and not a string
I should've payed attention on the return type from the beginning lolWas 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.