.Net Object Inheritance

March 10th, 2009

There is a nice little feature of OO (in .Net at least). When you create an object that is inherited from other objects, you can cast the object up and down the inheritance path to your hearts content, and the object happily retains its information from each of the levels, even though you might not be able to see that information at certain levels. The only proviso is that you can only convert as far down the chain as the original object. If you imagine an object as a sheet of paper, and a class as one of those ‘code cards’ you used to get as a kid (You know, the ones with the holes in specific locations that you could place over a sheet of paper to read the ‘hidden’ message. Although you are only able to see a subset of the information, all of the information is still contained on the page. You can change the code card to different types of card, but the underlying information remains.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Module Module1
 
    Sub Main()
        Dim Guard As New GuardDog
        Guard.Name = "Killer"
        Guard.Location = "The Garden"
        Guard.patrol()
        Guard.Bark()
 
        'Now we can convert the guard back into a basic pet object
        Dim myPet As Pet = DirectCast(Guard, Pet)
        Guard = Nothing
        Console.WriteLine("Name: " & myPet.Name) 'Name: Killer
 
        'We can still take this lower object, and convert it back up to a higher object.
        Dim newGuard As GuardDog = DirectCast(myPet, GuardDog)
        myPet = Nothing
        'His properties are still the same as before
        newGuard.patrol() 'Killer patrols The Garden, looking for intruders!
 
        Console.ReadLine()
 
    End Sub
 
End Module
 
Public Class Pet
    Public Name As String
    Public IsAlive As Boolean
End Class
 
Public Class Dog
    Inherits Pet
 
    Public Sub Bark()
        Console.WriteLine(Me.Name & " Barks!")
    End Sub
End Class
 
Public Class GuardDog
    Inherits Dog
 
    Public Location As String
 
    Public Sub patrol()
        Console.WriteLine(Me.Name & " Patrols " & Location & ", looking for intruders!")
    End Sub
End Class

This can be quite handy when passing objects back and forth between forms. If form a is expecting a type of pet, but you have a type of GuardDog, you can send it on fine. When the object is sent back to form b, you can convert it back up to the GuardDog type, in order to regain access to those object methods/properties.

.NET , , , ,

Of Webservices and Reporting Services

January 1st, 2009

I discovered an annoying issue a few months back, thought it might be useful to post it for people.

Its possible to set up MS Reporting services reports to get their data from a webservice call, rather than a direct call to the DB. This can be particularly handy if you want to perform various ‘in-code’  calculations before displaying this information in the report. The method itself is fairly straightforward (and available elsewhere - I can post it if there is a requirement, but Im not at a machine with examples at this moment), and works perfectly… so long as you dont try to pass any parameters.

If you do try, you may well find that although the function is called, the parameters are never passed correctly. After many hours of struggling with this, I finally came across an obscure post somewhere on the web (I forget where, so apologies for not giving appropriate credit), which detailed the answer.

The issue is so simple, and obscure, that you would never think to consider it. When a new webservice is created, it sets up a default namespace. This namespace has a trailing forward slash

System.Web.Services.WebService(Namespace:="http://tempuri.org/")

Its this trailing forward slash that is causing the issue. Remove the slash (you need to do this even if the namespace is not the default), and it works perfectly

System.Web.Services.WebService(Namespace:="http://tempuri.org")

I thought I should possibly post this so that it might save the hassle for other people, that I suffered.

.NET , , , ,

Happy New Year

January 1st, 2009

Happy New Year everyone. Hope you had a good one!

Fergal

General

Downtime

December 31st, 2008

After a looooong downtime, I finally brought my server back up. I’ll try to add more entries when I can.

General

Handling Null Values (Part Two)

August 11th, 2008

During my studies whilst over in Baku for work, I came onto the topic of Generics in .Net. I wont bore you with all the details of Generics here, but one of the interesting features of it, is that if you assign a generic variable a value of nothing when the generic is something like a date, or an integer, you get a default value for that type.

This got me thinking about whether I could compress the whole class from my previous Blog into just a single generic Function. I believe I can! See the code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    Public Class HandleNullValues
 
        Private Sub New()
            'Does nothing. Set to private to prevent instantiation of the class.
        End Sub
 
        Public Shared Function CheckNull(Of T)(ByVal obj As Object) As T
            If IsDBNull(obj) Then
                Dim ret As T = Nothing
 
                Return ret
            Else
                Return CType(obj, T)
            End If
        End Function
 
        Public Function DefaultValue(Of T)() As T
            Dim ret As T = Nothing
            Return ret
        End Function
    End Class

As you can see, this class consists of just two methods (ignoring the ‘new’ method). The core of the class is the CheckNull Function. This will compare the object and return the objects value if there is one. If it is null, it returns nothing (for objects) or the default value of the type (for things like dates/integers/boolean/etc).

There is a proviso to this function, however. The usage is not necessarily the most clear cut. You need to use the syntax below:

Dim myVal as Integer
Dim obj as new object
 
myVal = CheckNull(of Integer)(obj)

As you can see, you need to specify the generic type in brackets after the Function name, but before the parameters list.

The second Function, DefaultValue, takes no parameters (although you still need to specify the generic type as above). This Function will return the default value of the given type. I’ve only really provided it so that you can see what will be returned if the object is null. Remember that, this may or may not be the same as a valid object value (ie, if the object holds an integer value of 0, then this is the same as a null object)

.NET , , , , ,

WordPress Loves AJAX