TechTips .NET/ CSLA.NET / ASP.NET / VB.NET / C# / SQL Server / MySql / Android
  • Oct
    12

     

    Hi friends.. sometime back I had a discussion with few of my friends on different topics on OOPS. Just thought to put a post of some of the important facts which might help others as well.  Starting with Abstract classes. Will be writing on more topics in next posts.
    Abstract Classes

    1. Abstract classes can have a non abstract ctor and they can’t be instantiated.
    2. Since abstract classes can’t be instantiated one should not use Public and Protected Internal modifier with Abstract class Ctor.
    3. Abstract class can be derived from a concrete class.
    4. Abstract class can have non abstract members (methods, events, properties) but non abstract class can’t have abstract members.
    5. An abstract class may contain abstract methods and accessors.
    6. An abstract method is implicitly a virtual method.
    7. Static and Virtual modifiers can’t be used with abstract methods
    8. An abstract inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.
    9. Abstract classes can have concrete static methods.

    Hope this helps..Please put your comments and any additional info about abstract class which may help others as well.

    No Comments
  • Jun
    16


    So what basically is reflection:


    Using a class library methods at runtime without adding its reference in your project is done through Reflection. In this article I will show you how to use reflection using a simple example.
    Let’s first create a Class Library.

    ScreenHunter_01 Jun. 13 12.06

    let’s code our ReflectionTest Class

    
    Namespace reflectionclass
    Public Class ClsReflection
    
    #Region "Methods"
    Public Function add(ByVal x As Integer, ByVal y As Integer) As Integer
    Return x + y
    End Function
    
    Public Function [sub](ByVal x As Integer, ByVal y As Integer) As Integer
    Return x - y
    End Function
    #End Region
    
    End Class
    End Namespace
    

    let’s make a test project now. Add a windows form application to the solution.

    ScreenHunter_02 Jun. 13 12.20

    Now let’s design our Form.

    ScreenHunter_02 Jun. 16 12.48

    so after putting two command buttons two listview controls and four textbox controls our form should look like this.

    Let’s code our form now

    Imports System.Reflection
    
    Public Class Form1
    Dim objAssembly As Assembly
    'Declare an array of Types to Hold All Classes of Assembly
    Dim arrTypes As Type()
    Dim objType As Type
    'Delcare an array to hold all methods of Assembly
    Dim arrMethods As MethodInfo()
    Dim objMethod As MethodInfo
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Try
    OpenFileDialog1.ShowDialog()
    txtAssembly.Text = OpenFileDialog1.FileName
    objAssembly = System.Reflection.Assembly.LoadFrom(txtAssembly.Text)
    For Each cls In objAssembly.GetTypes
    '
    If cls.FullName.IndexOf("ClsReflection") > 0 Then
    lstClasses.Items.Add(cls.FullName)
    End If
    Next
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub
    
    Private Sub lstClasses_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstClasses.SelectedIndexChanged
    Try
    lstMethods.Items.Clear()
    objType = objAssembly.GetType(Me.lstClasses.SelectedItem.ToString)
    arrMethods = objType.GetMethods
    For Each method In objType.GetMethods
    lstMethods.Items.Add(method.Name)
    Next
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub
    
    Private Sub cmdCallMethod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCallMethod.Click
    Try
    objMethod = objType.GetMethod(lstMethods.SelectedItem.ToString())
    Dim obj As Object
    obj = Activator.CreateInstance(objType)
    'Let's Create Array of Parameter
    Dim objParameters As Object() = {Int32.Parse(txtNum1.Text), Int32.Parse(txtNum2.Text)}
    'If there is no parameter in method then on the place of oo pass the null
    txtResult.Text = objMethod.Invoke(obj, objParameters).ToString()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try
    End Sub
    End Class
    

    ScreenHunter_03 Jun. 16 13.09

    Select Add and put any two numbers in the first and second number textbox and click on execute method and you should get the result.

    ScreenHunter_04 Jun. 16 13.12

    Now select Sub and enter any two number in first number and second number textbox and press execute method and you should get the desired result.

    This article show you how you can call method dynamically at runtime without referencing the assembly in your project. It is a very basic example but I think it will help you to understand how reflection works in .Net.

    If you have any point you are not getting in this article please let me know i will try to explain.

    Happy Coding!

    Kshitij

    6 Comments
  • Jun
    5

    Recently i faced lots of problem with my hosting company as almost daily my site was down due to some problems in their servers. They usually send me a link to trace my site. Basically that site provide service to ping back any URL from their servers which are located in different countries. So it is like first their US server pings the URL and sends back the results weather site is responding or not and then all of the servers located in different part of the word do the same thing and finally giving a summery. For me it was annoying to go again and again to that site and then type URL to trace it back. Furthermore they charge for their services if you wants to check your site (or any other URL) 24×7. So i decided to write my own service. In this service i write the ping back results to windows log but you can always enhance this code to send mails or even SMS if your site is down.

    So lets start and create a Windows Web Service Project

    New_prj_WinService
    Read the rest of this entry »

    8 Comments

Translate

EnglishFrenchGermanHindiItalianPortugueseRussianSpanish
| More