Friday, August 29, 2014

C# Destructors


Destructors are used to destruct instances of classes.

Following are some key points about C# Destructor:

  1. Destructor in a class must be prefix with the tilde "~" character.
    Ex. If your class name is "MyClass" then it's destructor name should be "~MyClass()

    Sample Program:
    public class MyClass
        {
            public MyClass()
            {
                Console.WriteLine("Constructor");
            }

            ~MyClass()
            {
                Console.WriteLine("Destructor");
            }
        }
  2. A Class can only have one destructor.
  3. Destructors can not defined in structs. 
  4. Destructors can not be overloaded or inherited.
  5. Destructors can not be called. They invoked automatically.
  6. Destructors does not take modifiers or any parameters.
  7. Use destructor only when your class consumes 'unmanaged' resources like files, GDI objects (Bitmap),SQL connection objects.
  8. The destructor implicitly calls 'Finalize' method of the base class object.
  9. Empty destructor should not be used.When a class has destructor, an entry is added in "Finalize" queue. Empty destructor causes loss of performance.
  10. The programmer has no control over when the destructor is called because this is determined by the garbage collector.
Conclusion:

Destructors are not useful in most of the cases. You need to be careful if you are adding destructor in your classes. 
To release resources immediately, you can also choose to implement the dispose pattern and the IDisposable interface. The IDisposable.Dispose implementation can be called by consumers of your class to free unmanaged resources, and you can use the Finalize method to free unmanaged resources in the event that the Dispose method is not called.



I hope this article will help you in understanding C# Destructors.

Thanks & Regards,
Mahesh Bagul







Monday, August 25, 2014

Passed Exam 70-483 Programming in C#

I have taken first step towards MCSD certification and I  passed 70-483 Programming in C# in last month.

I used following books for this exam and it helped me a lot in understanding of few concepts.
  1. Exam Ref 70-483: Programming in C#
  2. MCSD Certification Toolkit (Exam 70-483): Programming in C#

In addition to this, I went through the Programming in C# Jump Start videos. It's really good videos for novice developers. 

Please note that there were few questions that were not at all covered in this book or videos. You may need to use additional resources like MSDN for exam preparation. 
Few resource links are mentioned below:


If you have any questions regarding this exam, please email me on mahesh.bagul@gmail.com

Happy certification.

Thursday, May 26, 2011

Change Windows Registered Owner and Orgnization name

You may need to change the registered user and company name for a Windows O.S. Unfortunately, this process is rather difficult and needs to be done through the registry.
Following the steps will help you to edit the registered user and orgnization name for your Windows computer.


  1. Open 'regedit.exe' through the start menu search box, and then locate the following registry key:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
  2. Find the 'RegisteredOwner' and 'RegisteredOrganization' keys in the right-hand pane. Right click on each of these and click on 'Modify'. Change them with new values.


3.To show off the new changes, just type 'winver.exe' into the start menu search box to see the About Windows box.
Regards,
Mahesh Bagul

Tuesday, January 18, 2011

Static Constructor in C#

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Properties of Static Constructor:
  1. Static constructor can't have access modifiers like Public,Private, Protected.
  2. It does not accept parameters.
  3. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  4. A Static constructor can be called directly.
  5. Static constructor can only access static members.
  6. The user has no control on when the static constructor is executed in the program.

Ex:

public class MYStaticClass
{

  // Static constructor:

  static MYStaticClass()
  {
   System.Console.WriteLine("Static constructor called.");
  }

  public static void StaticMethod()
  {
   System.Console.WriteLine("StaticMethod is called.");
  }
}



class TestClass
{
  private void Test()
  {
   // Call to static method.
   MYStaticClass.StaticMethod();

  }

}




//Output:
//Static constructor called.
//StaticMethod is called.



Regards,

Mahesh Bagul

Sunday, January 16, 2011

Vehicle Tracking System in Pune, India

Remote Data Exchange,Pune,India provides GPS based Vehicle Tracking, Personal Tracking solutions.


GPS based Vehicle Tracking System offers an unique way for companies and individuals to monitor and control their cars, jeeps, trucks and other vehicles.  Vehicle Tracking  is applicable in following areas:

  1. Fleet Management
  2. Call Center´s, BPO´s
  3. Courier Services
  4. Public Transport Systems
  5. Taxi and Cool Cab Services
  6. Cash/Jewellery Carrying Vans
  7. Railways
  8. Goods Carrier Companies 
  9. Oil Tankers
  10. Milk Vans
  11. Car Rental and Leasing companies
  12. School Vans
Remote Data Exchange is reputed Vehicle Tracking Company in Pune ,India

Remote Data Exchange,Pune,India provides GPS based Vehicle Tracking, Personal Tracking solutions all over India.

Contact Remote Data Exchange for enquiry.

Address:
Remote Data Exchange,
A1-402, Mahalaxmi Nagar, Mumbai-Bangalore Highway,
Warje, Pune 411 058, Maharashtra, India.

