jump to navigation

Interfaces & Reflection April 23, 2006

Posted by Christian López Espínola in CSharp, Programming.
4 comments

Recently, at work, we needed to know if a class implemented an interface by using Reflection. I was selected to do this research, and it was very easy. This example needs no description:

class Program
{
static void Main (string [] args)
{
Type cocaType = typeof(Coca);
string interfaceNeeded = "iveneno";
Type interfaceType = cocaType.GetInterface(interfaceNeeded, true);
if (interfaceType != null)
{
Console.WriteLine(cocaType.Name + " implements " + interfaceType.Name);
}
else
{
Console.WriteLine(cocaType.Name + " doesn't implement " + interfazBuscada);
}

Console.ReadLine();
}
}

public interface IVeneno
{
void Envenenarse ();
}

public interface IDroga : IVeneno
{
void Chutarse ();
}

public class Hachis : IDroga
{
public void Chutarse ()
{
Console.WriteLine("Fumate un porro de hachi amparo");
}
public void Envenenarse ()
{
Chutarse();
}
}
public class Coca : IDroga
{
public void Chutarse ()
{
Console.WriteLine("Tiene un pinxo a mano?");
}
public void Envenenarse ()
{
Chutarse();
}
}

Setting up a web application locally with WXP_SP2, IIS 5.1, ASP.NET 2.0 & SQL Server 2005 Express April 4, 2006

Posted by Christian López Espínola in CSharp, Programming.
13 comments

Submitted by Rafael Vargas:

I decided to write this short how to in English because the information around the web is not enough to set up the right enviroment to debug our application in our localhost.

So, let’s start!!

I’ve been building a little Web Application in ASP.NET 2.0 which uses a local database file in the App_Data directory. It works fine in the ASP.NET server… but when I tried to use it in the IIS 5.1 it doesn’t. Now, we will take the following steps in order to make it work.

First thing I did was Publishing Web Site, Go to Build menu and choose Publish Web Site instead of Build Web Site. Okay… We will publish it in a folder we’ve created previously. If it founds an old version of our site, it prompts you so as to delete all old files. Choose Ok if so.

Once you have your nice website published, you will find that App_Code folder has disappeared. Don’t worry, classes have been compiled so there’s no need of the sources. (You will find a special file in the bin folder with the assemblies).

After that, you must right-click the root folder of our web application a choose Web Sharing tab from the Propieties window. Select Share this folder and then click Add. A new window appears. In the Alias field, type the name of the Virtual Name of the application. If you write Yadayada, your application will be found at: http://localhost/yadayada. In Access Permissions, you have to let it Read and Write (of course, otherwise you couldn’t update your database), in the Application Permissions one, choose Execute (because Scripts are ASP, not ASP.NET (this is a common mistake)). Click Ok and close the window.

