Solved

URGENT! - Using Shawdow variables in VB.NET Classes

Posted on 2003-12-03
6
235 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

806 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