Search:

Custom Search
_________________________________________________________________

Monday, April 28, 2008

Deleting elements from a list while iterating

In C# you are not allowed to iterate and delete elements from a list at the same time. So what you need to do is to find a way to delete these elements later, after knowing what you are going to eliminate. In C++ you won’t have this problem but in C# an exception will be triggered.

This example below demonstrates how to solve this nicely. Basically what it does is that looks for none existing files from your hard drive and saves these on a separate List. Suppose that the object Element contains a string value representing a file path:

public void validateList() {

//New list that will contain the Objects to be deleted later on.
List<Element> listToDelete = new List<Element>();

//ListAll contains all the Element objects
foreach (Element e in listAll()) {
string location = e.loc;
if(!File.Exists(location)){
listToDelete.Add(e);
}

}
foreach (Element e in listToDelete) {
listAll.Remove
(e);
}

}

Sunday, April 20, 2008

Text file Reading and Writing in C#

This program below demonstrates the use of StreamWriter and StreamReader (both derive form the abstract type TextWriter and TextReader respectively). The .Net Framework has simple solutions to work with files. When working with text files there are usually three common steps:

1- Opening the file
2- Reading/Writing
3- Closing it

Now, lets take a look at this example:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication
{

class Program
{

public static void Main(string[] args)
{

//C# Code for writing into a file we call: File.txt

FileInfo f = new FileInfo("File.txt");
StreamWriter Text = f.CreateText();

//Insert text in separate lines
Text.WriteLine("-->Line One content");
Text.WriteLine("--->Line Two Content");

//Insert text but does not create a new line
Text.Write("---->Line Three ");
Text.WriteLine("Content");

//To create new Line.
Text.Write(Text.NewLine);
Text.WriteLine(System.DateTime.Now);
Text.Close();

//Calling the method to read File.txt
readFiles();

Console.Read();


}
public static void readFiles()
{

//We open File.txt
StreamReader sr = new StreamReader("File.txt");
string lines = null;

//Here we do a while loop in order to look for none empty lines.
while ((lines = sr.ReadLine()) != null)
{
Console.WriteLine(lines);
}

sr.Close();
}

}
}

Thursday, April 10, 2008

Connection to a data source in C#

Here is an easy way to get access for example, to an Access database located in your program folder using ADO .NET .

//Add this namespace for database handle
using System.Data.OleDb;

public OleDbConnection GetConnection()
{

OleDbConnection acc = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb";

return acc;

}

Later, when you want to open the database to load, save or whatever you need to do you can use the sentence, being Instance() a method used by Singleton pattern and ConnectionMgr the class name for managing connections:

IDbConnection acc = ConnectionMgr.Instance().GetConnection();

Convert Icon to Image in C#

This is actually very easy since we can use methods provided by the framework.So let’s write an example of how easy this is.

Icon a = new Icon(location);//location can be a string pointing to a file in your hard drive

Image im = a.ToBitmap();

Tuesday, April 1, 2008

The continue and goto statements.

The continue statement basically what is does is that it return controls to the boolean expressing that controls an iteration statement. Example:

for (int i = -6; i != 0; i++)
{

if (i == -3)
{

continue;
}

Console.WriteLine("i value: " + i);
}

So when the continue statement is executed under the if condition, it tells the for iteration to continue to the next iteration, and because of this the value -3 does not appear in the console.


The Continue statement is commonly used in while, do, for, switch and foreach statements.


The goto statement is very useful when you need to unconditionally transfer control to a labelled statements. In C# to make a labels statement is very easy, just place a name for the labelled followed by “:” on any statement. Example:

for (int j = 0; j != 7; j++)
{

Console.WriteLine("j value: " + j);
if (j == 4)
{

goto Exit;
}
}
Exit: Console.WriteLine("Exit reached, loop finished");

You can also use it in a switch statement by writing for example, “goto case 2”.