Foros del Web » Programando para Internet » ASP Clásico »

Constructor de clase en ASP

Estas en el tema de Constructor de clase en ASP en el foro de ASP Clásico en Foros del Web. Creanlo o no, no he podido encontrar en ningun lado la sintaxis para crear un bendito constructor de clase en ASP (el viejito) mi clase ...
  #1 (permalink)  
Antiguo 12/06/2007, 12:40
 
Fecha de Ingreso: junio-2007
Mensajes: 18
Antigüedad: 17 años
Puntos: 0
Constructor de clase en ASP

Creanlo o no, no he podido encontrar en ningun lado la sintaxis para crear un bendito constructor de clase en ASP (el viejito)

mi clase se llama Computadora

necesito saber como declarar el constructor para inicializar variables.

Gracias!!
  #2 (permalink)  
Antiguo 12/06/2007, 13:03
 
Fecha de Ingreso: junio-2007
Mensajes: 9
Antigüedad: 17 años
Puntos: 0
Re: Constructor de clase en ASP

hola:
tambien me vi en esas circunstancias hace un tiempo atras. no encontre la forma de declarar un constructor, hasta donde yo se no se puede, lo que hice fue declarar un procedimiento (Inicializar) en la clase, donde inicializo las variables que necesito.

ej.:
set ObjComputador = new Computador
ObjComputador.Inicializar

fue la unica solucion que encontre en el momento.
ojala te sirva de algo. seguire buscando como declarar un constructor ya que tambien lo necesito.
  #3 (permalink)  
Antiguo 12/06/2007, 13:07
Avatar de u_goldman
Moderador
 
Fecha de Ingreso: enero-2002
Mensajes: 8.031
Antigüedad: 22 años, 5 meses
Puntos: 98
Re: Constructor de clase en ASP

Existe el evento Initialize de la clase en VBS, que podría ser visto como un constructor:

http://msdn.microsoft.com/library/de...9b1146d97f.asp
__________________
"El hombre que ha empezado a vivir seriamente por dentro, empieza a vivir más sencillamente por fuera."
-- Ernest Hemingway
  #4 (permalink)  
Antiguo 12/06/2007, 13:21
Avatar de Myakire
Colaborador
 
Fecha de Ingreso: enero-2002
Ubicación: Centro de la república
Mensajes: 8.849
Antigüedad: 22 años, 4 meses
Puntos: 146
Re: Constructor de clase en ASP

efectivamente, ejemplo:

Código:
....
    'Constructor
    Private Sub Class_Initialize()
       HoraAcceso = Now()
			 set objDicPantallas = Server.CreateObject("Scripting.Dictionary") 
    End Sub
    'Destructor		
    Private Sub Class_Terminate()
        Set objDicPantallas = Nothing
    End Sub
....
  #5 (permalink)  
Antiguo 12/06/2007, 14:43
 
Fecha de Ingreso: junio-2007
Mensajes: 9
Antigüedad: 17 años
Puntos: 0
Re: Constructor de clase en ASP

oye Myakire:

entonces como seria:

tienes que instanciar a "Class_Initialize" y a "Class_Terminate", o se instancian cuando pones "new" o "nothing" respèctivamente?
  #6 (permalink)  
Antiguo 12/06/2007, 15:17
Avatar de Myakire
Colaborador
 
Fecha de Ingreso: enero-2002
Ubicación: Centro de la república
Mensajes: 8.849
Antigüedad: 22 años, 4 meses
Puntos: 146
Re: Constructor de clase en ASP

Funcionan como un constructor y destructor normalitos .... es decir, no tienes que invocar en el objeto instanciado a esos métodos, ellos se lanzan al momento del instanciamiento y su destrucción
  #7 (permalink)  
Antiguo 28/12/2009, 04:51
 
Fecha de Ingreso: noviembre-2008
Ubicación: Tenerife
Mensajes: 6
Antigüedad: 15 años, 7 meses
Puntos: 0
Respuesta: Constructor de clase en ASP

Buenas.

