Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for including class files

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I created a .cls file and included it in my html page like this (for our intranet site not our internet site):

<SCRIPT language=vbscript src="../My_Library/My_Class.cls">
</SCRIPT>


The class is initialized like this:

Code:
Private Sub Class_Initialize()

    Set mobjRootDSE = GetObject("LDAP://rootDSE") ' bind to the rootDSE for portability
End Sub

All is well as long as i open the page like this:
\\servername\path\My_Page.htm

However, it fails when I open the page like this:

My vbscript within the html page declares my class like this:
Dim AD
Set AD = New clsMy_Class


I receive the error "variable undefined clsMy_Class" upon executing the set statement.

Consequently, I don't understand why launching in IE using a syntax like this (\\sever\path...) works and launching it like this ( doesn't.

Can someone explain the syntax I should be using to include the class file so it works either way? I hate to hard code the full path because I will have to remember to change it when I move the code from our test site to our live site.
 
I think it is mainly how you script your class and how it looks like the My_Class.cls. Can you show the sketch of it?
 
I stripped the class down to what was needed for the page that was being opened. Note that instead of clsMy_Class it's clsActiveDirectory.
Code:
Option Explicit

Class clsMMC_ActiveDirectory

'*************************
'*  Public Declarations  *
'*************************

    Dim mstrsAMAccountName
    Dim mobjUser
    Dim mobjRootDSE 

'**********************************
'*  Class Initialize / Terminate  *
'**********************************

Private Sub Class_Initialize()

    Set mobjRootDSE = GetObject("LDAP://rootDSE")
End Sub

Private Sub Class_Terminate()
    Set mobjUser = Nothing
    Set mobjRootDSE = Nothing
End Sub


Function IsMemberOfGroup(strGroupName, varUserName)
  Dim grp
  Dim strPath
	Dim strUserName
	
	if (IsNull(varUserName)) then
		strUserName = mstrsAMAccountName
	else
		strUserName = varUserName
	end if
	
  strPath = "WinNT://MyDomain/"
  Set grp = GetObject(strPath & strGroupName & ",group")

  IsMemberOfGroup = grp.IsMember(strPath & strUserName)

End Function

Property Get RootDSE()
    RootDSE = mobjRootDSE.Get("defaultNamingContext")
End Property

Property Get DistinguishedName(varsAMAccountName)

    Dim objConnection
    Dim objCommand
    Dim objRecordSet

    Dim strUserName

    if (IsNull(varsAMAccountName)) then strUserName = UserName else strUserName = varsAMAccountName

    Set objConnection = CreateObject("ADODB.Connection")
    objConnection.Provider = ("ADsDSOObject")
    objConnection.Open

    Set objCommand = CreateObject("ADODB.Command")
    objCommand.ActiveConnection = objConnection
    objCommand.CommandText = "<LDAP://" & RootDSE & ">;(&(objectCategory=User)" & "(samAccountName=" & strUserName & "));distinguishedName,sAMAccountName,name;subtree"

    Set objRecordSet = objCommand.Execute

    If objRecordSet.RecordCount = 0 Then
        Set objRecordSet = Nothing
        objConnection.Close
        Set objConnection = Nothing

        err.Raise 2000,"DistinguishedName",strUserName & " does not exist in Active Directory."

    Else

        DistinguishedName = objRecordSet.Fields("distinguishedName")
        Set objRecordSet = Nothing
        objConnection.Close
        Set objConnection = Nothing

    End If


End Property


Property Let UserName(strUserName)

    mstrsAMAccountName = strUserName

    Set mobjUser = GetObject("LDAP://" & DistinguishedName(mstrsAMAccountName) & "")

End Property

Property Get UserName()
    UserName = mstrsAMAccountName
End Property

End Class
 
[1] Just want to have you confirm that the file name being My_Class.cls and you script actually it instantiate with
[tt] Set AD = New clsMMC_ActiveDirectory[/tt]
rather than My_Class. (I think that's what you have meant, though).

[2] When, inside the script section, you put up the _bare_ statements like set AD=new clsMMC_ActiveDirectory, as it seems to be so, risks are the whole script is not fully load. You do it this way.
[tt]
dim AD
sub startdoingthing
Set AD = new clsMMC_ActiveDirectory
'etc
end sub
set window.onload=getref("startdoingthing")
[/tt]
These are my comments without looking into the detail of the sketch, as the error occurred at the set statement already before you doing anything material.

 
[1'] After re-read your post, your file name might be clsActiveDirectory. In any case, the file name is not important. It is the class name that is important. And, the new keyword take up the class name, _not_ the file name.
 
In my original post, to make things simple, I called it My_Class.cls. The name of the actual file is MMC_ActiveDirectory.cls. You have it basically correct. To make it simple, it looks basically like this:
Code:
<SCRIPT language=vbscript src="..\Afolder\MMC_ActiveDirectory_Class.cls">
</SCRIPT>

<SCRIPT language=vbscript>

	Option Explicit

        Const gcintGroup_User  = 900
        Const gcintGroup_Mgr   = 950
        Const gcintGroup_Help  = 980
        Const gcintGroup_Admin = 1000

        Dim gintGroup

Sub cmdOK_onclick()
    Call GetGroup("AUsersName")
End Sub

Sub GetGroup(strNTUserName)

	Dim ADir			'ActiveDirectory object
	
	set ADir = new clsMMC_ActiveDirectory

	ADir.username = strNTUserName

	If (ADir.IsMemberOfGroup("WebMaster", Null)) then
		gintGroup = gcintGroup_Admin
	elseif (ADir.IsMemberOFGroup("HeatWeb Admins", Null)) then
		gintGroup = gcintGroup_Help
	elseif (ADir.IsMemberOfGroup("Department Managers", Null)) then
		gintGroup = gcintGroup_Mgr
	else
		gintGroup = gcintGroup_User
	end if

End Sub
</SCRIPT>
 
If all fall in place, the problem would have to be credential problem. It would be the line of getobject at the class_initialize. Change the html extension to hta and try again with admin logon.
 
I'm back!! I had put this issue on the back burner but now am faced with dealing with it. I tried your last suggestion of changing it to an hta extension, but no luck. Every class I create I have the same issue.

The following class' file name is MMC_TabControl.cls. I have since stripped everything out of the class so it now looks like this:
Code:
Class clsMMC_TabControl

	Private Sub Class_Initialize()
	End Sub

	Private Sub Class_Terminate()
	End Sub


End Class
In my html code I include and reference it like this:
Code:
<SCRIPT language=vbscript src="..\MyLibrary\MMC_TabControl.cls"></SCRIPT>

<SCRIPT language=vbscript>

	Dim mobjTab

sub window_onload()

	Set mobjTab = New clsMMC_TabControl

end sub

Again, the problem is that if I launch the page like this
\\servername\path\My_Page.htm it works.

However, if I launch the page like this it doesn't work and I get the err message "variable undefined clsMMC_TabControl" upon executing the set statement.

Obviously, if I include the .cls file using an absolute address, it will work either way. But I prefer to user the Relative address.

Also note that I have several other files that I include without problems that have extension of .vbs. Only the ones with .cls cause problems.

I don't understand???
 
Do I have to set something in the Web Server Extensions via the IIS Manager?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top