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

Wednesday, January 02, 2013

Test value generator

Frequently in the test time required to set random values to variables. The simple class will help you to save a time in the future:

public static class Generator
{
    public static readonly Random RND = new Random();
        
    // Generate IP address
    public static string GetIP(bool isIPv6 = false)
    {
        if (isIPv6)
        {
            return string.Format("{0}.{1}.{2}.{3}.{4}.{5}", RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256));
        }
        return string.Format("{0}.{1}.{2}.{3}", RND.Next(0,256), RND.Next(0,256), RND.Next(0,256), RND.Next(0,256));
    }

    // Generate GPS coordinate
    public static System.Device.Location.GeoCoordinate GetGeo()
    {
        return new System.Device.Location.GeoCoordinate
        {
            Latitude =  GetDouble(90),
            Longitude = GetDouble(90),
        };
    }
    // Generate string
    public static string GetString(int size, int percentOfSymbols = 30, int percentOfDigits = 30)
    {
        StringBuilder builder = new StringBuilder();
        int[] symbol = new[] { 33, 36, 38, 64, 94 };
        int[] digits = new[] { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
        int symbolsAmount =  (int) size * percentOfSymbols /100;
        int digitAmount = (int)size * percentOfDigits / 100;
        for (int i = 0; i < size; i++)
        {
            int switcher = RND.Next(3);
            char ch;
            if (switcher == 0 && symbolsAmount > 0)
            {
                int id = RND.Next(symbol.Length);
                ch = Convert.ToChar(symbol[id]);
                symbolsAmount--;
            }
            else if (switcher == 1 && digitAmount > 0)
            {
                int id = RND.Next(digits.Length);
                ch = Convert.ToChar(digits[id]);
                digitAmount--;
            }
            else
            {
                bool isLower = RND.Next(2) == 0 ? false : true;
                ch = Convert.ToChar(
                        Convert.ToInt32(
                        Math.Floor(26 * RND.NextDouble() + ((isLower) ? 65 : 97))
                            )
                        );

            }
            builder.Append(ch);
        }
        return builder.ToString();
    }
    // Generate Date
    public static DateTime GetDate()
    {
        return DateTime.Now.AddMinutes(  RND.Next(10000000)  * (RND.Next() % 2 == 0 ? -1 : 1));
    }
    //Get double
    public static double GetDouble(int limit = default(int))
    {
        return double.Parse(string.Format("{0}.{1}", limit == default(int) ? RND.Next() : RND.Next(90), RND.Next())) *  (RND.Next() % 2 == 0 ? -1 : 1);
    }
}

kick it on DotNetKicks.com