Saturday, March 19, 2011

Copying Array's Content to Another

Several methods used to copy the contents of an array to another.

Random r = new Random();
int[] pins = new int[4]{ r.Next() % 10, r.Next() % 10,
                         r.Next() % 10, r.Next() % 10 };

// 1st method
int[] copy = new int[pins.Length];
for (int i = 0; i < pins.Length; i++ )
{
 copy[i] = pins[i];
}

// 2nd method
int[] copy2 = new int[pins.Length];
pins.CopyTo(copy2, 0); //starting from index 0 of copy2

// 3rd method
int[] copy3 = new int[pins.Length];
Array.Copy(pins,copy3,copy3.Length);

// 4th method
int[] copy4 = new int[pins.Length];
copy4 = (int[])pins.Clone();

All of them are copying the reference of the original array, not the value.

Friday, March 11, 2011

Step Through Codes during Debugging Process

  • Right-click at the starting line of the code which you want to analyze
  • Select "Run to cursor"
  • Your program will be run. Enter some values to test the code
  • In the lower-left side, choose 'Local' tab. Here, you can monitor the local variables assignment. 
  • Debug -> Step into (shortcut: F11), to run the next code
  • Do the last step for several time while monitoring the 'Local' tab, until you figure out how your code works.

Wednesday, March 9, 2011

Browse and Display Text File (C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;          //namespace buat OpenFileDialog 
using System.IO;

namespace BrowseAndDisplay
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonBrowse_Click(object sender, RoutedEventArgs e)
        {
            //create an instance of the open file dialog box
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "C:";
            openFileDialog.Title = "Choose one of the text file";
            openFileDialog.FileName = "";
            openFileDialog.Filter = "Text file|*.txt";

            openFileDialog.ShowDialog();

            //isi textBox FileLocation dengan path file yang akan dibuka
            textBoxFileLocation.Text = openFileDialog.FileName;

            //tampilkan isi file text ke textBox DisplayTextFile
            FileInfo src = new FileInfo(openFileDialog.FileName);

            textBoxDisplayTextFile.Text = "";
            TextReader reader = src.OpenText();
            string line = reader.ReadLine();

            while (line != null)
            {
                textBoxDisplayTextFile.Text += line + '\n';
                line = reader.ReadLine();
            }
            reader.Close();
        }
    }
}