Thursday, July 07, 2005

Operator overloading in VB.NET

Finally VB (VS 2005) supports operator overloading.. See this article for a detailed overview

http://www.devx.com/codemag/Article/28267/1954?pf=true

Here is some example..

Public Class MyString
   Private _text As String
   Public Sub New(ByVal text As String)
      _text = text
   End Sub
   Property Text() As String
      Get
         Return _text
      End Get
      Set(ByVal value As String)
         _text = value
      End Set
   End Property
   Public Shared Operator -(ByVal str1 As MyString, _
      ByVal str2 As MyString) As String
      Return str1.Text.Substring _
         (0, str1.Text.Length - _
         str2.Text.Length) & str2.Text
   End Operator
End Class

Generics:

Generics is another new feature in the .NET 2.0 Framework. As such, languages such as C# and Visual Basic 2005 now support this new feature.

Using Generics, you can define a class definition without fixing the data type that it is using at design time. Generics are simply like template classes in C++. That is a great addition for our VB :)

Thursday, April 14, 2005

Recovering ASP.NET after an IIS crash

Your Internet Information Server crashed, and you reinstalled it. Now, how to re install ASP.NET so that IIS can load and execute aspx pages? There is a tiny tool aspnet_regiis.exe in your Microsoft.NET system folder (e.g, at C:\WINDOWS\Microsoft.NET\Framework\vx.x.xx). Execute aspnet_regiis.exe -i (don't foget the -i switch) in the command prompt.

Creating custom attributes for metadata

Do you want to create custom attributes for describing your functions and classes when developing .NET projects?

First of all, create an attribute class, derived from System.Attribute. Now, I'll create an attribute class named DescriptionAttribute. Mark it with AttributeUsageAttribute (it is a system provided attribute), to show that this class can be used as an attribute.

<AttributeUsageAttribute(AttributeTargets.Class)> _
Public Class DescriptionAttribute
Inherits System.Attribute
Public Description As String = ""
Public Sub New(ByVal MemberDescription As String)
'Whether we show this object type/method in the schema
Description = MemberDescription
End Sub
End Class

AttributeTargets.Class means, only classes can be marked with this attribute. Now, let us apply our above attribute to another class

<DescriptionAttribute("Cool Class")> _
Public Class MyClass
End Class

The string "cool class" will be passed to the constructor of your above class. That is it. Now, you may need to check a class to know whether your DescriptionAttribute is applied on it. In my next example, I'll show how to read back your custom attributes from a data type (class, functions etc) in a library.

Converting VS.NET solutions between versions

If you are using a version of Visual Studio.NET, you need a utility for converting VS.NET solutions and projects from one version to other. Go for vsconvert. This utility converts projects and solutions from VS.NET 7.0 to VS.NET 7.1 and vice versa See www.codeproject.com/macro/vsconvert.asp

Connecting to Access mdb from ASP.NET

A lot of newbies are facing security problems, when they try to connect to a Microsoft Access Database ( mdb file) from ASP.NET, using oledb. This is because, the ASPNET user account in Windows doesn't have security access to the mdb file by defualt. ASP.NET uses the ASPNET user account for connecting to the database. To solve this,

  • Right click the Access database file, click properties.
  • Click the security tab in properties dialog box.
  • Add the user ASPNET to the security list, and provide full control (check allow full control checkbox)
  • If you can't see the security tab when you take properties, it is because 'simple file sharing' is enabled by default. Use windows explorer->Tools->Folder Options to disable simple file sharing.

Tuesday, April 05, 2005

XSD Object Generator

XSD Object Generator (xsdobjectgen.exe) tool from Microsoft is a powerfull tool for working with Extensible Schema Types. For example, consider that you need to create an object model from a schema with cross references. XSD Object Generator can identify such references, and it will create a collection for child elements. That is excellent, isn't it? The classes generated are suitable for both creating and consuming XML and for use as parameters and return values in web service implementations.This is an ideal tool for developers who need to implement support for XML based on an existing schema, such as schemas published by industry standards groups. The classes generated can also be serialized. Download it from Here

Sunday, March 20, 2005

Patterns And Practices - GOF Patterns and more

In real world application development, you see recurring problems. Once such an issue is addressed, the we addressed that problem can be re-used. Such reusable recurring solutions are called patterns. For example, if you need to create a class with only single instance can exist, you use a pattern called Singleton. The GOF (Gang Of Four) patterns are used widely these days. See Design patterns are recurring solutions to software design problems you find again and again in real-world application development. See http://www.dofactory.com/Patterns/Patterns.aspx for reading about patterns and to view their implementations in C# . (I hope I can write VB.NET examples soon as a tutorial). Microsoft has also proposed certain patterns and practices for developers working in MS platforms (mainly .NET) - see http://www.microsoft.com/resources/practices/default.mspx . If you know these patterns, you can decide which pattern should be used for solving a problem - and not how to solve a problem from the ground level.

Articles - Design Patterns, Neural Networks, C#, Programming