0 Comments

Was strolling around StackOverflow and came to a question #1118232.  Where this guy was asking how come you can create a class inheriting from another generic class like so:

class Person : EntityBase<Person>
{
 //Whatever here...
}

He said it’s like the chicken and egg problem :)  Which I sort of agree.  After pondering on the thing for a bit, I tried mucking around with it in Visual Studio and came up with this that I think sort of give me a bit more clarification on the idea, but not by much… Still trying to find when I need to really use something like this.   And the code that I came up with is this:

class Program
    {
        static void Main(string[] args)
        {
            var wife = new Human(Gender.Female);
            var baby = wife.GiveBirth();
            Console.WriteLine(baby.Gender);
 
            Console.ReadKey();
        }
    }
 
    class CanGiveBirthTo<T> where T : new()
    {
        public CanGiveBirthTo()
        {
        }
 
        public T GiveBirth()
        {
            return new T();
        }
    }
   
    class Human : CanGiveBirthTo<Human>
    {
        public Gender Gender { get; private set; }
 
        public Human(Gender gender)
        {
            Gender = gender;
        }
 
        public Human()
        {
            Gender = RandomlyAssignAGender();
        }
 
        Gender RandomlyAssignAGender()
        {
            var rand = new Random();
            return (Gender) rand.Next(2);
        }
    }
 
    enum Gender
    {
        Male = 0,
        Female = 1
    }

I don’t know if this is of any use to anyone…, but enjoy… LoL.

Any idea what sort of real application code that could fall into this pattern in real project?

0 Comments

I blogged not too long ago about some stuffs that I was pondering

Finally found out how one can get the Object of type ‘System.Decimal’ cannot be converted to type ‘System.Int32’ error.

Given this code:

using System;   
  using System.Data;   
  using System.Data.SqlClient;   
  using System.Collections.Generic;   
     
  namespace ConsoleApplication1   
  {   
      public class Foo   
      {  
          public int Id { get; set; }  
          public int Number { get; set; }  
      }  
    
      class Program  
      {  
          static void Main(string[] args)  
          {  
              var foos = new List<Foo>();  
              var fooType = typeof(Foo);  
    
              using (var cn = new SqlConnection(@"Server=.\SQLEXPRESS;Database=JimmyTest;Integrated Security=true"))  
              {  
                  using (var cmd = cn.CreateCommand())  
                  {  
                      cmd.CommandText = "SELECT * FROM dbo.[tablea] a INNER JOIN dbo.[tableb] b ON a.[BId] = b.[Id]";  
                      cmd.CommandType = CommandType.Text;  
    
                      cn.Open();  
    
                      using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))  
                      {  
                          var fieldCount = reader.FieldCount;  
    
                          while (reader.Read())  
                          {  
                              var foo = new Foo();  
    
                              for (var i = 0; i < fieldCount; i++)  
                              {  
                                  var property = fooType.GetProperty(reader.GetName(i));  
                                  if (property != null)  
                                  {  
                                      property.SetValue(foo, reader.GetValue(i), null);  
                                  }  
                              }  
                              foos.Add(foo);  
                          }  
                      }  
                  }  
              }  
    
              Console.ReadLine();  
          }  
      }  
  }

And this database:

USE master
GO

CREATE DATABASE JimmyTest
GO

USE JimmyTest
GO

CREATE TABLE tablea (Id int, BId int, Number int)
GO

CREATE TABLE tableb (Id int, Number decimal(10,2))
GO

INSERT INTO tablea VALUES(1, 1, 100)
INSERT INTO tablea VALUES(2, 2, 50)
GO

INSERT INTO tableb VALUES(1, 33.33)
INSERT INTO tableb VALUES(2, 15.23)
GO

Can you spot the bug?

Found it in the wild while troubleshooting one of my clients’ code.

Hint: Greed is one of the seven deadly sins.  It’s pure evil!

0 Comments

I used to code using Resharper previously and have gotten used to the F12 key to navigate to the next problem area / errors in my code and then pressing Alt+Enter to quickly fix the problem.

This is very useful and speed up my coding time considerably when doing TDD style development.

Missing Resharper in Visual Studio 2010, I was determine to find a way to replicate this.

After messing around a while with Tools, Options, Keyboard and Macro and what not, I found a way to do this.

It turned out my keyboard scheme was set to Visual Basic 6 for some reason instead of C# and the thing that I’m looking for was not bind to any key.

In C# scheme, the View.NextError command is bound to Ctrl+Shift+F12.

In VB6 scheme, View.NextError is not even bound.

After fixing the scheme, I was able to work much faster.

1. Code as usual per TDD, some squiggly lines will show up as you typed in class / method name that has not been defined yet.   Keep going until done.

2. Press Ctrl+Shift+F12, it will jump to the first squiggly.  Click Ctrl+. or Alt+Shift+F10 to show the smart tag options.

3. Choose the action that I want (Generate stub, etc.) and hit Enter.

4. Press Ctrl+Shift+F12 again to go to the next squiggly, repeat the process until I am done.

Awesome!!

I might remap the keyboard shortcut to how Resharper setting (F12 and Alt+Enter).  So used to those settings and less keystrokes involved.

0 Comments
public class CallToAction
{
    abstract class CSharpDeveloper { }
    class You : CSharpDeveloper { }
    string ReadTheContentOf(System.Uri url) { return string.Empty; }
    System.Uri URL = new System.Uri("http://stackoverflow.com/questions/9033/hidden-features-of-c");

    public CallToAction()
    {
        var you = new You();

        if (you is CSharpDeveloper)
            ReadTheContentOf(this.URL).AndLearnTonsOfStuffs();
    }
}

public static class MyExtensions
{
    public static void AndLearnTonsOfStuffs(this string input) { }
}

emoticon

”This compiles by the way.  It won’t do squat, but it compiles!!

Go spend half a day or a day and read that post, all the answers, the comments and the links within it.  I promised you won’t regret it and you might actually learn a thing or two about C# that you don’t know about.  I know I did.

Hint: the URL in the code above is actually clickable if you didn’t notice.  Or if you are hyperlink-challenged, you can cut and paste the following URL to your browser:
http://stackoverflow.com/questions/9033/hidden-features-of-c

Enjoy…

emoticon

0 Comments

“New request is not allowed to start because it should come with valid transaction descriptor.”

"Distributed transaction completed. Either enlist this session in a new transaction or the NULL transaction.”

“Internal .Net Framework Data Provider error 60.”

and…

“Object of type 'System.Decimal' cannot be converted to type 'System.Int32'”

Hmmm…

The first three has something to do with MSDTC, orphaned transaction and connection pooling I think…

The other one is really puzzling me.  I can’t think of anything that might result in that particular exception, but apparently something did…

My head is starting to hurt…

Need to figure out a different way to approach this problem…

Hmmm….