Call:
Mr.Tushar S. Wakde

+91 98224 36151 
+91 20 64700247
Email: tushar@rdxindia.net

Tuesday, January 11, 2011

Learn Expression Blend in 5 days

Hi Friends,

Microsoft provides a very good training session for Expression Blend. Click following link:

Learn Expression Blend in 5 days



Keep smiling

Mahesh Bagul

Wednesday, January 5, 2011

How to install assembly in GAC

Hi friends,

Follow following steps to install assembly in GAC (Global assembly cache) using C# VS 2005/2008 .


  1. In Visual Studio, create a new Visual C# Class Library project, and name the project TestGAC.
  2. To install assembly in GAC, assembly should be strong name. We use SN tool to generate cryptographic key pair. This tool is located in the \bin subdirectory where the .NET Framework Solution Developer Kit (SDK) is installed. Or eaiser way is:
    • In Solution Explorer, right-click GACDemo, and then click Properties.
    • Click the Signing tab, and then click to select the Sign the assembly check box.
    • In the Choose a strong name key list, click .
    • Type Mykey.snk
    • Compile the project.
3.Now you can install your assembly using Gacutil tool.

Ex: gacutil -I "[DriveLetter]:\[PathToBinDirectoryInVSProject]\TestGAC.dll"



That's it.Your assembly is in GAC....

Keep smiling..
Keep programming...

Mahesh Bagul
 
  1. http://support.microsoft.com/kb/815808
  2. http://support.microsoft.com/kb/315682

( Mahesh Bagul )
 Sr. Software Eng. Pune, India.

useful Links:

Monday, January 3, 2011

Global Assembly Cache

The GAC (Global Assembly Cache) is a machine wide cache that contains the assemblies that are shared by multiple applications.
The global assembly cache stores assemblies specifically designated to be shared by several applications on the computer.
Install assembly in GAC only if required.

GAC is used in the following situations:
  1. If the application has to be shared among several applications.
  2. If assembly has special security requirement.
There are two ways to deploy an assembly into the global assembly cache:
  1. Use utility called as "Global assembly cache tool (Gacutil.exe), provided by Windows Software Development Kit (SDK)
  2. Use an installer designed to work with GAC.
Requirement to install assembly in GAC.
  • Assembly should be Strongly named.
  • Assembly residing in the GAC must adhere to a specific versioning scheme which allows for side-by-side execution of different code versions.

That's it for now.

Keep smiling.....
Keep programming......

Regards,

Mahesh Bagul

( Mahesh Bagul )
Sr.Software Engineer
Pune, India

Monday, December 27, 2010

C# irreguler/Non rectangular shape Windows form

Hi all,

Today we will see how to customize your form shape according to your needs.

get bored looking at your rectangular shaped form.....
Following are simple steps to do this.

Step 1: Create a Windows Form Application

Step 2: Select Form1, In Properties change FormBorderStyle property to None.

Step 3: Set the image to BackgroundImage property of Form1. (This image will be the shape of your form)

Step 4: Set form Width and height equal to background image width and height.

Step 5: Set Transperencykey to Magenta color.

Step 6: Run and see your form.....


Be happy...... Make every one happy..............


Mahesh Bagul

ClickOnce Deployment

ClickOnce deployment enables you to publish Windows-based applications to a Web server or network file share for simplified installation.

Visual Studio provides full support for publishing and updating applications deployed with ClickOnce technology.

ClickOnce deployment is available for projects created with Visual Basic and Visual C#, but not for Visual C++.

Please refer MSDN for more details about ClickOnce deployment
http://msdn.microsoft.com/en-us/library/t71a733d(VS.90).aspx

Following are the steps for ClickOnce deployment:

Step 1: Create a Windows Form Application. Give name as RubberBandRectangle.

Step 2: Right click on the RubberBandRectangle project from the solution explorer and select Properties from the context menu.



Step 3: Select Publish tab.
Specify Publishing folder location. In My case I have given "F:\RubberBand Publish\" path. (This folder path must be shared)

Enter Installation Folder URL:
//MAHESHB/RubberBand%20Publish/ (In my case)






Step 4:  Click on Update and turn on Update feature by checking "The Application should check for updates"




Step 5: After you done with it Click on Publish Now button.


That's it... You have done it.


Keep smiling....

Mahesh Bagul

Thursday, December 16, 2010

Draw Rubber Band like Explorer using C# .NET

Hi Friends,

