2012-01-09 10 views
5

Potrzebuję użyć struktury klasycznej w klasycznym asp. Napisałem następujące trzy klasy.Struktura klasy w klasycznym asp

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> 

Kiedy uruchomić ten kod, ja znalazłem jakiś problem. Błąd jest:

Microsoft VBScript Runtime (0x800A01B6) Obiekt nie obsługuje tej właściwości lub metody.? CategoryVar '”

Jak można wyjaśnić to proszę mi pomóc

+0

Próbowałaś 'Ustaw ItemVar.Category = CategoryVar'? –

+0

Powtórzyłem twój błąd, to dziwne! – Rafael

Odpowiedz

8

W VBScript, jeśli wiesz, że nieruchomość będzie zawierać odwołanie do obiektu, należy go określić za pomocą instrukcji Property Set. Ponadto podczas przypisywania referencji do zmiennych należy użyć instrukcji Set. Mając to na uwadze następujące zmiany muszą być dokonane:

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

ja. „ve przetestowaniu powyższy kod, a jednocześnie zrobiłem się błąd, to nie jest jeden pan wspomniał.

Przede wszystkim definiowania Name dwukrotnie w klasie Item, trzeba być Item. Se Przypisz, przypisujesz ItemVar.Category = CategoryVar, ale klasa Item nie ma właściwości Category.

Oto kod I wykorzystywane do testowania i działa prawidłowo:

<% 
    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, pytanie zostało zmodyfikowane od czasu opublikowania tej odpowiedzi. Wspomniana literówka nie jest już w pytaniu. –

0

I napotkał te same problemy jak Rory, przedefiniowanie jej nazwy, a nie ma przy własność kategoria.

Mam dostosować poniższy kod, aby uwzględnić kwestię edycja:

Jeżeli to nie będzie coś jak poniżej?

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

Zaktualizowałem moje pytanie. – zanhtet

Powiązane problemy