Thursday 14 June, 2007

Some VB.Net Keywords

  • Mustoverride
The MustOverride keyword specifies that a property or procedure in a base class must be overridden in a derived class before it can be used.

The MustOverride keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example :Public MustOverride Sub Paint(ByVal g As Integer, ByVal r As Integer)
Public MustOverride Property abc() As String The derived class must implement this property.

  • Overridable
The Overridable keyword specifies that a property or method can be overridden in a derived class.

Overridable keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example: Public Overridable Function Abc() As String
End Function

The difference between MustOverride and Overridable method lie in the implementation , methods marked with MustOverride can not have an implementation while members marked with Overridable may or may not have an implementation in the base class

  • Overrides

The Overrides keyword specifies that a property or method overrides a member inherited from a base class.

The Overrides keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example : Public Overrides Sub A(ByVal str As String)

  • Overloads
The Overloads keyword declares a property or method with the same name as an existing member, but with an argument list different from the original member.

The Overrides keyword is used in these contexts:
Function Statement
Property Statement
Sub Statement

Example:
'Base Class
Public MustInherit Class BaseClass
Public MustOverride Sub A()
End Class
'Derived Class
Public Class DerivedClass
Inherits BaseClass
Public Overloads Overrides Sub A()
End Sub
Public Overloads Sub A(ByVal test As String)
End Sub
End Class

  • Shadow
The Shadows keyword indicates that a declared programming element shadows, or hides, an identically named element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind.

The Shadows keyword is used in these contexts:
Class Statement
Const Statement
Declare Statement
Delegate Statement
Dim Statement
Enum Statement
Event Statement
Function Statement
Interface Statement
Property Statement
Structure Statement
Sub Statement

Example :
The Shadows and Overloads keywords cannot be specified at the same time.

Class Base
Sub F()
End Sub

Sub F(ByVal i As Integer)
End Sub

Sub G()
End Sub

Sub G(ByVal i As Integer)
End Sub
End Class

Class Derived
Inherits Base
' Only hides F(Integer)
Overloads Sub F(ByVal i As Integer)
End Sub

' Hides G() and G(Integer)
Shadows Sub G(ByVal i As Integer)
End Sub
End Class


Finally a Very Good Question :

Whats the output of the following program

Class A
Public Overridable Sub F()
Console.WriteLine("A.F")
End Sub
End Class

Class B
Inherits A
Public Overrides Sub F()
Console.WriteLine("B.F")
End Sub
End Class

Class C
Inherits B
Public Shadows Overridable Sub F()
Console.WriteLine("C.F")
End Sub
End Class

Class D
Inherits C

Public Overrides Sub F()
Console.WriteLine("D.F")
End Sub
End Class

Module Test
Sub Main()
Dim d As New D()
Dim a As A = d
Dim b As B = d
Dim c As C = d
a.F()
b.F()
c.F()
d.F()
End Sub
End Module

Answer

B.F
B.F
D.F
D.F

Sunday 10 June, 2007

CTS

Q. What is Common Type System (CTS)

A.The common type system defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime's support for cross-language integration. The common type system performs the following functions:

* Establishes a framework that helps enable cross-language integration, type safety, and high performance code execution.
* Provides an object-oriented model that supports the complete implementation of many programming languages.
* Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other.

The CTS provides every language running on the .NET platform with a base set of data types. While the CTS is responsible for defining the types that can be used across the .NET languages, most languages choose to implement aliases to those types. For example, a four-byte integer value is represented by the CTS type System.Int32. C# defines an alias for this called type called int.

Wednesday 30 May, 2007

Questions related with assemblies

Q .How should an assembly be uninstalled
A.Uninstalling an assembly should be done with care.
The following command removes the assembly hello from the global assembly cache(GAC) as long as no reference counts exist for the assembly.
gacutil /u hello

Note : If there is only one version of hello assembly the above command is fine and you are in safer side. Incase if there are more than one version of the assembly or different assembly with same name exists then the above command might remove more than one assembly from the assembly cache because the assembly name is not fully specified. For example, if both version 1.0.0.0 and 3.2.2.1 of hello are installed in the cache, the command gacutil /u hello removes both of the assemblies.

Then how to remove the assembly safely: Use the following example to avoid removing more than one assembly. This command removes only the hello assembly that matches the fully specified version number, culture, and public key.
gacutil /u hello, Version=1.0.0.1, Culture="de", PublicKeyToken=45e343aae32233ca

Q . How do I see the physical file structure of GAC in explorer and how can one debug assemblies in GAC?

A. It is possible to see physical file structure of GAC folder.

As such Winnt\assembly\gac cannot be browsed using windows explorer.
To view the physical file structure add a binary value named 'DisableCacheViewer' to the registry key HKLM\Software\Microsoft\Fusion and set it to a non-zero value.

An alternative to this is by opening visual studio command prompt and navigating to
cd c:\%windir%\assembly\gac and listdown the directories by using the dir commandthere will be individual directory for every assembly installed in GAC.If you will further drilldown
to the hierarchy of assembly you will see a folder starting with version number and ending with public key of the assembly
something like : 1.0.5000.0__b03f5f7f11d50a3a
In thats particular folder you will find the copy of the assembly , if you want to debug an assembly which is installed in gac(Shared Assembly) then you need to copy the pdb file here.

