Archives

Archives / 2004 / October
  • Another RSS article from another author

    Few days later after ASPToday published my article about RSS, they published another cool article which shows how you can create a RSS user control. I know there are already few of them out there, but this one is a control, which scrolls the RSS news in an IFRAME. A very nice article.

    Read the complete article here.

    Sonu

  • Sharing Web Content Using RSS

    In this article Sonu Kapoor will show you how to use RSS to improve visibility of the content on your website. He'll examine the XML schema for an RSS feed and will see how a feed can be added to a website. This article will also examine a couple of the popular tools available for viewing RSS feeds. The accompanying downloadable sample code illustrates a website with an RSS feed that is automatically updated using ASP.NET code whenever data in an underlying database is changed through an administrative user interface.

    Read the complete article here.

    Sonu

  • Sharing Web Content Using RSS

    My latest article on ASPToday.com

    "In this article Sonu Kapoor will show you how to use RSS to improve visibility of the content on your website. He'll examine the XML schema for an RSS feed and will see how a feed can be added to a website. This article will also examine a couple of the popular tools available for viewing RSS feeds. The accompanying downloadable sample code illustrates a website with an RSS feed that is automatically updated using ASP.NET code whenever data in an underlying database is changed through an administrative user interface."

    Read the complete article here.

    Sonu

  • Generics in VB.NET

    Generics in VB.NET

    By Sonu Kapoor


    Abstract
    Introduction

    What are Generics?

    Why should I use Generics?

     

     

    Abstract:

     

    Generics are very useful if you know what they are and how to use them. In this article we will dive into the world of generics. We will show you what generics are and what they are used for. We will show you situations and practical examples where the usage of generics makes sense. Once you have completely read the article, you will understand what the term generic stands for and how it can be used in your own projects.

     

    Introduction:

     

    C++ was one of the first languages, which used some types of generics, however they were called “templates”. C# uses a similar syntax as the C++ templates, however in C# they are much easier to use. Generics are first available in Whidbey. Earlier versions of the CLR don’t support it. <> is used in C# to define a generic. However in VB.NET we use the keyword “Of”. In this article we will only discuss the VB.NET syntax.

     

    What are Generics?

     

    The term “generic” means not to be bound to any specific type regardless of whether we are referring it to any programming language or not! Just think of a “glass”, which can be filled with the following types: water, juice, soda etc. As you can see it is not bound to one type. It can be filled with any type. In the same way we can refer it to VB.NET. We could create a class called “Glass” which could store any of these types depended on the users choice.

    Generics in one sentence:
    Generics can be used to store collections, which are not bound to any data type.

     

    Why should I use Generics?

     

    Just imagine that you are working in a class where you would need to store different data types in a collection. Of course you could use an array, but don’t forget that you will need to create different arrays for different data types, which will just bloat your code. Furthermore your code will be unnecessary longer than needed. In that case we can use generics. We could use one generic class that could handle all of data types, without requiring writing the code again and again. This will save our time, resources, and increase the performance, because the user would not need to cast the data types. In summary we can say that generics are used for the following reasons:

     

    • Performance
    • Type Safe
    • Code Reuse

     

    Performance:

     

    How can generics help the developer to get a better performance? This is simple. The data-types are checked in the compile-timer rather than in run-time. Simply this change will improve the performance. No type casting necessary at run-time. However you should know that performance and speed are not the main aspects to use generics, but in maintainability and cleaner code.

     

    Type Safe:

     

    This means that whenever you add an object to the collection it is checked at compile time. That means that the data type should be safe and if you pass a wrong data-type to the collection, it will throw an error during the compile time and not in the run time. This way you can make sure to handle all errors so that they don’t appear at run time, when the client is using the application.

     

    Code Reuse:

     

    If you want to store different data types in a (non-generic) collection, than you won’t have any other option than declaring different collections for that. For example: You will need to declare a collection for integers, another for strings and another for double data types. These will unnecessary bloat your code. When using generics you just need to set the data type as a parameter and viola you can use it with whatever type you like.

     

    Simple Example

     

    We have read a lot by now. It’s time to see some code. To demonstrate how simple it is to use we have chosen a simple example. Lets see how it looks like:

    Our Gen Class:

    Public Class Gen(Of T)

        Dim dataType As T

        Public Property Val() As T

            Get

                Return dataType

            End Get

            Set(ByVal value As T)

                dataType = value

            End Set

        End Property

    End Class

     

    Our Main:

     

    Sub Main()

            ' This one accepts strings

            Dim mystring As New Gen(Of String)

            mystring.Val = "hello world"

            System.Console.WriteLine(mystring.Val)

     

            ' Change the declaration so that it accepts now an integer

            Dim myInt As New Gen(Of Integer)

            myInt.Val = 5

            System.Console.WriteLine(myInt.Val)

     

            Console.WriteLine("Press Enter to finish ... ")

            Console.Read()

    End Sub

     

    Our Output:

     

    hello world
    5

     

    How it works:

    As you can see above we have only two main parts. The first is the class “Gen” and the second is the main function. The “Gen” class is generic. As already described in the introduction we need the “Of” keyword to declare it as generic. In C# and C++ we can use <> to create generic classes.

     

    The “T” next right to the “Of” represents our data-type which we will pass when we create a new instance of that class. The rest of the code should be familiar to you.

     

    The interesting part is going on in the main function where we create two instances of the “Gen” class. In the first instance we are using a String. This will allow passing string values to the class variables.

     

            Dim mystring As New Gen(Of String)

            mystring.Val = "hello world"

            System.Console.WriteLine(mystring.Val)

     

    In the second instance we are using an Integer. Here we can use integer values to pass to the class variables.

     

            Dim myInt As New Gen(Of Integer)

            myInt.Val = 5

            System.Console.WriteLine(myInt.Val)

     

    As you can see this was our first simple generic example. To get the same result without using generic, will cost you two classes. Your code will just bloat for nothing. The first one would need a string and the second one could use an integer, but what if you decide to create a third data-type, lets say a double? Well then you will need to create a third class, which will again make your code longer than it already is. While using generics you only need one class for any data-type.