tags:

views:

42

answers:

2

I've the following class which is a singleton implementation:

Imports Microsoft.VisualBasic
Imports System.Xml


Public Class GlobalController

    Private Shared instance As GlobalController
    Private ControlsXmlDoc As XmlDocument
    Private xmldocpath As String

    Sub New()
        ControlsXmlDoc = New XmlDocument
        xmldocpath = HttpContext.Current.Server.MapPath("~/cp/GlobalControl.xml")
        ControlsXmlDoc.Load(xmldocpath)
    End Sub

    Shared Function GetInstance() As GlobalController
        If instance Is Nothing Then
            Return New GlobalController
        Else
            Return instance
        End If
    End Function

    Shared Property IsExtracting() As Boolean
        Get
            Return Boolean.Parse(GetInstance.ControlsXmlDoc.SelectNodes("global/extraction/proceed").Item(0).InnerText)
        End Get
        Set(ByVal value As Boolean)
            HttpContext.Current.Application.Lock()
            Dim node As XmlNode = GetInstance.ControlsXmlDoc.SelectNodes("global/extraction/proceed").Item(0)
            If Not Boolean.Parse(node.InnerText) = value Then
                node.InnerText = value.ToString
                node.Normalize()
                SaveDocument()
                GetInstance.ControlsXmlDoc.Load(GetInstance.xmldocpath)
            End If
            HttpContext.Current.Application.UnLock()
        End Set
    End Property

    Shared Sub SaveDocument()
        GetInstance.ControlsXmlDoc.Save(GetInstance.xmldocpath)
    End Sub

End Class

In my page I am doing something like this:

GlobalController.IsExtracting = False
Response.Write(GlobalController.IsExtracting)

I am always getting the output as "true". What is wrong with the code?

+1  A: 

According this link Operator precedence and associativity, ! (or vb.net Not) have greater priority than == (= in VB.NET); so, your expression is always evaluated as

Not(True) And False

and never enters that If statement.

Try to use Boolean.Parse(node.InnerText) != value or Not (Boolean.Parse(node.InnerText) = value) in order to get correct result.

Rubens Farias
A: 

All, thanx for ur answers. I apologize for what I am about to say. I found the bug: it was with the way I implemented the singleton. Forgot to assign the newly created object instance to the shared variable.

Shared Function GetInstance() As GlobalController 
    If instance Is Nothing Then 
        instance = New GlobalController 
    End If 
    Return instance
End Function
deostroll

related questions