2

I'm wondering how to get a Public Property from an external script. The test code is below and and it seems to display an empty variable for tValue using this method.

Is there something I'm not doing here?

'External Code
Set nObj = New Test
Response.Write(nObj.tValue)

'The Class
Class Test
    Public Test1

    Public Property Get tValue
        tValue = Test1
    End Property

    Sub Loadit
        Test1="123"
    End Sub
End Class
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Control Freak
  • 12,965
  • 30
  • 94
  • 145

2 Answers2

2

The name of your constructor is incorrect. The default name of the constructor of a class in classic ASP is Class_Initialize. Based on that, your code should be:

'External Code
Set nObj = New Test
Response.Write("tValue = " & nObj.tValue)

'The Class
Class Test
    Private Test1

    Public Property Get tValue
        tValue = Test1
    End Property

    Public Sub Class_Initialize
        Test1 = "123a"
    End Sub 
End Class

Or if you wish to keep the code of your class as-is, you should change your external code to:

'External Code
Set nObj = New Test
nObj.Loadit
Response.Write(nObj.tValue)
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Nevermind, Its because it wasn't calling the Sub first.

This fixed it.

 'External Code
 Set nObj = New Test

 Call nObj.Loadit

 Response.Write(nObj.tValue)
Control Freak
  • 12,965
  • 30
  • 94
  • 145