Tengo una duda en referencia al Class_Initialize. Necesito que cuando se haga el llamado al contructor de la clase, se le pasen parámetros a la misma, para poder inicializar unos atributos de dicha clase, por ejemplo:


index.asp

Código:
...

Set prueba = New Pepito("hola", "adios")

...

pepito.asp

Código:
Class Pepito
  
  ' Atributos de la clase
  '--------------------------------------
  Private variable1
  Private variable2


  ' Constructor
  '-------------------
  Private Sub Class_Initialize(ByVal str_variable1, ByVal str_variable2)
   
    variable1 = str_variable1
    variable2 = str_variable2

  End Sub

End Class

Lo he probado y me lanza el siguiente error:

La inicialización o finalización de la clase no tiene argumentos

Con lo cual creo que no es posible pasarle parámetros al contructor, entonces no se si me pueden aconsejar en algo, ya que lo unico que se me ocurre es crear un metodo propio de la clase en la cual se inicialicen los valores, pero esto no lo veo muy eficiente que digamos .

Muchas gracias de antemano y un saludo
  #8 (permalink)  
Antiguo 28/12/2009, 13:45
Avatar de Myakire
Colaborador
 
Fecha de Ingreso: enero-2002
Ubicación: Centro de la república
Mensajes: 8.849
Antigüedad: 22 años, 4 meses
Puntos: 146
Respuesta: Constructor de clase en ASP

Solo para futuras referencias, antes de revivir un tema antiguo verifica que lo vas a hacer con información que ayude a resolver el problema original, en este caso no se da esa condición, por lo que el tema debiera ser cerrado. Lo ideal seria abrir un tema propio con tu duda en particular e incluyendo la liga a este o a los temas que quieras utilizar como referencia.

Aunque solo por que la referencia estaba a la mano, te colocare lo que dice la MSDN al respecto:

Cita:
Parameterized Constructors
You're probably aware that support for object initialization in earlier versions of Visual Basic has been very limited. In Visual Basic 6.0, for example, your only means for controlling object initialization is to add a custom Class_Initialize method to your class. Let's quickly review what happens when you do this.
When you supply a custom implementation of Class_Initialize, the Visual Basic runtime is guaranteed to call this method whenever a client creates an object from your class. However, this style of initialization isn't very flexible because the Class_Initialize method cannot take parameters. Since the Class_Initialize method cannot take parameters, there's no way for the client (that is, the object creator) to pass instance-specific data when creating an object. Therefore, the only thing that can be initialized inside Class_Initialize are data that have the same values for every instance of the class.
Many experienced developers using Visual Basic found that the best way to work around the limitations of Class_Initialize was to add a public method designed exclusively for object initialization. For example, you can add a public method named Init to your class and give it the parameters that are necessary to properly initialize an object. When a class includes a public method for initialization, the client has the responsibility of calling this method after creating a new object. Here's an example of a class that is based on this type of design approach.
Class Person
Private name As String
Private age As Integer
Public Sub Init(n As String, a As Integer)
name = n
age = a
End Sub
End Class

Given a class definition such as this, the client should call the Init method immediately after creating the object. Here's an example of what the client code usually looks like.
Dim obj As New Person
obj.Init("Bob", 32)
' now access other members
A design technique such as the one I've just shown is often called two-phase construction. The object is created in phase one and it's initialized in phase two. It's important to keep in mind that designs that use two-phase construction place a responsibility on the programmer writing client-side code against the class. It is incorrect for client code to access any other member defined inside the class before calling Init. If the client-side programmer forgets to call Init, the class code will probably not behave as the author intended. It's unfortunate, but there's nothing you can do as a class author to make the compiler force a client-side programmer to call the Init method.
The introduction of parameterized constructors provides a much more elegant solution to the problem that I've just described. Let's take a look at how to redesign the Person class using this new feature.
http://msdn.microsoft.com/en-us/magazine/cc301735.aspx

Saludos
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Tema Cerrado




La zona horaria es GMT -6. Ahora son las 18:36.