Polymorphism
04 Mar 2010 1 Comment
in OOPs Tags: OOPs, Polymorphism
Polymorphism
Polymorphism means same operation may behave differently on different classes. Polymorphism implemented by overloading and overriding. Method Overloading is the polymorphism at compile time where as Method overriding is polymorphism at run time.
Method Overloading
Method with same name but with different arguments is called method overloading. Method Overloading forms compile-time polymorphism because at compile time you know which method will be called because of method signature difference.
Example of Method Overloading:
Namespace MethodOverLoading
”’
”’ Parent Class
”’
”’
Public Class Parent
#Region “public Overloads APIs …”
Public Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from Parent”)
End Sub
Public Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(bv_Message & vbCrLf)
End Sub
#End Region
End Class
”’
”’ Child Class Inherited from public class
”’
”’
Public Class child
Inherits Parent
#Region “public Overloads APIs …”
Public Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from child”)
End Sub
Public Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(bv_Message & vbCrLf)
End Sub
#End Region
End Class
End Namespace
Sub Main()
‘Method Over Loading
Dim obj1 As New MethodOverLoading.Parent()
obj1.helloworld()
obj1.helloworld(“Hello world! calling parent object from Main!”)
Dim obj2 As New MethodOverLoading.child()
obj2.helloworld()
obj2.helloworld(“Hello world! calling child object from Main!”)
Dim obj3 As MethodOverLoading.Parent = New MethodOverLoading.child
obj3.helloworld()
obj3.helloworld(“Hello world! calling parent object created from child class from Main!”)
‘It will throw a run time error
‘Unable to cast object of type ‘MethodOverLoading.Parent’ to type ‘MethodOverLoading.child’.
Dim obj4 As MethodOverLoading.child = New MethodOverLoading.Parent
obj4.helloworld()
obj4.helloworld(“Hello world! calling child object created from parent class from Main!”)
System.Console.ReadLine()
End Sub
//Output
Hello World Default Message! Calling from Parent
Hello world! calling parent object from Main!
Hello World Default Message! Calling from child
Hello world! calling child object from Main!
Hello World Default Message! Calling from Parent
Hello world! calling parent object created from child class from Main!
Method Overriding
Method overriding occurs when child class overrides a method with the same signature in one of its parent class. Method overriding is Run-time polymorphism because you do not know which method it will be calling its depends on that object.
Example of Method Overriding:
Namespace MethodOverRiding
”’
”’ Parent Class
”’
”’
Public Class Parent
#Region “public Overridable APIs …”
Public Overridable Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from Parent”)
End Sub
Public Overridable Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(bv_Message & vbCrLf)
End Sub
#End Region
End Class
”’
”’ Child Class Inherited from public class
”’
”’
Public Class child
Inherits Parent
#Region “public Overrides APIs …”
Public Overrides Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from child”)
End Sub
Public Overrides Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(“From Child Class: ” & bv_Message & vbCrLf)
End Sub
#End Region
End Class
End Namespace
//Output
Hello World Default Message! Calling from Parent
Hello world! calling parent object from Main!
Hello World Default Message! Calling from child
From Child Class: Hello world! calling child object from Main!
Hello World Default Message! Calling from Parent
From Child Class: Hello world! calling parent object created from child class from Main!
Rahat Indori at wah wah
11 May 2009 1 Comment
किसने दस्तक दी ये दिल पर कौन है,
आप तो दिल में है तो बाहर कौन है |
राज जो कुछ हो दिल में इशारो में बता भी देना,
हाथ जब उससे मिलाना तो दबा भी देना |
कभी दिमाग कभी दिल कभी नजर में रहो,
ये सब तुम्हारे ही घर है किसी भी घर में रहो |
जुबा तो खोल नजर तो मिला जवाब तो दे
मैं कितनी बार लुटा हु मुझे हिसाब दो दे |
तेरे बदन की लिखावट में है उतार चड़ाव,
मैंने कैसे पडूंगा मुझे किताब तो दे |
रोज तारो को नुमाइश में खलल पड़ता है,
चाँद अँधेरे में निकल पड़ता है |
उसकी याद आई है ऐ सासों जरा धीरे चलो,
की धड़कन से भी इबादत में खलल आता है |
Adding Accordion Panes at run time
11 May 2009 Leave a Comment
in AJAX Toolkit, ASP.Net Tags: Accordion
Hello Friends,
Hope you are doing great!
Today I will show you how create Accordion Panes at run time.
As you might know, The Accordion is a web control that allows you to provide multiple panes and display them one at a time. It is like having several CollapsiblePanels where only one can be expanded at a time.
The Accordion is implemented as a web control that contains AccordionPane web controls. Each AccordionPane control has a template for its Header and its Content.
Here I will show you how to generate them at rum time and adding some control to them.
Lets first define our Accordion control:
Accordion ID="MyAccordion"
runat="server"
SelectedIndex="0"
FadeTransitions="false"
FramesPerSecond="40"
TransitionDuration="250"
AutoSize="None"
RequireOpenedPane="false"
HeaderCssClass="accordionHeader"
HeaderSelectedCssClass="accordionHeaderSelected"
ContentCssClass="accordionContent"
Now read the Pane Titles from Db into a table.
Dim dtDataTable As CollectionDS.CollectionDataTable = clsDb.pub_CollectionRead.Collection
Above statement will return a list collections which will serve as Pane Titles.
now first clear our Pane collection and then create pane one by one.
MyAccordion.Panes.Clear()
Dim intCount As Integer = 1
For Each drRow As CollectionDS.CollectionRow In dtDataTable.Rows
'Create a new Pane
Dim test As AjaxControlToolkit.AccordionPane = New AjaxControlToolkit.AccordionPane()
test.ID = "test_" & drRow.lngId.ToString
'Add A header
Dim lblhead As New Label
lblhead.ID = "lblHead_" & drRow.lngId.ToString
lblhead.Text = intCount.ToString & ". " & drRow.strCollectionName
lblhead.CssClass = "accordionLink"
lblhead.ForeColor = Drawing.Color.White
test.HeaderContainer.Controls.Add(lblhead)
'You can add any control here, i am using a grid
Dim gvGrid As New GridView
gvGrid.ID = "gvGridView_" & drRow.lngId.ToString
gvGrid.AutoGenerateColumns = False
gvGrid.CssClass = "GridStyle"
gvGrid.RowStyle.CssClass = "GridItemStyle"
gvGrid.AlternatingRowStyle.CssClass = "GridAlternatingItemStyle"
gvGrid.HeaderStyle.CssClass = "GridHeaderStyle"
Dim colId As New BoundField
colId.DataField = "lngId"
colId.HeaderText = "ID"
gvGrid.Columns.Add(colId)
Dim coltext As New BoundField
coltext.DataField = "strName"
coltext.HeaderText = "Name"
gvGrid.Columns.Add(coltext)
Dim colDesc As New BoundField
colDesc.DataField = "strDescription"
colDesc.HeaderText = "Description"
gvGrid.Columns.Add(colDesc)
test.ContentContainer.Controls.Add(gvGrid)
'I can also define handler for row bound if i want.
AddHandler gvGrid.RowDataBound, AddressOf gvGrid_RowDataBound
gvGrid.DataSource = clsDb.pub_CollectionItemRead(drRow.lngId).CollectionItems
gvGrid.DataBind()
gvGrid.Columns(0).Visible = False
Me.MyAccordion.Panes.Add(test)
intCount = intCount + 1
Next
Thats it, too simple, so by this way I can create my accordion panes on fly and also assign them the controls.
I hope this article is useful to you and you like it.
Happy Programming
Ashish Kumar Mahajan.
Find tables used in stored procedures.
12 Apr 2009 Leave a Comment
in Database, SQL Server Tags: Stored Procedures, Tables
Hello Friends,
Hope you are doing great!
Today I will show you how to find any text in stored procedures. We need to do it so many times and you might be doing manual work to find it.
You may also use sp_depends “tableName”, but it may not return accurate results.
So here are some other ways:
SELECT *
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE ‘%lnkSpedPagesDataToStudent%’
——————————————————
SELECT *
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE ‘%lnkSpedPagesDataToStudent%’
….
Hope, it works for you!
Thanks,
Ashish
Add column to temp table
10 Apr 2009 Leave a Comment
in Database, SQL Server Tags: SQL Server, Temp Tables
Dear Friends,
In this post, I will show you how to find if a column already exists in a temporary table and if not add that column in temporary table.
First create our temporary table and also add some columns in it.
IF OBJECT_ID(‘tempdb..#tempTable’) IS NOT NULL
DROP TABLE #tempTable
CREATE TABLE #tempTable
(
FieldName VARCHAR(50),
FieldDescription VARCHAR(100),
FieldData VARCHAR(5000)
)
Now suppose you have a table tblMenus with a column strMenuName and you want to add one column in #tempTable for each row in tblMenus. Here is the way to do it:
DECLARE @strMenu AS VARCHAR(30)
DECLARE @sql AS VARCHAR(1000)
DECLARE cur_Menus CURSOR
FOR SELECT strMenuName
FROM dbo.tblMenus
OPEN cur_Menus
FETCH NEXT FROM cur_Menus INTO @strMenu
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql = ‘IF NOT EXISTS (SELECT ”X” FROM TEMPDB.SYS.COLUMNS WHERE [OBJECT_ID] = OBJECT_ID(”tempdb..#tempTable”) AND [NAME] = ”’ + @strMenu + ”’)
BEGIN
ALTER TABLE #tempTable ADD [' + @strMenu + '] VARCHAR(30)
END’
EXEC(@sql)
FETCH NEXT FROM cur_Menus INTO @strMenu
END
CLOSE cur_Menus
DEALLOCATE cur_Menus
You Now have all the row as column in temp table.
and of course never forget to free our temporary resources.
IF OBJECT_ID(‘tempdb..#tempTable’) IS NOT NULL
DROP TABLE #tempTable
Thanks,
Ashish
Get Values from Hidden Columns in Gridview
07 Apr 2009 Leave a Comment
in ASP.Net Tags: ASP.Net, GridView
Dear Friends,
As working with GridView object in ASP.Net 3.5, i was facing a problem.
I have a gridview with some hidden column, here is my gridview structure:
<asp:boundfield visible=”False” datafield=”ID” />
<asp:boundfield datafield=”DOCNAME” headertext=”Name”>
<itemstyle horizontalalign=”Left”>
</asp:boundfield>
<asp:boundfield datafield=”VersionNumber” headertext=”Version”>
<itemstyle horizontalalign=”Center”>
</asp:boundfield>
<asp:boundfield visible=”False” datafield=”PDFFileName”>
<itemstyle horizontalalign=”Left”>
</asp:boundfield>
<asp:boundfield datafield=”intSequenceNumber” headertext=”Active”/>
<asp:checkboxfield datafield=”blnActive” headertext=”Active” />
As you can see I have Id and PDFFileName column hidden.
Now if i select some files and on postback i need those selected files and their Ids, i am getting blanks values for all the hidden cells.
This is how i was doing it:
Dim strIds As String = String.Empty
Dim strFiles As String = String.Empty
For Each drRow As GridViewRow In Me.gvHiddenColumn.Rows
strIds = strIds + drRow.Cells(pvt_GridColumn.ID).Text + “,”
strFiles = strFiles + drRow.Cells(pvt_GridColumn.PDFFileName).Text + “,”
Next
but i am getting ids and files balnks.
A simple solution for this problem is make the column hidden in the grid row bound event or after that, so i did it in prerender and it worked fine.
Private Sub GridViewHiddenColumn_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Me.gvColumns.Columns(pvt_GridColumn.ID).Visible = False
Me.gvColumns.Columns(pvt_GridColumn.PDFFileName).Visible = False
End Sub
Happy Programming,
Thanks,
Ashish