May 23, 2012, 05:01:26 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Prowlie Welcome back! The site migration is complete and normal service resumed.
Advanced search
Pages: [1]   Go Down
Print
Author Topic: C# practise (arrays) :)  (Read 1732 times)
0 Members and 2 Guests are viewing this topic.
adzy-2k6
Senior Member
****
Offline Offline

Posts: 720


« on: April 07, 2009, 06:26:18 AM »

Well, I have began to learn C# (I know its not exactly a partucularly liked language, but if i get good i can use the XNA game studio). Anyway, heres a practise i did to help me get used to arrays:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArrayTest
{
    class ArrayTest
    {
        static void Main(string[] args)
        {
            Console.Write("Input a string: ");
            string test = Console.ReadLine();
            Console.WriteLine();
            char [] array;
            int i = 0;
            foreach (char ch in test)
            { i++; }
            int j = 0;
            array = new char[i];
            foreach (char ch in test)
            {
                array[j] = ch;
                j++;
            }
            foreach (char s in array)
            {
                Console.WriteLine(s);
            }
        }
    }
}

Anyway, if anyone has any C&C on efficiency and use of code, be my guest Smiley (Im assuming a lot of languages are similar, so if theres a good method in actionscript, I should be able to do something similar in C#).

Edit: Btw, forgot to say what each part of the code does. Overall it will take a the string you type, and spell it out vertiacally using an array to store the letters.

The code first asks what string you would like to use, and allows an imput to variable test. It then creates an array called test (not establishing its size or making it usable yet). It then creates an integer called i, and uses a foreach loop to count how many letters are in the string, and sets the array to that size (now making it usable). It then creates another integer (j), and uses another foreach loop to add the letters to the array (using j to adjust the number of the slot in the array by increasing by one each loop). A final foreach loop prints the letters on the screen (one Console.WriteLine per letter, prints one letter per line). Then, the program closes on the next keypress.
« Last Edit: April 07, 2009, 08:38:01 AM by adzy-2k6 » Logged

Cel
Senior Member
****
Offline Offline

Gender: Male
Posts: 967


« Reply #1 on: April 09, 2009, 05:27:48 PM »

I got some C&C for you Smiley

Code:
foreach (char ch in test)
            { i++; }
You don't need this. You can do test.Length; to get the length of the string (so you can create the array after you get the input with a set length). Which also means that the first two loops are unnecessary.

Also, string has a few other useful methods like toCharArray(), that returns char[].

Code:
static void Main(string[] args)
        {
            Console.WriteLine("Input string: ");
            string input = Console.ReadLine();
            char[] charArray = input.ToCharArray();
            foreach (char currChar in charArray)
            {
                Console.WriteLine(currChar);
            }
        }

Btw, I dunno if VS Express has this, but you can use the TAB key to auto-complete loops and conditions. For example, you type foreach, and press TAB twice. The IDE will create the loop for you, and you can tab around the parameters easily instead of writing the whole thing yourself.

Also, the most important thing (and I mean, the MOST important thing): Name your variables properly. Every var, method, class, etc, must have a meaningful name. No i's and j's and tests. Sure, it doesn't matter in a test application like this, but you gotta get used to naming things properly if you're planning to write bigger software. And comments. A lot of comments. No joke. A month after you write something, you'll have no idea whats going on there anymore, and other people who may be working with you will have a hard time figuring stuff out.
Logged

My DeviantArt

<a href="http://img.photobucket.com/albums/v661/Vector88/sfx_3.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/sfx_3.swf</a><a href="http://img.photobucket.com/albums/v697/Espengenin/banner1star-1.swf" target="_blank">http://img.photobucket.com/albums/v697/Espengenin/banner1star-1.swf</a>
adzy-2k6
Senior Member
****
Offline Offline

Posts: 720


« Reply #2 on: April 10, 2009, 03:46:38 AM »

Btw, I dunno if VS Express has this, but you can use the TAB key to auto-complete loops and conditions. For example, you type foreach, and press TAB twice. The IDE will create the loop for you, and you can tab around the parameters easily instead of writing the whole thing yourself.

Also, the most important thing (and I mean, the MOST important thing): Name your variables properly. Every var, method, class, etc, must have a meaningful name. No i's and j's and tests. Sure, it doesn't matter in a test application like this, but you gotta get used to naming things properly if you're planning to write bigger software. And comments. A lot of comments. No joke. A month after you write something, you'll have no idea whats going on there anymore, and other people who may be working with you will have a hard time figuring stuff out.

Yea, I must appologise for not naming variables properly. Im too used to my maths where you are expected to use single letters to represent everything, usually not even the letter the word starts with (such as s representing distance in mechanics). I will try to keep that in mind from now on. As for comments, I usually try to include them, but for this I was sort of just absent mindedly typing away, trying to keep my sintax correct and getting the code to work. As for "string has a few other useful methods like toCharArray(), that returns char[].", at the time of writing this code, I had no idea about methods and properties (despite using similar things in python, such as ball.radius etc, I didnt realise that other classes had similar properties). Working though the book (I found a quite good ebook), It covers arrays before classes and properties, and i did this just as I had finished reading the arrays section. Looking at your code, I can see what you are on about. ours manages to do everything mine does in less than half the lines, and displays it in a much easier to read way. Hopefully, i will at some point be good enough to shorten my code to that extent, so thanks for the C&C to help me towards that Smiley
Logged

Cel
Senior Member
****
Offline Offline

Gender: Male
Posts: 967


« Reply #3 on: April 10, 2009, 03:52:22 AM »

Sure, no problem. Let me know if you need help with stuff. Smiley
Logged

My DeviantArt

<a href="http://img.photobucket.com/albums/v661/Vector88/sfx_3.swf" target="_blank">http://img.photobucket.com/albums/v661/Vector88/sfx_3.swf</a><a href="http://img.photobucket.com/albums/v697/Espengenin/banner1star-1.swf" target="_blank">http://img.photobucket.com/albums/v697/Espengenin/banner1star-1.swf</a>
Brackenwood
   

 Logged
Pages: [1]   Go Up
Print
Jump to:  

Theme by Pieter, based on Black Rain by Crip Powered by SMF 1.1.16 | SMF © 2011, Simple Machines XHTML | CSS

Page created in 0.102 seconds with 24 queries.