VB.NET Quick Tips and Tricks

VB.NET


1 Where does the Web page belong in the .NET Framework class hierarchy?

System.Web.UI.Page

2 Where do you store the information about the user’s locale?


System.Web.UI.Page.Culture

3 What’s the difference between Codebehind="MyCode.aspx.cs" andSrc="MyCode.aspx.cs"?

CodeBehind is relevant to Visual Studio.NET only.


4 What’s a bubbled event?

When you have a complex control, like DataGrid, writing an event processing routine for each object (cell, button, row, etc.) is quite tedious. The controls can bubble up their eventhandlers, allowing the main DataGrid event handler to take care of its constituents

5 What tags do you need to add within the asp:datagrid tags to bind columns manually?

Set AutoGenerateColumns Property to false on the datagrid tag

6 What tag do you use to add a hyperlink column to the DataGrid?




7 Which property on a Combo Box do you set with a column name, prior to setting the DataSource, to display data in the combo box?
DataTextField property

8 To test a Web service you must create a windows application or Web application to consume this service?

False, the webservice comes with a test page and it provides HTTP-GET method to test

9 When you inherit a protected class-level variable, who is it available to?

Classes in the same namespace.

10 What’s the top .NET class that everything is derived from?

System.object

11 How's method overloading different from method over-riding? How are they different from shadowing?

