Tuesday, September 16, 2014

How to include value types AND strings in generic constraint.

I'm very like to use generics in the code. Usually generic implementation is working smoothly but exists one small problem with generic value types: cannot use string typed value because System.String is a class.

  1. Foo<string>("abc");



  1. private void Foo(T value) where T : struct
  2. {
  3. //...
  4. }
 
The simple solution is creating overwrite method:

  1. private void Foo(T value) where T : string
  2. {
  3. //...
  4. }

Nice, but not perfect. The beautiful idea was offered by KeithS instead to restrict a value to struct change it to IConvertible. Next types implemented an interface IConvertible but now String can be included in the constraint:
Boolean   
Byte   
Char   
DateTime
Decimal   
Double   
Int16   
Int32   
Int64   
SByte   
Single   
String   
Type   
UInt16   
UInt32   
UInt64 


Final:

  1. private void Foo(T value) where T : IConvertible
  2. {
  3. //...
  4. }



Tuesday, August 26, 2014

C# generation: from '?' via '??' to '???'

C# include several usable but not famous operators. Today we'll consider the generation of  operator '?'.
The first and known from language C old operator '?' was appeared in C# 1.0 in the Visual Studio 2002. An operator using for condition instead to block  if-else:
  1. Random rnd = new Random();
  2. //Long way
  3. bool isLessTo50;
  4. if (rnd.Next(100) < 50)
  5. {
  6.    isLessTo50 = true;
  7. }
  8. else
  9. {
  10.    isLessTo50 = false;
  11. }
  12.  
  13. //Short way
  14. bool isLessTo50 = rnd.Next(100) < 50 ? true : false;

The next '??' was offered 3 years later in Visual Studio 2005. The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand:
  1. DateTime? dt = GetDate();
  2. //Regular way
  3. if (dt != null)
  4. {
  5.    return dt;
  6. }
  7. else
  8. {
  9.    return DateTime.Now;
  10. }

  11. //Short way
  12. return (dt != null) ? dt : DateTime.Now;

  13. //New way
  14. return dt ?? DateTime.Now;

The last operator '???' was announced in Visual Studio 2012. I'm very like it because he is cutting the validation of object:
  1. class Person
  2. {
  3.  public string Name { get; set; }
  4.  public int ID { get; set; }
  5. }
  6.  
  7. //The program
  8.  Person person = InitPerson();

  9. //Old way
  10. if (person != null)
  11. {
  12.   return person.ID;
  13. }
  14. else
  15. {
  16.   return default(int);
  17. }
  18.  
  19. //New way
  20. return person.ID ??? default(int);

In Visual Studio 2014 the operator '?..' did not receive renewal but maybe in next versions we'll see '????'  and even '?????' :-).

Tuesday, October 15, 2013

Repair IE8 (IE7) and IE9

Unbelievable! After 2 days of research in Microsoft's sites why IE doesn't work the solution was found in non-Microsoft site.
Thank you very much Mr Kai Schätzl!

Link to article

Monday, September 30, 2013

How to blink text in all browsers

Javascript:
<script type="text/javascript" language="javascript">
 window.onload=blinkOn;
 
function blinkOn()
{ document.getElementById("blink").style.color="Red";
  setTimeout("blinkOff()",1000);
}
 
function blinkOff()
{  document.getElementById("blink").style.color="";
  setTimeout("blinkOn()",1000);
} 
</script>


HTML
<div id="blink">Hello, World!</div>

Thanks to Steven McConnon

Tuesday, February 26, 2013

C# property names without hardcoded strings


public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Test
{
    protected void InitDropDownList()
    {
        DropDownList ddl = new DropDownList();
        ddl.DataSource = new List<Person> { 
            new Person{ Id = 1, Name = "John Smith" },
            new Person{ Id =  2,Name = "Moshe Perez" } };

        //Old and bad way
        ddl.DataValueField = "Id";
        ddl.DataTextField = "Name";

        //New way
        ddl.DataValueField = GetPropertyName(() => new Person().Id);
        ddl.DataValueField = GetPropertyName(() => new Person().Name);

        ddl.DataBind();
    }

    private static string GetPropertyName<T>
(Expression<Func<T>> expression)
    {
        MemberExpression body = (MemberExpression)expression.Body;
        return body.Member.Name;
    }
}



From here