❔ 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
HimmDawg
HimmDawg2y ago
Well, what do you have so far and what don't you understand about it?
The king of kings
Ok! Let me show you what I got so far. https://paste.mod.gg/ybjlegwnwtnu/1 Do you see where the issue is?
HimmDawg
HimmDawg2y ago
Well you tell me 😄 I thought you were asking about how methods are used here
The king of kings
I am asking about the methods invocation?
SinFluxx
SinFluxx2y ago
What do you understand method invocation to be?
The king of kings
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.
SG97
SG972y ago
it's good to also include the compile errors if there are any
SinFluxx
SinFluxx2y ago
^ 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!
The king of kings
Ok! Yeah! I have 3 errors that are missing some arguments.
SG97
SG972y ago
the actual errors
The king of kings
🤔
SG97
SG972y ago
"missing some arguments" when you can actually point to the exact place they are missing
SinFluxx
SinFluxx2y ago
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?
The king of kings
Well! This is where I am stuck, what arguments should I pass?
SinFluxx
SinFluxx2y ago
What arguments is it telling you to provide? Also, post the error text
SG97
SG972y ago
what does your IDE say you're missing
ero
ero2y ago
(for context, this is faraj, they changed their name)
SG97
SG972y ago
yes
ero
ero2y ago
(not that it's relevant to anything, just thought it might give more detail from previous problems)
The king of kings
Well! The first method invocation requires me to pass an argument for the object param.
SinFluxx
SinFluxx2y ago
Why have you made it require a UserAccount object, when the purpose of that method is to create a UserAccount object?
The king of kings
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
SinFluxx
SinFluxx2y ago
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?
Pobiega
Pobiega2y ago
MakeUserAccount should not be doing any json serialization, thats the job of SaveUserAccount you are mixing up responsibilities here
The king of kings
So that means the variable jsonString is the one who holds the value not the variable account 🤔
Pobiega
Pobiega2y ago
make should ONLY create and return a new user account.
The king of kings
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 🤦
Pobiega
Pobiega2y ago
There are a lot of words in that sentence that don't strictly need to be there
SinFluxx
SinFluxx2y ago
I'm creating a user account after the serialization
Also listen to what Pobiega's said, you shouldn't be serializing anything in this method
Pobiega
Pobiega2y ago
Does it make sense for a method that is called "MakeUserAccount" to require an account being passed in?
The king of kings
What, I didn't know that. I mean I knew that we write data to a text file using WriteAll method by saving user account
Pobiega
Pobiega2y ago
Just think about it for a while
The king of kings
No, actually not at all. I'm gonna modify my logic
Accord
Accord2y ago
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.
The king of kings
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();?
Pobiega
Pobiega2y ago
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 void
The king of kings
Ok! 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.
SinFluxx
SinFluxx2y ago
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
The king of kings
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
SinFluxx
SinFluxx2y ago
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 king of kings
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.
SinFluxx
SinFluxx2y ago
Yes you just need to make sure you're sending the correct parameters and actually using them I'm afraid I don't, sorry
The king of kings
Ok! Even for 10 minutes?
SinFluxx
SinFluxx2y ago
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?
The king of kings
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?
SinFluxx
SinFluxx2y ago
I mean it is a UserAccount object you're returning, yes
The king of kings
Exactly
SinFluxx
SinFluxx2y ago
So then what should you pass into your SaveUserAccount function?
The king of kings
Nothing then the object that was created as a user account, right?
SinFluxx
SinFluxx2y ago
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
The king of kings
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
SinFluxx
SinFluxx2y ago
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?
The king of kings
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 🤷🏻‍♂️
SinFluxx
SinFluxx2y ago
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
The king of kings
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
Pobiega
Pobiega2y ago
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
The king of kings
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?
Pobiega
Pobiega2y ago
var account = MakeUserAccount();
SaveUserAccount(account);
var account = MakeUserAccount();
SaveUserAccount(account);
The king of kings
I know that the MakeUserAccount method should be returning a user account object, but here where I'm stuck 🤔 Ok 🙆🏻‍♂️
Pobiega
Pobiega2y ago
I think you've misunderstood or missed some vital core thing about the whole idea of passing variables between methods
The king of kings
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 😆
Pobiega
Pobiega2y ago
What does that even mean? what does "return and pass a parameter" mean? what does "use the parameter as an argument" mean?
The king of kings
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.
Pobiega
Pobiega2y ago
?
The king of kings
I feel pretty dam 🤦‍♂️ sometimes tbh particularly in such logic
Pobiega
Pobiega2y ago
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.
The king of kings
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.
Pobiega
Pobiega2y ago
because I always thought how would I be able use the user account object in the SaveUserAccount method
well 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?
The king of kings
I don't see any other way to access it except for invoking it from what I know
Pobiega
Pobiega2y ago
WHAT DOES THAT MEAN?
The king of kings
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
MakeUserAccount
MakeUserAccount
invocation. Second step is that we are passing the variable
account
account
that stores our user account object in order to save it in our
SaveUserAccount
SaveUserAccount
method.
Pobiega
Pobiega2y ago
okay, yes that actually makes sense for once. its a description of the code snippet I posted above
The king of kings
Yep, based on reviewing what you and the other guy wrote I did understood how the logic should be.
Pobiega
Pobiega2y ago
but do you understand why?
The king of kings
Of course because we want to create a user account and serialize the user account into json format, correct?
The king of kings
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?
SinFluxx
SinFluxx2y ago
Why can't you? Everything in c# is an object
The king of kings
Ok! You're right. I had to fix the issue at the end bro.
The king of kings
Look
The king of kings
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 lol
Accord
Accord2y ago
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.

Did you find this page helpful?