Component-Based Features

I l @ ve RuBoard

Until this point we've focused on programs and even program fragments in introducing the concepts of the language. However, VB.NET has several features that enable building component-based applications.

Namespaces and Assemblies

Namespaces provide a logical organizational system both internally within the program and externally to other programs. Consider the following example:

  Namespace Que.ByExample     Class Book      ...     End Class End Namespace 

Because namespaces may be nested, the preceding example is functionally equivalent to the following:

  Namespace Que     Namespace ByExample         Class Book         ...         End Class     End Namespace End Namespace 

In either case, we can reference the Book class via its fully qualified namespace:

 Que.ByExample.Book 

We can avoid using the fully qualified namespace by using the Imports statement as we have seen with the System namespace in prior examples:

 Imports Que.ByExample 

Assemblies are used for physical packaging and deployment by acting as containers for types. Assemblies take two forms, .exe files, which include a main entry point, and .dll libraries, which do not have a main entry point. The VB.NET compiler produces .exe applications by default. The compiler produces libraries by specifying the target switch as follows :

 vbc /target:library filename.vb 

Attributes

VB.NET supports attributes whereby programmers can specify declarative information for program entities and retrieve this information at runtime via reflection. In fact, the .NET Framework provides a number of special case attribute types already. Take the following example for instance:

  < WebMethod(Description="Returns the stock price", EnableSession=true)> Public Function GetCurrentStockPrice() As Desimal ... End Function 

In this fragment from a stock lookup Web service, we have marked the GetCurrentStockPrice method with the WebMethod attribute, which denotes it as callable via Web services. Just as the .NET Framework team developed the WebMethod attribute, we can derive our own custom attributes from the System.Attribute class:

 Imports System [AttributeUsage(AttributeTargets.All)] Public Class HelpDocAttribute     Inherits Attribute     Public Keywords As String     Private url As String     Private topic As String     Public Sub New(url As String, topic As String)         Me.url = url         Me.topic = topic     End Sub     Public Property Url As String         Get             Return url         End Get     End Property     Public Property Topic As String         Get             Return topic         End Get     End Property End Class 

In creating our custom attribute, we first have to denote on which targets our attribute can be used. We do this, ironically, by using another attribute on our class declaration, the AttributeUsage class. Note that we specify our attribute to be available on any elements by passing the enumerated value AttributeTargets.All to the AttributeUsage constructor. Please consult the .NET Framework docs for available values of the AttributeTargets enumeration.

After designating which targets our attribute will be used on, we next set up our positional and named parameters. For custom attributes, positional parameters are those corresponding to the arguments for the public constructors of the attribute. Named parameters are those defined by public read/write properties of the class. Note that in our example we have created two positional parameters, Url and Topic , and one named parameter, Keywords .

Once declared, our custom attribute may be employed in this way:

  <HelpDocAttribute("http:'help.xyzcompany.com/docs/SportsTicker.aspx", _      "SportsTicker Class")> Public Class SportsTicker     <HelpDocAttribute("http:'help.xyzcompany.com/docs/SportsTicker.aspx", _         "SportsTicker Constructor", Keywords="SportsTicker")>     Public Sub New()     ...     End Sub End Class 

Notice that for the SportsTicker class itself, we pass the positional parameters of Url and Topic , and for the public constructor, we add a third parameter for a keyword search.

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net