Posted in .Net Basics, C# 4.0

Abstract Classes in .Net

An abstract class means that, no object of this class can be instantiated, but can make derivations of this.

Its a special type of class that cannot be instantiated. (So why we need that ??) There are some reasons, an Abstract class is only to be sub-classed. That means it enforces some contracts or say methods to all the subclasses.

Here is a basic sample of abstract class and how to access their abstract and non abstract methods.

abstract class testDemo
    {
        public void callme() // non abstract method
        {
            // Some implementation
        }

        abstract public void ImplementMe(); // abstract method
    }

    class Child : testDemo
    {
        //implement the abstract method
        public override void ImplementMe()
        {
            throw new NotImplementedException();
        }
    }

This is how you can access methods of the abstract class.

 public void TestMethod()
        {
            Child obj = new Child();
            obj.callme();       // call to a non abstract method
            obj.ImplementMe();  // call to an abstract method which is implemented on the Child class
        }

An abstract class can also have abstract properties as well, derived class has to implement this property.

An Abstract class cannot be a sealed class. Following is not valid

  abstract sealed class testDemo {}

An abstract Method cannot be Private. Access modifier of the abstract method must be the same in both abstract class and its derived class. Otherwise it will be detected as error by compiler.

private abstract int myProp(); // incorrect

An Abstract method can’t be virtual as it is implicitly virtual.

An abstract member can’t be static but an abstract method can be a static one.

Hope this help someone.

Posted in Silverlight

Silverlight 5.0 Released !!

Silverlight 5 is finally released!

In addition to the platform having a release, I was really pleased to see an update to the Silverlight Toolkit, which has been one of the most popular things almost every Silverlight developer/application uses.

I would love to explore this new set of tools and will post some sample codes as I build them..

Go download the stuff and start building great apps..

Have fun !!

Posted in C# 4.0, Silverlight

Dynamic Style/theme Change in Silverlight 4.0

In Silverlight application, we often need to change the theme or style dynamically. It is quite simple to do from code side. Here is an example how to do that.

I have two separate theme files, Theme1.xaml and Theme2.xaml defined as ResourceDictionary.

Theme1.xaml

  <ResourceDictionary  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
  xmlns:sys="clr-namespace:System;assembly=mscorlib">       
  <!-- explicit style (has a key) -->
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">
        <Setter Property="Text" Value="SuperAwesome Co. LLC" />
        <Setter Property="Foreground" Value="Orange" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="15" />
 </Style>
 <!-- implicit style (no key - just a TargetType) -->
 <Style TargetType="Button">
     <Setter Property="Foreground" Value="Orange" />
     <Setter Property="Background" Value="Yellow" />
</Style>
</ResourceDictionary>

Theme2.xaml

<ResourceDictionary     
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
   xmlns:sys="clr-namespace:System;assembly=mscorlib">     
   <!-- explicit style (has a key) -->     
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">         
     <Setter Property="Text" Value="SomeOtherCompany Inc." />         
     <Setter Property="Foreground" Value="CornflowerBlue" />        
     <Setter Property="FontWeight" Value="Bold" />         
     <Setter Property="FontSize" Value="14" />     
  </Style>       
  <!-- implicit style (no key - just a TargetType) -->     
  <Style TargetType="Button">         
     <Setter Property="Foreground" Value="BlueViolet" />         
     <Setter Property="Background" Value="DarkOrchid" />     
  </Style> 
</ResourceDictionary>

Now I have added their reference in my App.xaml as below;

 <Application.Resources>         
    <ResourceDictionary>             
       <ResourceDictionary.MergedDictionaries>                 
         <ResourceDictionary Source="Themes/theme1.xaml"/>                 
         <ResourceDictionary Source="Themes/theme2.xaml"/>             
       <ResourceDictionary.MergedDictionaries>         
   </ResourceDictionary>    
</Application.Resources>

By default it will use Theme2.xaml as it is the last defined style. Now we’ll change the theme and use Theme1.xaml. Here is the code to change the theme dynamically from code behind.

  ResourceDictionary rd = new ResourceDictionary();
  //Load resource dictionary
  rd.Source = new Uri("/Themes/theme1.xaml", UriKind.RelativeOrAbsolute);
  //Clear previous styles if any...
  App.Current.Resources.MergedDictionaries.Clear();
  //Add the loaded resource dictionary to the application merged dictionaries
  App.Current.Resources.MergedDictionaries.Add(rd);

Hope this help some one 🙂

Posted in Microsoft, Silverlight

Send Email using Outlook from Silverlight 4.0 & COM

Today we’ll see one simple functionality of sending email from Silverlight application. I know many of you think that it is a very simple stuff.

Let’s do it using your Outlook application. To achive this we need to use a class called AutomationFactory which is available in System.Runtime.InteropServices.Automation.AutomationFactory.

Here is the code, which is very straight forward. But remember you must have to run the application out of browser to make this work!!

You need to add

using System.Runtime.InteropServices.Automation;
            var outlook = AutomationFactory.CreateObject("Outlook.Application");
            var olMail = outlook.CreateItem(0);
            olMail.To = txtTOEmailAddress.Text;
            olMail.Cc = txtCCEmailAddress.Text;
            olMail.Attachments.Add(@"C:\mypic.jpeg");
            olMail.Subject = txtsubject.Text;
            olMail.HTMLBody = txtmessage.Text;
            olMail.Display();
            olMail.Save();
            //You can send a mail by using Send() method.
            olMail.Send();

I hope this will help someone.

Posted in Silverlight

Lazy Initialization

Today I was searching for some tips to improve performance of my Silverlight application and I came across a feature called Lazy Initialization.

Continue reading “Lazy Initialization”