Later, open the IIS Management Console (It’s found in Control Panel > Administrative Tools > Internet Information Services. Extends the Default Web Site node: you should see an item with the alias you chose before (yadayada). Right-click it and choose Properties. Then, go to the Directory Security tab and find the Edit button in the Annonymous Access and Authentication Control frame. Check the Annonymous Access and click Browse, in the new window, write “ASPNET” and click Ok. In this way, any user can use our web application and localhost\ASPNET user account. Now, apply the new configuration.

Finally, we must give the ASPNET user account permission to write in the folder of the web application. Right-click the root folder of our web application a choose Security tab from the Propieties window. If this tab doesn’t appear, you will have to do some changes in your windows configuration. Don’t panic, it’s so easy. In a windows explorer window go to Tools menu, open Folder Options, choose View tab, Uncheck the last option in the Advanced Settings: Use simple file sharing. Apply the new configuration and next time you choose the properties windows from the explorer, you will see a Security Tab.

Okay, you’re got it. You’ll see the Advance button in the bottom of the page. Click it and a new window appears. Just click Add in the new window. And write down ASPNET and click okay. It’s the same kind of window you see before. It adds the local machine aspnet account. Now, you have to choose what permissions this account will have: click Full Control if you’re sure that any of your applications on that folder cannot damage the system (of course man! what are you doing then?). Apply this configuration and try out your web application. You can see there’s no trouble now ^^

I hope this little tutorial help.

Goodbye.

If you’ve got any comment, suggerences or questions, feel free to write down a comment.

I have no post yet February 2, 2006

Posted by Christian López Espínola in Personal.
add a comment

Maybe some reader wants a new brief tutorial. I’m not going to do it now.
At the moment I’m very busy. I have a lot of exams right now. My stopped work needs to be restarted.
And I’m reading about eXtreme Programming and agile software development.
I’m also doing some experiments with NUnit, and what I learn I will post here soon.

Configuring an application January 14, 2006

Posted by Christian López Espínola in CSharp, Programming, xml.
6 comments

At first, sorry for posting so late, I didn’t forgot you. Today we’re going to make easy configuring our applications.
We need a file in our project called “App.config”, which will contain all our application parameters. This is a XML file, with the following format:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="settingName" value="settingValue"/>
<add key=�anotherSettingName� value=�anotherSettingValue�/>
[more...]
<add key=�Height� value=�200�/>
<add key=�Width� value=�500�/>
<add key=�FormTitle� value=�Hello World�/>
</appSettings>
</configuration>

If you use VS .NET 2005, right click at your project, Add a new item and select Application Configuration File will generate the file “App.config” automatically.

Now in our application we import the System.Configuration namespace, located at the System.dll assembly.
We must use an instance of AppSettingsReader class.


public class MyClass : System.Windows.Forms.Form
{
private AppSettingsReader configReader = new AppSettingsReader();
[...]
}

The AppSettingsReader class implements a method with the following prototype:


object GetValue(string key, Type type)

For example, we will make a Windows Form and configure it like follows (we can make it at the constructor or at InitializeComponent method):


this.Text = (string)configReader.GetValue(“FormTitle�, typeof(string));
this.Height = (int)configReader.GetValue(“Height�,typeof(int));
this.Width = (int)configReader.GetValue(“Width�,typeof(int));

Finally… build and execute! Change values at “App.config” and execute again!

How to make a splash screen. December 8, 2005

Posted by Christian López Espínola in CSharp, Programming.
5 comments

When we execute some applications, we see a splash screen with the logo of the program on it.

The computer user thinks that if there’s no new window in his monitor, computer is not working.

By that, we should show something when the application is loading his data structures.

How can we make it? Using threads. In CSharp we have to include the System.Threading namespace.

At our Main function we have to declare a thread like follows:

Thread th= new Thread (new ThreadStart(Splash));

Splash is an static function we will see later.

Then we start thread’s execution, and we initialize our structures. After that, we kill the secondary thread:

th.Start();

//initialize structures…

th.Abort();

Finally we start our main window:

Application.Run(mainForm);

Our Splash function could be like follows:

public static void Splash()

{

SplashForm sf = new SplashForm();

sf.ShowDialog();

}

If our structures load fast, the splash screen will not can visible. We can make it visible with a thread sleep. The time that thread sleeps is taken in milliseconds:

Thread.Sleep(4000);

Now our applications will seem more professional. :D

Connecting to an Access DataBase with ADO.NET December 6, 2005

Posted by Christian López Espínola in CSharp, Programming.
4 comments

This is a simple guide for reading data from a database. I chose Access for the example, but using another Database Manager is very similar to this.

At first, we have to connect to the database, using its connection string, and open the connection:

OleDbConnection con;
con = new OleDbConnection();
con.ConnectionString = @�Provider=Microsoft.Jet.OLEDB.4.0;� +
@�Data source= C:\cd.mdb�;
con.Open();

Now we create the command. For this we use OleDbCommand. Its constructor receives as parameters one string with the SQL sentence and the connection object.

OleDbCommand cmd;
cmd = new OleDbCommand(“select * from Customers”, con);

For sentences that read data, we have to use the method ExecuteReader. Its result rows are saved in an object called DataReader.

OleDbDataReader dr;
dr = cmd.ExecuteReader();

Now we iterate over the reader and print data at console. For this we use a DataReader indexer, which parameter is the name of the column.

while (dr.Read())
{

Console.WriteLine(“Name: ” + dr["Name"].ToString();
Console.WriteLine(“Name: ” + dr["Phone"].ToString();
Console.WriteLine(“Name: ” + dr["Mail"].ToString();

}

Closing all opened connections is very important:

con.Close();

Writing good code is important. For writing good code, we should take care about exceptions methods can throw. In this little demo we don’t make it, but be careful in your applications.

Welcome December 6, 2005

Posted by Christian López Espínola in Uncategorized.
add a comment

Welcome to my new blog. It’s the first blog I write in english, so be comprehensive with me. I will try not to use dictionaries or other help, so if you find a mistake please comment it to me. In this blog I’m going to write some technical articles, code and things relationated to my work, my studies, and, why not, my passion.

Be like at home ;)

Follow

Get every new post delivered to your Inbox.