C# Console App

Associate
Joined
7 Aug 2008
Posts
302
Hey Lads.

I'm currently programming or trying to program a C# console app that reads in a number of records from a text file line by line and I am trying to store the data into an array.

Now I should be able to type N and it goes to the next record, P and it goes to the previous however I can't find a way to be able to increment the record when the N button is entered or decrement it when the P button is entered. I think once I find out how to do this, the Last and First commands should be simple.

Any ideas what I could use?

Cheers.
 
At the moment I am using a case statement for the different commands but I don't think it's a good way of doing it.

I have a variable which is equal to the array; example = arrText.ToArray then using example - so for example fo display the first record it would be i = 0, then for next i++ however it will only go to the next record and if I press N again it stops as the counter has been reset back to 0 I think.

Don't know what the best way to do it is.
 
What is and isn't working?
It's hard to give you advice without seeing your code.

The general approach doesn't seem too bad. From what you've said it doesn't seem that you need anything too complicated, so a switch statement sounds like a reasonable way to do things.

If you have an array with your data then you will need to maintain a variable with the current row index, and increment or decrement that based on the key presses.
It sounds like you already have something similar to that, so without further info I can't be more helpful.
 
Code:
string[] scores = arrText.ToArray(typeof(string)) as string[];
                

                switch (what)
                {
                    case '1':
                        i = 0;
                        Console.WriteLine(scores[i]);
                        break;
                    case '2':
                        i++;
                        Console.WriteLine(scores[i]);
                        break;
                    case '3':
                        i--;
                        Console.WriteLine(scores[i]);
                        break;
                    case '4':
                        i = scores.Length - 1;
                        Console.WriteLine(scores[i]);
                        break;
                }
                Console.WriteLine();
 
Looks reasonable. You'll need to have this in a loop using Console.ReadKey() to ensure you pick up the various key presses that can occur.
You'll probably want to include some checking that you haven't gone outside the array bounds when incrementing and decrementing though.

In the documentation for Console.ReadKey() there is an example showing a way to do the key checking:
http://msdn.microsoft.com/en-us/library/471w8d85.aspx
 
I think I am moving away from this as I need to be able to perform Sorting on Dates as well.

How would I go about reading the records into a Class or a Structure?

i.e FirstName, Surname etc

Any suggestions on the best way of doing this would be appreciated.

Cheers.
 
I think I need to read the text file into the array and then sort of break this array into a structure;

For Example;

FirstName = [0]
Surname = [1]

That kind of thing?

Just haven't got a clue how I can go about this.
 
What format is the data in your text file? Ideally an example extract (few lines) would be most beneficial, assuming the data's not sensitive. :p
 
I have a few Strings, a DateTime, Char's all seperated by commas.

I have manged to split the data by commas so I have [0] representing one element, [1] representing the other etc and have set these up into some kind of structure.

However, I am now stuck on how to display the records line by line depending on what key has been pressed. I can get the data to display all lines however this is of no use to me.
 
I've actually created a program which stores all info in text files (no use of a formal database...yet). So, I've got 1000s of lines of code which is able to read/write/sort data from and to text files.

In a nutshell, here's how you would do it (I'm using List instead of Array)

0. create a list: List<string> line = new List<string>();
1. read the contents of a text file, line by line (use: ReadLine).
2. you will then store the contents of that line in an element of the list (line):

so the pseudo code would be something like this:

string singleLine = ReadLine (filePath);
line.add(singleLine);

You would iterate through the entire file, until all lines, have been loaded into the line (List).

You now have all the contents of your file, loaded into the list; each separate element of line (list), is a separate line of the file.

4. Now, you need to create a class which is going to hold the details:

Lets call the class CustomerDetails.

5. lets create a list of class Details: List<CustomerDetails> customerDetails = new CustomerDetails();

6. Lets start populating the list with customer details:

for each (string singleLine in line)
{
customerDetails .add(new CustomerDetails(singleLine));
}


7. The constructor of CustomerDetails: I shall leave you to work that out, because it all depends on the format of your text file.

8. you should now have full access to the properties, in each instance of CustomerDetails.

so, to access element2 of the list customerDetails, and return the surname, you would do something like

string theSurname = customerDetails[2].surName;

OR

if you create a GetSurName method in CustomerDetails, you could do:

string theSurname = customerDetails[2].GetSurName();


General Question for any developers
Why do developers like to create programs, using the console?
I find that the console is most user unfriendly interface known to man and can't see how it offers any advantage over creating a simple gui with a text box and a display box (for output).

