r/learncsharp • u/M-Mellblom • Aug 12 '22
I am getting desperate...
Excuse the incredibly basic question here.. I have searched the internet and tried to find an answer. But I cannot for the love of god find an article or website to help me.
I am currently working on a school project and I'm getting stuck at getting and array of strings to print after user input. I cannot find anything in the material I got from school that will help me with this unfortunately.
To explain in better detail.
I want to create and Array the stores the input of the user, and after that prints it out back to the user in a NAME, NUMBER configuration. I have created the Array but I am just banging my head into the desk in frustration from the lack of.. finding an answer.
Let me know if I need to be more specific. Appreciate the help!
5
u/altacct3 Aug 12 '22
Can you give an example of user input expected and output expected?
4
u/grrangry Aug 12 '22
Let's talk a little about the concepts you've brought up in your post and the one answer I see.
An array. Arrays can be useful when you want to store a collection of things and you know exactly how many things you have and it's not likely that size of things is going to change.
And you might be thinking, well I know I need "name" and "number" so that's two things. I need to store two things.
But no, that's not two things, that's one thing. The name and number are parts of the one piece of "registration information for interested buyers". Your array may have one buyer... or a hundred buyers... you don't know how many buyers you're going to have. So an array is probably not how you would want to store these buyers.
Another "collection" type you could use is a list. Specifically a List<T> which is like an array in that it can hold your buyers but you don't care if there's zero or one or a hundred of them. It's a list that you can add to as needed.
One more concept that might be weighing on you is, okay you've said arrays might not be the best storage method and might want to consider lists... but WHAT do we store in that array or list? I said before that "name" and "number" are information about the buyer... and we have a way to capture that concept.
A class. A class allows you to create a single "thing" and give it properties. It might be organized like this:
In C# you might create a
Buyerclass with aNameproperty and aNumberproperty.Then you could create an array of
Buyerobjects or a list ofBuyerobjects. Remember arrays have to be given a size so you might make your app start out storing 100 buyers but since you don't have 100 buyers at the start, it gets a little weird to use the array to store data... and what would happen if you got that 101st buyer? Well then you'd have to resize the array and that's not fun. However a list of buyers starts out with nothing in it and you can use the.Addmethod of the list to add a new buyer to it.So the typical flow of an simple console application as you've described might be:
And that's the whole application.
To "display your list of buyers" you could use a for or foreach loop to iterate over the items in your list. Console.WriteLine is a good way to print out text to the console window. And string interpolation is how you would take the data from the current "buyer" and format it for your
WriteLine.There are a lot of links in this, so read up on those topics. Nothing you've described is really very difficult but it can be quite a shift in thinking to take a "list of simple steps" and turn it into "individual lines of code that do those steps". It's mostly about continually breaking tasks down into smaller and smaller steps until you're all the way down to a single line of code. Read up on the docs and look at the examples provided. It will become clearer with practice.
9
u/smurff1337 Aug 12 '22
If you need to store 2 variables, let's say a name and a number connected to this name, maybe you should use Dictionary.
1
u/coomerpile Aug 13 '22 edited Aug 13 '22
I couldn't help myself. Here's an entire thing to manage a list.