Pages

Search:

Friday, November 28, 2008

In this very short article I will show you the easiest way to add anew text line on a txt file using C# .NET.

I was researching on this matter very hard. First I started to read the entire file to the end and then add the new line. There is a small problem on doing this: it might work at compilation time but when you execute the application, you will see an error that says that two processes cannot run at the same time. Sound fair.Then I started working with locks. There are a couple of locks, the lock when you read and the lock when you write. This is good stuff to talk later but not right now.

Saturday, August 30, 2008

You may saw in the previous post, a couple of exceptions we can use that are provided by the .Net Framework derived from the System.Exception class. Those two exceptions were: The StackOverflowException and the NullReferenceException.
Now I will describe to you the OutOfMemoryException and the DivideByZeroException.

The OutOfMemoryException

This exception is thrown by the CLR when your machine runs out of memory. Every time you create an object using new, some memory will be reserved for it. In case there is now enough memory, the exception will be triggered.
Let´s write an example now. Supose you want to create a Hashtable that allocated a large capacity. If there is no memory to do this, the CLR throws the exception.
Take a look at this C# example:

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

namespace ConsoleApplication5

{

classProgram

{

staticvoidMain(string[] args)

{

try

{

Hashtable hatb = newHashtable(91230000);

}

catch (OutOfMemoryException)

{

Console.WriteLine("CLR out of memory");

}

}

}

}

Console:

The DivideByZeroException

This exception is quite simple: when you are dividing by 0, the CLR throws this exception.
Take a look at this example:

Friday, August 22, 2008

The easiest way to explain how this works is by writing an example. What we are going to do in the following example is quite simple: we are going to create an Object and initialize it with null value. This object contains string public value type that we can malipulate. But in this particulare case, our Object was set to null, so the CLR will catch this particular exception:

Class Person

publicclassPerson

{

publicString name;

}

Main:

staticvoidMain(string[] args)

{

try

{

Person p = newPerson();

p = null;

p.name = "CoderG";

}

catch (NullReferenceException) {

Console.WriteLine("p set tu null, cannot be referenced");

}

}

Console:

The StackOverflowException:

This exception is catch by the CLR when it runs out of stack memory. Be aware that the CLR has a finite amount of stack space, so if it gets full, the StackOverflowException will catch the exception. An easy way to trigger this is by defining a recursive function that does nothing, just call itself. Eventually, if the stack was infinite, this method will never stop, but because it is finite, the exception catches this:

Wednesday, August 6, 2008

Destructors are similar to de Constructors, almost the oposite.The destructor is used by the CLR when objects are destroyed.All this process happens in the background so the developer does not have to preocupate about deletingcreated objects. Writing a destructor is not necessary but if you are going to create one in your class,you can only add one.

The Destructors sintaxis are very similar to the constructors.They start with the simbole ~ and the name must be the same to the class.Also, there are a few particulares things about destructors you should know:-No parameters are allowed here.-As said before, you can only write one.-You can not call a constructor like you will normally do with a method. They are automatically used by the CLR (by the garbage collection).-No overload or inheritances are allowed.

Monday, August 4, 2008

Normally you will see structures with only variables on it, but methods can also be included. This is usefull when you need to write a method that works directly with the content of the stuct.

- The first method that I will create inside the structure is a constructor. Remember that there can be many constructores as long as the parameter list is different of each other.- The second method you will see next is a simple bool method that return true if both values of the structure varaibles (integers) are 50.

Thursday, July 31, 2008

Interfaces are a set of methods, properties and event that provides functionalities. This interces can be implemented by C# classes or structures.

Interfaces do not provide methods of their own. They only contain methods identifiers, return values and parameters.This is very useful when you have certain behaviour that need to follow a set of operations, such as manipulating a Data Base.C# classes are allowed to implement many interfaces.

Implementing an Interfaz in a class says that the class must provide implementation to all of the methods signatures from the interface.

Let’s write a small C# example. In this example you will see of to implement interfaces that contain methods signatures and how a class implements this interface.

Code:

interfaceInterface1{

void Method1();void Method2();

}

publicclassClass1 : Interface1{

publicvoid Method1(){//Implementation of the method}

publicvoid Method2() { }

}

Now, you can also use interfaces that the .NET Frameworks has. In this example you will see how to implement the Idisposable interface. The Idisposable interface supports a method called Dispose(). It also supports a constructor and a destructor.

Also you will see in the Main class, the keyword using. It is used to create a new object and can be manipulated only within the braces ({}). After the close brace is reached, the object will be automatically destroyed.

Thursday, July 17, 2008

This example is very similar to the previous one where I showed you how to write an example using the YesNo MessageBox. Here the only difference is that the MessageBox is with the AbortRetryIgnore Button.

Ok, now I will proceed to write a small example of this. Basicly what I want to do here is to distinguish between the three different types of buttons that this type of MessageBox contains. Here I will use a swith statment because I find it a little more correct.
So the complete C# code is this:

Friday, June 27, 2008

With the previous post you could see that MessageBox class is very powerful and contains a lot of options for the static method Show. Now we will see in more detail how to implement the functionalities of a Dialog containing a YesNo Button.

When you give the user that option, it is obvious that the behavior of the Yes Button must be different from the No so you must set this.

Let's proceed to write an example. This C# code example has basicly 2 functions: when the Dialog popus up, the user must select between the Yes or No options. If the Yes is selected, the application exits. If the No button is clicked, a Dialog will appear. So here is the code:

MessageBox class is used when you want to show some kind of information or when the application needs user intervention.
This class contains static methods that help to interactuate with users. All you have to do is set the correct parameters of the show method. Some are: title, a proper message, icons, botons, quantities and more.

The show method returns a DialogResult type value that will need to be evaluated in order to make future actions, for example, open another dialog window.

Let’s look at some examples now:

I´ll start by creating a Windows From. Next, I´ll add 5 Buttons controls to the form and create a click event for all of them. On the click event method I created different messageBoxs for each one. Let’s analize them:

-For the button0, the code line is:

MessageBox.Show(this, "Hello", "Title Button0");

Nothing really complicated here. This creates a simple dialog with the text Hello, owner this and title Title Button0.

This is a little more interesting. We indicated here that the title would be Title 1, the message Abort, Retry, Ignore example and the Button for this specific dialog is the AbortRetryIgnore Button, the same one you see in most windows applications.

This is very similar to the previus one: Owner this, title Title2, message Error on button2, Continue? , Button YesNo and Icon Error. This will create a dialog that contains both options Yes and No, and also an Error Icon located left of the message will appear.

Similar to the others but now the Button is the Ok button and the Icon Warning will apear. So the purpose of this is to indicate an error, warning or a message that the user must see and give the only option of clicking the OK button.