I'm probably in the minority here, as most developers love user unfriendly environments - why, I do not know, but I'm hoping one of you can answer this. Please give me the advantages of using the console vs a simple GUI (with 1 or 2 text-boxes).
 
Last edited:
However, I am now stuck on how to display the records line by line depending on what key has been pressed. I can get the data to display all lines however this is of no use to me.

If you were using a text box to display the records (or a single property of each record), it would be easy peasy. I'm not expert on console use (I hate the console and believe it should've been banned a decade ago), but if you create a simple GUI (shouldn't take more than 5 minutes), it would be easy.

Actually, thinking about it, you should also be able to display individual properties, in a console (just like a textbox).

How are you displaying "all the records"?
 
Right cheers for that. Will have a proper look through it tomorrow.

The only reason I am using Console is because it is a requirement to. Trust me I would much rather use a GUI over it.

Thanks Again.
 
If you were using a text box to display the records (or a single property of each record), it would be easy peasy. I'm not expert on console use (I hate the console and believe it should've been banned a decade ago), but if you create a simple GUI (shouldn't take more than 5 minutes), it would be easy.

Actually, thinking about it, you should also be able to display individual properties, in a console (just like a textbox).

How are you displaying "all the records"?

Doing it properly you should create all of your business logic in a way that's completely agnostic of how it's being presented to the user.
That way you can reuse the same stuff and display it to the user in multiple ways, potentially console and GUI.
That may be a little overkill for this piece of work though.

Console apps still have their place, if you're doing stuff through batch files for instance a console app is a natural place to do things as it's a simple executable and doesn't have any external user interfaces to display other than the already open console window.
Similarly we use a console app at work to host our WCF services when we want to run them locally. That sort of stuff has no place in a GUI and a console app is a handy way of doing things rather than having to deploy a windows service.

GUIs are good for lots of things, but there are also many other things that they're not at all good for.
 
That way you can reuse the same stuff and display it to the user in multiple ways, potentially console and GUI.

That's why I prefer to load the contents/data into a class with properties, such as surname, firstName, etc.

This way, even in a console, indidual properties, can be displayed, depending on what the user inputs (into a GUI or console).

Another technique would be to avoid multiple properties. Just have a single property, called, "fullLineContents", in the class. Then if you call, GetSurname(), it will "extract" the surname out of the entire line contents. This method will be slower though, due to the extra processing required, at the point GetSurname(), is called.

Console apps still have their place, if you're doing stuff through batch files for instance a console app is a natural place to do things as it's a simple executable and doesn't have any external user interfaces to display other than the already open console window.

Ok. Batch files - I'll give you that one.

Similarly we use a console app at work to host our WCF services when we want to run them locally. That sort of stuff has no place in a GUI and a console app is a handy way of doing things rather than having to deploy a windows service.

GUIs are good for lots of things, but there are also many other things that they're not at all good for.

I'm not saying you are wrong or even arguing against the use of console based programming in its entirity, however, I would like to what advantages you would get by using a console vs a pretty/user friendly GUI?

I'll kick this off by giving you the advantages of GUI:

1. You can display output in a format which is easier for the user to understand.
2. A GUI can improve user friendliness, in that anybody who is not trained to read console output, could (potentially), read the contents of the output, with ease, with a GUI.
3. With a GUI, you gain the use of clickable buttons and mouse.
4. With a GUI, you can have multiple display boxes, each displaying different pieces of data. When using threads, this is VERY important. If you use multiple consoles, you will eventually run out of screen space, while a GUI can potentially display all the windows/data, in a smaller screen size.
5. For the visually impaired, you can use different font sizes on GUIs, while with a console, you are limited.
6. Speed of use: if you have to type commands into a console, you may to type out long strings, without any mistakes. On a GUI, you can create a button, which is able to execute the entire string, with a single button press.

There are many more advantages, but I want to know of the advantages of a console (for future reference). I've seen many developers shun GUIs, to use consoles instead and there must be a reason for this...I just haven't worked it out yet and need a little help here.

Thanks
 
The advantage of a console app is basically the simplicity when none of that stuff is required.

Going back to the example I gave above about a host for our WCF services. Sure, we could have built a GUI to display everything nicely with all the features you mention, but it's just not needed and frankly would add a lot of complexity.
Any logging gets automatically logged to the console window by means of a log4net appender. To do this in a GUI application would mean creating the GUI to actually show the stuff, I guess you'd have to direct your log4net output to a StringWriter, hook into this and show the output in your GUI. Lots of extra work for very little benefit.

For something that the end user will see I'd always use a proper GUI, but just to log the output of the server it's overkill.
 
Back
Top Bottom