Solved

URGENT! - Using Shawdow variables in VB.NET Classes

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

929 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

10 Experts available now in Live!

Get 1:1 Help Now