2012-01-09 71 views
5

Necesito usar estructura de clase en asp clásico. He escrito siguientes tres clases.Estructura de clase en asp clásico

category.asp

<% 

Class Category 

    Private NameVar 

    Public Property Get Name() 
     Name = NameVar 
    End Property 

    Public Property Let Name(nameParam) 
     NameVar = nameParam 
    End Property 

End Class 

%> 

Item.asp

<% 

Class Item 

    Private NameVar 
     Private CategoryVar 

    Public Property Get Name() 
     Name = NameVar 
    End Property 

    Public Property Let Name(nameParam) 
     NameVar = nameParam 
    End Property 

    Public Property Get Category() 
     category = categoryVar 
    End Property 

    Public Property Let Category(categoryParam) 
    CategoryVar = categoryParam 
    End Property 

End Class 

%> 

Test.ASP

<% 

    Dim CategoryVar 
    Set CategoryVar = New Category 

    CategoryVar.Name = "Weight" 

    Dim ItemVar 
    Set ItemVar = New Item 

    ItemVar.Name = "kg" 
    ItemVar.Category = CategoryVar 

%> 
<html> 
    <head> 
     <title>UoM Componet Testing</title> 
    </head> 
    <body> 
     <%= ItemVar.Name %><br/> 
    </body> 
</html> 

Cuando ejecuto el código, me he encontrado algún problema El error es:

Microsoft VBScript en tiempo de ejecución (0x800A01B6) El objeto no admite esta propiedad o método:.? 'CategoryVar'"

¿Cómo se puede explicar esto por favor me ayude

+0

¿Usted intentó 'Set ItemVar.Category = CategoryVar'? –

+0

He reproducido tu error, ¡es raro! – Rafael

Respuesta

8

En VBScript, si se sabe que una propiedad contendrá una referencia de objeto, debe definirlo mediante la instrucción Property Set. Además, debe usar la instrucción Set al asignar referencias de objeto a variables. Con esto en mente, los siguientes cambios deben hacerse:

Item.asp

Class Item 

    '<snip> 

    Public Property Get Category() 
     ' Add Set here 
     Set category = categoryVar 
    End Property 

    ' Change "Property Let" to "Property Set" 
    Public Property Set Category(categoryParam) 
     Set CategoryVar = categoryParam 
    End Property 

End Class 

Test.ASP

<% 
    ' <snip>  

    ItemVar.Name = "kg" 
    Set ItemVar.Category = CategoryVar 

%> 
0

I. han probado su código de arriba y, mientras me hizo llegar un error, que no es el que usted ha mencionado.

en primer lugar está definiendo Name dos veces en la clase Item, uno debe ser Item. se condly, asigne ItemVar.Category = CategoryVar pero la clase Item no tiene propiedad Category.

Aquí está el código que utiliza para probar y funciona bien:

<% 
    Class Category 
     Private NameVar 

     Public Property Get Name() 
      Name = NameVar 
     End Property 

     Public Property Let Name(nameParam) 
      NameVar = nameParam 
     End Property 
    End Class 

    Class Item 
     Private NameVar 
     Private ItemVar 

     Public Property Get Name() 
      Name = NameVar 
     End Property 

     Public Property Let Name(nameParam) 
      NameVar = nameParam 
     End Property 

     Public Property Get Item() 
      Item = ItemVar 
     End Property 

     Public Property Let Item(itemParam) 
      ItemVar = itemParam 
     End Property 
    End Class 

    Dim CategoryVar 
    Set CategoryVar = New Category 

    CategoryVar.Name = "Weight" 

    Dim ItemVar 
    Set ItemVar = New Item 

    ItemVar.Name = "kg" 
    'ItemVar.Category = CategoryVar ' There is no 'Category' property in your class 
%> 

<html> 
    <head> 
     <title>UoM Componet Testing</title> 
    </head> 
    <body> 
     <%= ItemVar.Name %><br/> 
    </body> 
</html> 
+1

FYI, la pregunta ha sido editada desde que se publicó esta respuesta. El error tipográfico que mencionas ya no está en la pregunta. –

0

me encontré con los mismos problemas que Rory, redefinición de nombre, y no habiendo estructura categoría.

He ajustado el código siguiente para tener en cuenta la cuestión Editado:

¿No debería ser algo así como más adelante?

Class Item  
    Private NameVar 
    Public Property Get Name()   
     Name = NameVar  
    End Property  
    Public Property Let Name(nameParam)   
     NameVar = nameParam  
    End Property  

    Private CategoryVar 
    Public Property Get Category()   
     Category = CategoryVar  
    End Property  
    Public Property Let Category(CategoryParam)   
     CategoryVar = CategoryParam  
    End Property 
End Class 




Dim CategoryVar  
Set CategoryVar = New Category  
CategoryVar.Name = "Weight"  

Dim ItemVar  
Set ItemVar = New Item  
ItemVar.Name = "kg"  
ItemVar.Category = CategoryVar.Name 
+0

He actualizado mi pregunta. – zanhtet