Solved

URGENT! - Using Shawdow variables in VB.NET Classes

Posted on 2003-12-03
6
233 Views
Last Modified: 2013-11-26
I created a class that wraps an XML node.  The class is named Human because it is used to wrap many different person nodes in my XML document.  Example:

<Approver Name="Bob" ActionDate="2003/12/03" SomethingElse="Blah Blah"/>
<Performer Name="sam" ActionDate="2003/11/03" SomethingElse="Blah Blah"/>
<Creator Name="ralph" ActionDate="2003/1/03" SomethingElse="Blah Blah"/>

I want to use the Human class but I need to change it's name.  I tried the Shadow keyword in both the Human class and the higher classes, but that doesn't work, I can't set the name in the higher class without explicitly setting the name a run-time (which is sloppy).

In my project, each class is constructed such:
Class clsApprover
  inherits Human
  Shadows HumanName as String = "Approver"
End Class

Class Human
  Shadows HumanName as String
 
'Check for parent node and thisNode, create if necessary
   Private Function HasNode() As Boolean
      If IsNothing(pNode.GetElementsByTagName(HumanName).Item(0)) Then
        thisNode = xmlALN.CreateElement(HumanName)
        pNode.AppendChild(thisNode)
        Return True
      Else
        thisNode = pNode.GetElementsByTagName(HumanName).Item(0)
      End If
    End Function


Any idea how I can do this efficiently?

Thanks,
Jeffrey
0
Comment
Question by:OgilbeeJ
  • 3
  • 2
6 Comments
 

Author Comment

by:OgilbeeJ
ID: 9868129
I discovered a way to do what I'm looking for but it's sloppy and I just know there's a better way.

  Public Class clsCreator
    Inherits clsHuman
    Friend Overrides Property HumanName() As String
      Get
        Return MyBase.HumanName
      End Get
      Set(ByVal Value As String)
        MyBase.HumanName = Value
      End Set
    End Property
    Public Sub New()
      HumanName = "Creator"
    End Sub
  End Class

  Public Class clsHuman
    'parent node is the node that will be checked for the child
    Public pNode As System.Xml.XmlElement
    Private thisNode As System.Xml.XmlElement
    Private hName As String = ""
    Friend Overridable Property HumanName() As String
      Get
        Return hName
      End Get
      Set(ByVal Value As String)
        hName = Value
      End Set
    End Property
    Private Function HasNode() As Boolean
      If pNode Is Nothing Then Return False
      If IsNothing(pNode.GetElementsByTagName(hName).Item(0)) Then
        thisNode = xmlALN.CreateElement(hName)
        pNode.AppendChild(thisNode)
        Return True
      Else
        thisNode = pNode.GetElementsByTagName(hName).Item(0)
        Return True
      End If
    End Function
...
End Class

Is there a better way?  

Also, is there any way to use XPath with a SmartDevice project in VB.NET?  I am really comfortable with the MSXML2 SelectSingleNode(xPath) and SelectNodes(xPath) functions, but System.XML doesn't seem to support that methodology.  Should this be a seperate post or can someone give me a quick example?


0
 
LVL 9

Expert Comment

by:malharone
ID: 9871694
no .. your class definition seem alright ... and yes you're right .. you "inherit" the class ... not "shadow" it.
0
 

Author Comment

by:OgilbeeJ
ID: 9878211
Thanks malharone, but I just want to believe there is a way to inherit a class and set default properties without overriding.  

Why doesn't the following code work?
 Public Class clsCreator
    Inherits clsHuman
    MyBase.HumanName = "Creator"
  End Class
0
 
LVL 9

Accepted Solution

by:
malharone earned 500 total points
ID: 9878798
okay ... sorry ...
from your post:
 Public Class clsCreator
    Inherits clsHuman
    MyBase.HumanName = "Creator"     '<-- if you're doing this w/o instantiating parent object , then you're in trouble
  End Class





'**************************************************************
'i'm using MustInherit if you want this class not to
'be directly instantiated, but only indirectly through a
'derived class .. but depending upon your application, you may
'wish to remove it
'**************************************************************
Public MustInherit Class clsHuman
    Public pNode As System.Xml.XmlElement
    Private thisNode As System.Xml.XmlElement
    Private hName As String = ""

    '**************************************************************
    'protected means all inherited classes have access to this
    'If you change 'protected' to 'public' not only will the derived classes
    '   have access to it, but also all other classes that have the objects
    '   of type clsHuman or any other class deriving clsHuman
    'check out http://visualbasic.about.com/library/blinheritancea.htm
    ' for further clarification
    '**************************************************************
    Protected Property HumanName() As String
        Get
            Return hName
        End Get
        Set(ByVal Value As String)
            hName = Value
        End Set
    End Property
    Private Function HasNode() As Boolean
        'your code
    End Function
    Public Sub New()
        'your initialization code
    End Sub
End Class
Public Class clsCreator
    Inherits clsHuman

    Public Sub New()
        MyBase.New()
        '**************************************************************
        ' you should always call MyBase.New for inheriting classes
        '**************************************************************

        '**************************************************************
        'it's also a good practice to be as explicit as possible
        'e.g. using MyBase.HumanName instead of HumanName
        '**************************************************************
        MyBase.HumanName = "Creator"
    End Sub
End Class
0
 

Author Comment

by:OgilbeeJ
ID: 10254740
DanRollins --> Yes, mahoarones answer was very helpful.  I appologize for not responding sooner, I got pulled on to another project and forgot to check back here.  
malharone --> Thanks for the snipit, your help is appreciated.

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now