"Method overloading means having two or more methods with the same name but different signatures in the same scope. These two methods may exist in the same class or anoter one in base class and another in derived class. Class Person
Private firstName As [String]
Private lastName As [String]
Private Sub New()
Me.firstName = """"
Me.lastName = """"
End Sub
Private Sub New(FirstName As [String])
Me.firstName = FirstName
Me.lastName = """"
End Sub
Private Sub New(FirstName As [String], LastName As [String])
Me.firstName = FirstName
Me.lastName = LastName
End Sub
End Class Calling Overloaded Methods.
Person(); // as a constructor and call method without parameter
Person(userFirstName); // as a constructor and call method with one parameter(like User's first Name)
Person(userFirstName,userLastName); // as a constructor and call method with one parameter(like User's first Name)
When to use Method Overloading?
Generally, you should consider overloading a method when you have required same reason that take different signatures, but conceptually do the same thing. "

"Overriding method definitions
In a derived class, if you include a method definition that has the same name and exactly the same number and types of parameters as a method already defined in the base class, this new definition replaces the old definition of the method.
A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to modify the methods defined in the superclass. This is referred to as method overriding. The following example demonstrates method overriding."

"Overriding: This method completely replace a base class method implementation. It uses the same signatures (Parameters and return values) but you add new statements from the original code or completely change the process. Here is a good example of Overriding in VB .NET:
'The Original Class
Public Class MyBaseClassSample
Public Overridable Function MyFunc(ByVal X as Integer, ByVal Y as Integer) as Integer
Retun X * Y
End sub
End Class
'The Derived Class
Public Class MyNewClassSample
Inherits MyBaseClassSample
Public Overrides Function MyFunc(ByVal X as Integer, ByVal Y as Integer) as Integer
'Take note: It accepts and return same values but it changes the process
Retun X + Y
End sub
End Class "

"Overloading is like expanding the versions of a method. You don't change the original method of the base class but you add new versions of it. To create new version of a method you must define new parameters or return new type of value. Here is an example of Overloading:
'The Original Class: Public Class MyBaseClassSample
Public Overridable Function MyFunc(ByVal X as Integer, ByVal Y as Integer) as Integer
Retun X * Y End sub End Class
'The Derived Class
Public Class MyNewClassSample
Inherits MyBaseClassSample
'Version 2: accepts new parameter
Public Function MyFunc(ByVal X as Integer, ByVal Y as Integer, ByVal Z as Integer) as Integer
Retun X * Y * Z
End sub
'Version 3: return new type
Public Function MyFunc(ByVal X as Integer, ByVal Y as Integer, ByVal Z as Integer) as String
Retun ""Answer is: "" & (X * Y * Z)
End sub End Class"

"Shadowing: This method has the characteristic of both Overloading and Overriding, You create a new version of a method (overloading) but you want the orignal version be hidden from the user of the derived class (overriding). To make it crystal clear, look at the following example:
'The Original Class
Public Class MyBaseClassSample
Public Overridable Function MyFunc(ByVal X as Integer, ByVal Y as Integer) as Integer
Retun X * Y
End sub
End Class

'The Derived Class
Public Class MyNewClassSample
Inherits MyBaseClassSample
'Version 2: accepts new parameter
Public Shadows Function MyFunc(ByVal X as Integer, ByVal Y as Integer, ByVal Z as Integer) as Integer
Retun X * Y * Z
End sub
End Class
'Client code

Dim MyShadowFunc as New MyNewClassSample ()
MyShadowFunc.MyFunc(3, 4) ' Generates error since this is the original method implementation
MyShadowFunc.MyFunc(3, 4, 5) ' Correct method"

12 What does the keyword virtual mean in the method definition?

 The method can be over-ridden

13 Can you override private virtual methods?

 No, moreover, you cannot access private methods in inherited classes, have to be  protected in the base class to allow any sort of access.

14 Can you prevent your class from being inherited and becoming a base class for some other classes?

Yes, that’s what keyword sealed in C# and keyword NotInheritable in the class definition is for.

15 Can you allow class to be inherited, but prevent the method from being over-ridden? Yes, just leave the class public and make the method sealed?

Yes, just leave the class public and make the method sealed.

C# VS Vb.NET

Assess Modifiers

 16 What is abstract class? (Must Inherit Class)


An abstract class is a special kind of class that cannot be instantiated. We use Abstract class and interface to enforce some rules to the classes which extends/implements. For example we can define a class say ""Bird"" and we can declare methods say ""Fly()"", ""Walk()"". This means whatever the class that is derived from Bird has to override or give definition for Fly() and Walk() and therefore we are making sure that all the derived classes follows or has the common functionalities. In other way the classes derived from superclass should have common properties. In addition to this the derive class can have its own methods like ""Swim()""... In other words, An Abstract class is only used for inheritance. This means it acts as a base class for its derived classes. One cannot use it to create objects. When a class is declared as “Abstract”, it can only be inherited and not instantiated. MustInherit Keyword can be used to declare a class as abstract.
Abstract classes can also specify abstract members."

17 Difference between abstract class and interfaces?

"Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract. In interface no assessebility modifiers are allowed which are ok in abstract class."

18 Example of Abstract Class?

"Imagine developing a new employee records system for a large multinational corporation. Your job is to supervise the implementation of classes to represent employees such that certain core requirements of the head office are met, while still providing flexibility for differing requirements from company branches elsewhere.
The core requirements are as follows:
• Name and DateOfHire properties will be implemented in the abstract class and cannot be overridable.
• A RetirementID property will be implemented in the abstract class to handle Social Security numbers because most employees live in the United States. Branches in other countries will use different ways to identify an employee's retirement identification, so this property will be overridable in derived classes to permit individual branches to implement it differently as required.
• A method called Compensation will take no arguments and will return a type Object containing details of the employee's compensation. Because compensation—salary, commissions, bonuses, and so on—are handled differently at the various branches, complete flexibility in implementing this method is necessary; it will be made an abstract method.
The code for the resulting abstract class, called EmployeeBase, is shown in Listing 1. You'll see that I've omitted the actual implementation of the base members because those details are not relevant here. Go to NEXT PAGE....."

"The EmployeeBase abstract class. At various company offices, a programmer will use this as the basis for classes that work with the local employee records software.
Public MustInherit Class EmployeeBase
Public Property Name() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property

Public Property DateOfHire() As Date
Get
End Get
Set(ByVal Value As Date)
End Set
End Property

Public Overridable Property RetirementID() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
"

"Public MustOverride Function Compensation() As Object
End Class
At the company office in Paris, France, a programmer is using the EmployeeBase class as the basis for the EmployeeFrance class (see Listing 2) to use with the local employee records software. This derived class will necessarily inherit the Name and DateOfHire members. Furthermore, the RetirementID member in the EmployeeBase class is suitable for use in France, so the new class will not override it. All that the programmer has to do is implement a method to override the abstract Compensation member. "

"The derived class EmployeeFrance.
Public Class EmployeeFrance
Inherits EmployeeBase
Public Overrides Function Compensation() As Object
' Implementation of the Compensation method for employees
' in France goes here.
End Function
End Class
In London, England, however, the base class's RetirementID property is not suitable, so the derived class (called EmployeeEngland, shown in Listing 3) will override that member as well as the Compensation member. The implementation of Compensation will likely be different from the one used in France.
Listing 3.
The derived class EmployeeEngland.
Public Class EmployeeEngland
Inherits EmployeeBase
Public Overrides Function Compensation() As Object
' Implementation of the Compensation method for
' employees in England goes here.
End Function
Public Overrides Property RetirementID() As String
' Implementation of the RetirementID property for
' English employees goes here.
Get
End Get
Set(ByVal Value As String)
End Set End Property
End Class"

Difference between Interface and Delegate?

"Interfaces allow us to extend some object's functionality, it's a contract between the interface and the object that implements it. It is used to
simulate multiple inheritance in C#. In the other hand, we have delegates...They're just safe callbacks or function pointers. They allow us to notify
that something has happened (Events). "

19 What is the difference between Finalize () and Dispose ()?

"Dispose() is called by as an indication for an object to release any unmanaged resources it has held.
Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object.
Dispose() operates determinalistically due to which it is generally preferred.
Although the .NET framework frees managed memory and resources transparently, it's not as adept at freeing unmanaged resources; you have to help it out by implementing the Dispose and Finalize patterns in your code. When the .NET framework instantiates an object, it allocates memory for that object on the managed heap. The object remains on the heap until it's no longer referenced by any active code, at which point the memory it's using is ""garbage,"" ready for memory deallocation by the .NET Garbage Collector (GC). Before the GC deallocates the memory, the framework calls the object's Finalize() method, but developers are responsible for calling the Dispose() method. Dispose methods must be called explicitly and hence the any object using IDisposable must also implement finalizer to free resources in situations wherein Dispose is not called. Multiple calls to dispose method must be ignored when called once. The objects disposable methods must be called in the order of containment."

20 Can you explain memory management in .NET ?

"One of the strengths of the Common Language Runtime (CLR) is its memory management abilities. It spawns garbage collection threads that clean up all of the objects that are no longer in use and frees up space in the memory heap that has been allocated to them. Even though the CLR guarantees that your unused objects will be cleaned up eventually, there’s no way of knowing when or how it’ll be removed from the memory heap. Theoretically, this could be years from the time that the object is instantiated.
There’s no good reason not to deterministically clean up your objects. Not only is it excellent programming practice, but you’re able to ensure that the object and all of the components that it encapsulates are being cleaned up immediately. You can deterministically finalize any object that implements the IDisposable interface by invoking its Dispose() method. If you use C# you have the advantage of the using statement which allows you to define a scope at the end of which an object will be disposed. One of the most common examples given uses a SqlConnection object:
using ( SqlConnection test = new SqlConnection( ... ) ) {
// code implementation
} // SqlConnection.Dispose() is automatically invoked
At the end of this scope, the SqlConnection’s Dispose() method is automatically invoked and all resources held by this object are released.

By adopting the practice of deterministic finalization, you’ll be able to efficaciously manage your resources. Additionally, this enables you to ensure that there are no objects that contain large chunks of unmanaged resources sitting around waiting for the garbage collector to come around and clean them up.

21 Difference between Array and ArrayList?

Array is collection of values of same data type, Array list is the collection of objects of same or different data type.



 22 What is the base class of .NET?

"System.Object is the base class of .NET
System.Web.UI is for asp.net
It Supports all classes in the .NET Framework class hierarchy and provides low-level services to derived classes. This is the ultimate super class of all classes in the .NET Framework; it is the root of the type hierarchy. Base class is a root class or superior class from which we can extend classes. It is the topmost in the classes so other classes can be derived from this class but it is not derived from any class. Depending on procedure base class may or may not give its objects to derived classes of it. "

23 How to store and retrieve images in the sql server database?

Images cannot be directly stored in SQL. Convert the image to a byte array using Memory stream object and store it in Sql by setting field to image



No comments:

Post a Comment