Solved

vbscript object oriented class within class

Posted on 2014-12-21
8
150 Views
Last Modified: 2015-04-14
OK, so my questions is probably really a general oo question but I will ask it here for vbscript.  (I know it is a bad language but I have to use it)

I have a log class CLogger, you can find more: https://github.com/miyamiya/VBSdoc/blob/master/CLogger.vbs


I have a GenericSystem class:

Class GenericSystem
        .....
	'' Constructor
	Private Sub Class_Initialize()
             Set myLogger = New CLogger
             .........
	End Sub
        .....
End Class

Open in new window


I have sub class called PCSystem...

How do I call myLogger in the subclass? It would be helpful to only call the cLogger in the GenericSystem rather than have to recreate it in every subclass....


Thanks!
0
Comment
Question by:adamc817
  • 4
  • 4
8 Comments
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40511938
You cant directly call a member of a parent class in a sub class.

What you can do is make a CLogger property in the sub class, and pass a reference to it when you define the sub class in the parent class.

Class GenericSystem
        .....
	'' Constructor
	Private Sub Class_Initialize()
             Set myLogger = New CLogger
             mySubClass.CLogger = myLogger

             .........
	End Sub
        .....
End Class

Open in new window

0
 

Author Comment

by:adamc817
ID: 40511990
Thanks for the quick response...

That sounds perfect!

One question does mySubClass need to be defined in the constructor as well?
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40512033
It needs to be a property, or a private variable within the class. If its defined in the constructor then it will only be available in the constructor.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:adamc817
ID: 40512182
ok, so I have this:

Class GenericSystem
	'' Properties
	Private p_strName
	Private PCSystem
	
	'' Constructor
	Private Sub Class_Initialize()
		....
		Set p_vbLogger = New CLogger
 		PCSystem.CLogger = p_vbLogger

	End Sub

End CLass

Open in new window


But how do I call that in my subClass...

Class PCSystem
	'' Properties
	Private CLASSSYSTEM

	'' Constructor
	Private Sub Class_Initialize()
		Set CLASSSYSTEM= new GenericSystem

		
	End Sub


End Class

Open in new window



THANKS!
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40513268
In Class PCSystem you need a public property for the logger, otherwise this line

PCSystem.Clogger = p_vbLogger

will crash

Try

Public CLogger as CLogger

After the class declaration in the subclass
0
 

Author Comment

by:adamc817
ID: 40513589
this does not seem to be helping....

I tried that line many different places without success.

I finally tried:

      Public Property Let CLogger(in_vbLogger)
            CLASSSYSTEM.p_CLogger = in_vbLogger
      End Property

and:
         Public pc_CLogger

In the PCSystem class but this failed as well.....

Can you be more specific?
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
ID: 40514989
You declare a property like this in vbscript

Private m_logger as C_Logger
    ' Logger property.
    Public Property Get Logger()
        Logger = m_Logger
    End Property

    Public Property Let Logger(value)
        m_logger = value
    End Property

Open in new window

0
 

Author Closing Comment

by:adamc817
ID: 40724401
sure why not
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

776 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