Today we will see how to draw a rubber band rectangle like Explorer does. in C#. You can easily do the same in VB.NET also.
It was a tough task assigned by my project manager. But I have achieved it.
Steps:
  1. Create a Windows Form application named as RubberBandRectangle.
  2. Give Form name as frmMain
  3. Paste the following code in your frmMain.cs file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace RubberBandRectangle
{
    ///
    /// Class to simulate Rubber Band rectangle
    ///

    /// Mahesh Bagul
    public partial class frmMain : Form
    {
        #region Declaration

        private Point MouseDownPosition { get; set; }
        private Point CurrentMousePos { get; set; }
        Color selectionColor;
        Brush selectionBrush;
        Color frameColor;
        Pen framePen;


        #endregion
        #region Constructor
        public frmMain()
        {
            InitializeComponent();

            this.MouseDownPosition = Point.Empty;
            DoubleBuffered = true;
            this.BackColor = Color.White;

            this.selectionColor = Color.FromArgb(200, 0xE8, 0xED, 0xF5);
            this.selectionBrush = new SolidBrush(selectionColor);
            this.frameColor = Color.FromArgb(0x33, 0x5E, 0xA8);
            this.framePen = new Pen(frameColor);

        }
        #endregion
        #region FormEvents
        ///
        /// MouseMove event
        ///

        ///
        /// Mahesh Bagul
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left && MouseDownPosition != Point.Empty)
                Invalidate();
        }

        ///
        /// OnPaint Event
        ///

        ///
        /// Mahesh Bagul
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

           
            if (MouseDownPosition != Point.Empty)
            {
                CurrentMousePos = this.PointToClient(MousePosition);
                Rectangle selectedRect = new Rectangle(
                    Math.Min(MouseDownPosition.X, CurrentMousePos.X),
                    Math.Min(MouseDownPosition.Y, CurrentMousePos.Y),
                    Math.Abs(CurrentMousePos.X - MouseDownPosition.X),
                    Math.Abs(CurrentMousePos.Y - MouseDownPosition.Y));
                e.Graphics.FillRectangle(selectionBrush, selectedRect);
                e.Graphics.DrawRectangle(framePen, selectedRect);
            }

        }
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }

        ///
        /// OnMouseDown Event
        ///

        ///
        /// Mahesh Bagul
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (e.Button == MouseButtons.Left)
                MouseDownPosition = e.Location;
        }
   
        ///
        /// OnMouseUp Event
        ///

        ///
        /// Mahesh Bagul
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            MouseDownPosition = Point.Empty;
            Invalidate();
        }


        #endregion
        #region Functions
     
        #endregion

      
    }
}




Compile and Run.....  fill beauty of .NET.
Keep Smiling.

Mahesh Bagul

Friday, December 3, 2010

Windows 7 - Enable/Turn on Remote Desktop

Remote Desktop is disabled by default in Windows 7 and also in Vista.
Simple steps to turn on Remote Desktop:


Right-click Computer and select Properties





















click the Remote Settings link on the left hand side:














Check Allow Remote Assistance connections to this computerSelect Allow connections from computers running any version of Remote Desktop(less secure) radio button




















It's done....

Keep smiling...
Mahesh Bagul

Windows 7 - Enable Telnet feature

By default Telnet is disabled in Windows 7 for security reason.
Following are simple steps to enable Telnet.
Start
Control Panel
Programs And Features
Turn Windows features on or off
Check Telnet Client
Hit OK
After that you can start Telnet via Command Prompt.

...........
Keep enjoying

Mahesh Bagul.

Thursday, December 2, 2010

Get Assembly attributes using C# .NET

Hi,
Extracting attributes of an assembly is very easy using .NET.
Following program will show this.


private void RetrieveAssemblyInfo()
{
Assembly asm = Assembly.GetExecutingAssembly ();

if ( asm == null )
return;

object[] asmAttributes = asm.GetCustomAttributes(false );

if ( asmAttributes.Length == 0 )
return;

foreach ( Attribute attribute in asmAttributes )
{
if ( attribute.GetType () == typeof ( AssemblyTitleAttribute ) )
{
tbxAssemblyTitle.Text = ( ( AssemblyTitleAttribute ) attribute ).Title;
}
else if ( attribute.GetType () == typeof ( AssemblyCompanyAttribute ) )
{
tbxCompanyName.Text = ( ( AssemblyCompanyAttribute ) attribute ).Company;

}
else if ( attribute.GetType () == typeof ( AssemblyCopyrightAttribute ) )
{
tbxCopyrights.Text = ( ( AssemblyCopyrightAttribute ) attribute ).Copyright;

}
else if ( attribute.GetType () == typeof ( AssemblyFileVersionAttribute ) )
{
tbxVersion.Text = ( ( AssemblyFileVersionAttribute ) attribute ).Version;

}

}

}

private void button1_Click( object sender, EventArgs e )
{
RetrieveAssemblyInfo ();
}

Tuesday, November 24, 2009

A method which can return multiple datatypes in C#

Simple solution to achive this.




private void button1_Click(object sender, EventArgs e)

{




String test = TestFunction("Mahesh Bagul.");




Console.WriteLine(test);




int val = TestFunction(10);




Console.WriteLine(val);

}







private T TestFunction<T>(T val)

{




return val;


//For a while I just return same value.

}

Monday, November 23, 2009

My First Blog

Hi Friends,
This is my first blog.
And it's nice to see you here...