5 Replies - 7359 Views - Last Post: 08 August 2012 - 12:53 PM Rate Topic: -----

#1 kieranator1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-August 12

Starting Off Just A little Advice

Posted 07 August 2012 - 12:54 AM

Hello i am just getting into the programming and want to develop some skills init. Quick question i am trying to make a basic program. Which will open a text file, count characters words. I have created the form but when i have tried to open the file its says error but the file is there :s any help appreciated

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kierans_Project
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Read the file as one string.
            System.IO.StreamReader myFile =
               new System.IO.StreamReader("c:\\text");
            string myString = myFile.ReadToEnd();

            myFile.Close();

            // Display the file contents.
            Console.WriteLine(myString);
            // Suspend the screen.
            Console.ReadLine();
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Starting Off Just A little Advice

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Starting Off Just A little Advice

Posted 08 August 2012 - 11:11 AM

I doubt the file is really called "text".
More likely it is text.txt and you have your Windows preference set to hide the extensions of known file types.

Instead of hard-coding the path, you probably should use an OpenFileDialog to let the user navigate to the file they want. This has the advantage of always obtaining the correct and complete filepath directly from the OS.

You shouldn't use the StreamReader like that. It is better to enclose it in a using ( ) { } construct so it disposes of itself when it is completed.

Please look in the resources below for tutorials on how to properly read and write files because what you got ain't it.

Rename your controls before you do anything with them like assigning handlers. button_1 is meaningless in 6 months, where btn_ReadFile is helpful. See tip 2 below

Don't put the code to do work in the GUI event handlers like that. See tip 4 below.



Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler




  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler




First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover. Do a couple hundred on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

Then later you can start architecting your own simple stuff. Build a calculator. Build a DVD library program. Etc. Stuff that doesn't involve the complexity of a game. Then move up to games.



There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

This post has been edited by tlhIn`toq: 08 August 2012 - 11:18 AM

Was This Post Helpful? 3
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Starting Off Just A little Advice

Posted 08 August 2012 - 11:18 AM

Also, since you have a Windows Forms program, these lines will probably not have the effect you were expecting:
            // Display the file contents.
            Console.WriteLine(myString);
            // Suspend the screen.
            Console.ReadLine();



As a quickie replacement, you could do MessageBox.Show() and pass in myString.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Starting Off Just A little Advice

Posted 08 August 2012 - 11:59 AM

Console.WriteLine will let him see the text in the Output window of Visual Studio when this project is run with F5 key.

Also, if you create this as a Windows Forms application - then change the project type to Console application in the project properties it will open a console window along with the form. A fairly common technique for debugging.

Attached Image


Maybe this will help get you started on a better foot.

Attached Image

Code:
Spoiler

Was This Post Helpful? 3
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Starting Off Just A little Advice

Posted 08 August 2012 - 12:34 PM

Ah! That's what I get for building and running my programs from the command line... I don't get to see the pretty Console redirects. :lol:
Was This Post Helpful? 0
  • +
  • -

#6 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Starting Off Just A little Advice

Posted 08 August 2012 - 12:53 PM

Also, don't forget the Debug class. It is a very useful class that has extra methods to help with debugging and general program correctness checking, but sadly seems to be generally under utilized.

Debug.WriteLine() calls don't get compiled into release builds where the DEBUG build constant isn't defined. They print to the Visual Studio output window by default, but can be used to print to any number of places with each call (including the console), and is easily configurable in the App.config file (or in code if desired).

Overkill if you just need a few quick throwaway prints perhaps, but potentially very useful for more extensive debugging output :)

The Trace class can also be potentially very useful for release build tracing when the TRACE build constant is defined (which it is by default), and has much the same flexibility as the Debug class when it comes to output.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1