Thursday 17 May, 2007

Q.What is an Assembly
A. Assembly is a reusable, versionable, and self-describing building block of a common language runtime application.

Assemblies provide the infrastructure that allows the runtime to fully understand the contents of an application and to enforce the versioning and dependency rules defined by the application. These concepts are crucial for solving the versioning problem and for simplifying the deployment of runtime applications.

Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. An assembly provides the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly.

In general, a static assembly can consist of four elements:

.The assembly manifest, which contains assembly metadata.
.Type metadata.
.Microsoft intermediate language (MSIL) code that implements the types.
.A set of resources.

Q Const and ReadOnly whats the difference ?

A. Hi :-)
Ever wondered whats the difference between ReadOnly and Const fields in .Net Type. Well i got the answer yesterday...( i suggest Applied .Net Framework Programming by Jeffery Richter as reference).

The values of all the Const fields should be known to the C# compiler at compile time. This value is then embedded in the metadata of the assembly that contains the type which has Const members.

e.g.
using System;
namespace spider
{
class useless
{
public const int maxVotes=30;
}

}

when the above is compiled the IL generated had 30 instead of maxVotes.
This can cause a versioning problem when we are talking about using constants defined in a different assembly.
lets take an example
imagine that another assembly were to use the const defined in the spider namespace ..(lets say the spider namespace is compiled into spider.dll).

using System;
using Spider;
namespace octopus
{
class TestConst
{
Console.WriteLine("The value of the constant : {0}",spider.useless.maxVotes);
}
}

lets say we compile the above code to get the octopus.exe.

Now lets assume that we change spider.dll to make the value of maxVotes=100,
so now when we run the octopus assembly we expect the value 100 to printed but that does not happen , the program still outputs the value 30. This is because the constant spider.useless.maxVotes is evaluated at compile time of the octopus.exe so unless we re compile octopus.exe we would not see the change reflected.

If U want to do away with this problem then the read only fields are a solution. They are evaluated at runtime.



Q. What is Finalize?

A. Finalize method acts as a safeguard to clean up resources in the event that your Dispose method is not called.


This method is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to SuppressFinalize. During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as ReRegisterForFinalize and GC.SuppressFinalize has not been subsequently called.

The garbage collector keeps track of objects that have Finalize methods, using an internal structure called the finalization queue. Each time your application creates an object that has a Finalize method, the garbage collector places an entry in the finalization queue that points to that object. The finalization queue contains entries for all the objects in the managed heap that need to have their finalization code called before the garbage collector can reclaim their memory

Finalize operations have the following limitations:

1.The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.

2.The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already finalized when the finalizer of Object A starts.
The thread on which the finalizer is run is unspecified.

The Finalize method might not run to completion or might not run at all in the following exceptional circumstances:

Another finalizer blocks indefinitely (goes into an infinite loop, tries to obtain a lock it can never obtain and so on). Because the runtime attempts to run finalizers to completion, other finalizers might not be called if a finalizer blocks indefinitely.
The process terminates without giving the runtime a chance to clean up. In this case, the runtime's first notification of process termination is a DLL_PROCESS_DETACH notification.

A type must implement Finalize when it uses unmanaged resources such as file handles or database connections that must be released when the managed object that uses them is reclaimed. See the IDisposable interface for a complementary and more controllable means of disposing resources.


Implementing Finalize methods or destructors can have a negative impact on performance and you should avoid using them unnecessarily. Reclaiming the memory used by objects with Finalize methods requires at least two garbage collections. When the garbage collector performs a collection, it reclaims the memory for inaccessible objects without finalizers. At this time, it cannot collect the inaccessible objects that do have finalizers. Instead, it removes the entries for these objects from the finalization queue and places them in a list of objects marked as ready for finalization. Entries in this list point to the objects in the managed heap that are ready to have their finalization code called. The garbage collector calls the Finalize methods for the objects in this list and then removes the entries from the list. A future garbage collection will determine that the finalized objects are truly garbage because they are no longer pointed to by entries in the list of objects marked as ready for finalization. In this future garbage collection, the objects' memory is actually reclaimed.

Note:Finalize is protected and, therefore, is accessible only through this class or a derived class.

Q Difference between Build and Rebuild options in .NET
A. VisualStudio.Net provides us with two options for compiling and creating builds for our application. They are Build Solution and Rebuild Solution options which can be accessed from the Build menu. The differences between these two options are
1. The Build Solution option compiles only those project files and components that have changed since the last build. For example consider that you have two projects Proj1 and Proj2 in your solution MySolution. When you compile the solution using Build Solution option after making some changes to Proj1 only Proj1 will be compiled and built but Proj2 will not be compiled since there are no changes to it.
2. On the other hand the Rebuild Solution option builds all project files and components irrespective of the changes made to them. For example consider that you have two projects Proj1 and Proj2 in your solution MySolution. When you compile the solution using Rebuild Solution option after making some changes to Proj1, both Proj1 and Proj2 will be compiled and built even though there are no changes made to Proj2.

Q. What Is XSD

A The XML Schema definition language (XSD) enables you to define the structure and data types for XML documents