Pages

Showing posts with label C# 4. Show all posts
Showing posts with label C# 4. Show all posts

Sunday, October 17, 2010

Convert vb to c sharp project to C# 4.0

1. start a new C# project,
2. add the 4 class files that you have,
3. run them each through the VB->c# translator you linked to originally,
4. dump the VB logging stuff and add in log4net
5. turn the Windows Scripting stuff from VB into C# (I think your problem with this is that the translator above is flipping out on the types of WindowsScripting Host stuff)
6. Compile and test.

With luck, this will take you a couple of hours. With bad luck, it depends on what the project actually does and that will determine how long.

I wish you good luck.

If you decide to go this route, be liberal about commenting out huge parts of code and compiling and working on eliminating compiling errors first. I'll try to keep an eye out to help you with any other specific questions that I see come across the front page.

Saturday, October 2, 2010

C# Polymorphism

Compile Time Polymorphism

Method overloading is a great example. You can have two methods with the same name but with different signatures. The compiler will choose the correct version to use at compile time.

Run-Time Polymorphism

Overriding a virtual method from a parent class in a child class is a good example. Another is a class implementing methods from an Interface. This allows you to use the more generic type in code while using the implementation specified by the child. Given the following class definitions:

public class Parent
{
public virtual void SayHello() { Console.WriteLine("Hello World!"); }
}

public class Child : Parent
{
public override void SayHello() { Console.WriteLine("Goodbye World!"); }
}

The following code will output "Goodbye World!":

Parent instance = new Child();
instance
.SayHello();

Early Binding

Specifying the type at compile time:

SqlConnection conn = new SqlConnection();

Late Binding

The type is determined at runtime:

object conn = Activator.CreateInstance("System.Data.SqlClient.SqlConnection");

Saturday, August 21, 2010

The dynamic Type

Example Using dynamic

C# 4.0 introduces a new type called dynamic. In some ways it looks just like any other
type such as int, string, or FileStream: you can use it in variable declarations, or func-
tion arguments and return types, as Example 18-4 shows. (The method reads a little
oddly—it’s a static method in the sense that it does not relate to any particular object
instance. But it’s dynamic in the sense that it uses the dynamic type for its parameters
and return value.)

static dynamic AddAnything(dynamic a, dynamic b)
{
dynamic result = a + b;
Console.WriteLine(result);
return result;
}