Foros del Web » Programación para mayores de 30 ;) » .NET »

Incluir una clase

Estas en el tema de Incluir una clase en el foro de .NET en Foros del Web. Hola, estoy experimentando con clases, como hago esto, me sale error siempre... Código HTML: <script runat= "server" > Public Class Bicicleta Public Modelo as String ...
  #1 (permalink)  
Antiguo 10/08/2005, 18:42
 
Fecha de Ingreso: julio-2005
Mensajes: 58
Antigüedad: 18 años, 9 meses
Puntos: 0
Incluir una clase

Hola, estoy experimentando con clases, como hago esto, me sale error siempre...

Código HTML:
<script runat="server">

Public Class Bicicleta 
    Public Modelo as String 
    Public Precio as Double 
    Public NumeroDeVelocidades as Integer 
    Private Velocidad as Integer 

    Public Sub Acelerar(ByVal km As Integer) 
       Velocidad = Velocidad + km 
    End Sub 

    Public Sub Frenar() 
       If Velocidad > 0 Then 
          Velocidad = Velocidad -1 
       End If 
    End Sub 

    Public Function ConsutarVelocidad() As Integer 
       Return Velocidad 
    End Function 
End Class

Dim objBicicleta as Bicicleta = New Bicicleta 

Dim VelocidadActual as Integer 
objBicileta.Modelo = "Montaña" 
objBicicleta.Precio = 200 
objBicicleta.NumeroDeVelocidades = 21 

objBicicleta.Acelerar(5) 
objBicicleta.Frenar() 

VelocidadActual = objBicicleta.ConsultarVelocidad

Response.write VelocidadActual

</script> 
Gracias
  #2 (permalink)  
Antiguo 11/08/2005, 12:21
 
Fecha de Ingreso: julio-2005
Mensajes: 58
Antigüedad: 18 años, 9 meses
Puntos: 0
alguien sabe como lo tengo que hacer ?
  #3 (permalink)  
Antiguo 11/08/2005, 12:37
Avatar de RootK
Moderador
 
Fecha de Ingreso: febrero-2002
Ubicación: México D.F
Mensajes: 8.004
Antigüedad: 22 años, 2 meses
Puntos: 50
No le veo errores en tu clase mas que ortográficos......

Cita:
Dim objBicicleta as Bicicleta = New Bicicleta

Dim VelocidadActual as Integer
objBicicleta.Modelo = "Montaña" 'te faltaba la "c"
objBicicleta.Precio = 200
objBicicleta.NumeroDeVelocidades = 21

objBicicleta.Acelerar(5)
objBicicleta.Frenar()

VelocidadActual = objBicicleta.ConsutarVelocidad 'aqui también está mal escrito tu métod
Salu2
__________________
Nadie roba nada ya que en la vida todo se paga . . .

Exentrit - Soluciones SharePoint & Net
  #4 (permalink)  
Antiguo 12/08/2005, 10:33
 
Fecha de Ingreso: julio-2005
Mensajes: 58
Antigüedad: 18 años, 9 meses
Puntos: 0
GRacias!!!

gracias Rootk, ahora tengo esto, ya lo arregle...

Código HTML:
<script runat="server">

Public Class Bicicleta 
    Public Modelo as String 
    Public Precio as Double 
    Public NumeroDeVelocidades as Integer 
    Private Velocidad as Integer 

    Public Sub Acelerar(ByVal km As Integer) 
       Velocidad = Velocidad + km 
    End Sub 

    Public Sub Frenar() 
       If Velocidad > 0 Then 
          Velocidad = Velocidad -1 
       End If 
    End Sub 

    Public Function ConsultarVelocidad() As Integer 
       Return Velocidad 
    End Function 
End Class

Dim objBicicleta as Bicicleta = New Bicicleta 

Dim VelocidadActual as Integer 
objBicicleta.Modelo = "Montaña" 
objBicicleta.Precio = 200 
objBicicleta.NumeroDeVelocidades = 21 

objBicicleta.Acelerar(5) 
objBicicleta.Frenar() 

VelocidadActual = objBicicleta.ConsultarVelocidad()

Response.write VelocidadActual

</script>

Pero me arroja este error: ... espero me puedas ayudar, porque no encuentro la razon, muchas gracias

Código HTML:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30188: Declaration expected.

Source Error:

 

Line 25: 
Line 26: Dim VelocidadActual as Integer 
Line 27: objBicicleta.Modelo = "Montaña" 
Line 28: objBicicleta.Precio = 200 
Line 29: objBicicleta.NumeroDeVelocidades = 21 
 

Source File: c:\inetpub\wwwroot\pruebas\aspnet\test.aspx    Line: 27 



Show Detailed Compiler Output:


C:\WINNT\system32> "c:\winnt\microsoft.net\framework\v1.1.4322\vbc.exe" /t:library /utf8output /R:"c:\winnt\assembly\gac\system.enterpriseservices\1.0.5000.0__b03f5f7f11d50a3a\system.enterpriseservices.dll" /R:"c:\winnt\assembly\gac\system.data\1.0.5000.0__b77a5c561934e089\system.data.dll" /R:"c:\winnt\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll" /R:"c:\winnt\assembly\gac\system.web.mobile\1.0.5000.0__b03f5f7f11d50a3a\system.web.mobile.dll" /R:"c:\winnt\assembly\gac\system.xml\1.0.5000.0__b77a5c561934e089\system.xml.dll" /R:"c:\winnt\assembly\gac\system.web.services\1.0.5000.0__b03f5f7f11d50a3a\system.web.services.dll" /R:"c:\winnt\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll" /R:"c:\winnt\assembly\gac\system.web\1.0.5000.0__b03f5f7f11d50a3a\system.web.dll" /out:"C:\WINNT\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\root\2135a508\8d69a834\smtkbgrj.dll" /debug-  "C:\WINNT\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\root\2135a508\8d69a834\smtkbgrj.0.vb"


Microsoft (R) Visual Basic .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322.573
Copyright (C) Microsoft Corporation 1987-2002. All rights reserved.

c:\inetpub\wwwroot\pruebas\aspnet\test.aspx(27) : error BC30188: Declaration expected.

objBicicleta.Modelo = "Montaña" 
~~~~~~~~~~~~                    
c:\inetpub\wwwroot\pruebas\aspnet\test.aspx(28) : error BC30188: Declaration expected.

objBicicleta.Precio = 200 
~~~~~~~~~~~~              
c:\inetpub\wwwroot\pruebas\aspnet\test.aspx(29) : error BC30188: Declaration expected.

objBicicleta.NumeroDeVelocidades = 21 
~~~~~~~~~~~~                          
c:\inetpub\wwwroot\pruebas\aspnet\test.aspx(31) : error BC30188: Declaration expected.

objBicicleta.Acelerar(5) 
~~~~~~~~~~~~             
c:\inetpub\wwwroot\pruebas\aspnet\test.aspx(32) : error BC30188: Declaration expected.

objBicicleta.Frenar() 
~~~~~~~~~~~~          
c:\inetpub\wwwroot\pruebas\aspnet\test.aspx(34) : error BC30188: Declaration expected.

VelocidadActual = objBicicleta.ConsultarVelocidad()
~~~~~~~~~~~~~~~                                    
c:\inetpub\wwwroot\pruebas\aspnet\test.aspx(36) : error BC30188: Declaration expected.

Response.write VelocidadActual
~~~~~~~~                      

 



Show Complete Compilation Source:


Line 1:    '------------------------------------------------------------------------------
Line 2:    ' <autogenerated>
Line 3:    '     This code was generated by a tool.
Line 4:    '     Runtime Version: 1.1.4322.573
Line 5:    '
Line 6:    '     Changes to this file may cause incorrect behavior and will be lost if 
Line 7:    '     the code is regenerated.
Line 8:    ' </autogenerated>
Line 9:    '------------------------------------------------------------------------------
Line 10:   
Line 11:   Option Strict Off
Line 12:   Option Explicit On
Line 13:   
Line 14:   Imports Microsoft.VisualBasic
Line 15:   Imports System
Line 16:   Imports System.Collections
Line 17:   Imports System.Collections.Specialized
Line 18:   Imports System.Configuration
Line 19:   Imports System.Text
Line 20:   Imports System.Text.RegularExpressions
Line 21:   Imports System.Web
Line 22:   Imports System.Web.Caching
Line 23:   Imports System.Web.Security
Line 24:   Imports System.Web.SessionState
Line 25:   Imports System.Web.UI
Line 26:   Imports System.Web.UI.HtmlControls
Line 27:   Imports System.Web.UI.WebControls
Line 28:   
Line 29:   Namespace ASP
Line 30:       
Line 31:       Public Class test_aspx
Line 32:           Inherits System.Web.UI.Page
Line 33:           Implements System.Web.SessionState.IRequiresSessionState
Line 34:           
Line 35:           Private Shared __autoHandlers As Integer
Line 36:           
Line 37:           Private Shared __initialized As Boolean = false
Line 38:           
Line 39:           Private Shared __fileDependencies As System.Collections.ArrayList
Line 40:           
Line 41:           
Line 42:           #ExternalSource("c:\inetpub\wwwroot\pruebas\aspnet\test.aspx",1)
Line 43:           
Line 44:   
Line 45:   Public Class Bicicleta 
Line 46:       Public Modelo as String 
Line 47:       Public Precio as Double 
Line 48:       Public NumeroDeVelocidades as Integer 
Line 49:       Private Velocidad as Integer 
Line 50:   
Line 51:       Public Sub Acelerar(ByVal km As Integer) 
Line 52:          Velocidad = Velocidad + km 
Line 53:       End Sub 
Line 54:   
Line 55:       Public Sub Frenar() 
Line 56:          If Velocidad > 0 Then 
Line 57:             Velocidad = Velocidad -1 
Line 58:          End If 
Line 59:       End Sub 
Line 60:   
Line 61:       Public Function ConsultarVelocidad() As Integer 
Line 62:          Return Velocidad 
Line 63:       End Function 
Line 64:   End Class
Line 65:   
Line 66:   Dim objBicicleta as Bicicleta = New Bicicleta 
Line 67:   
Line 68:   Dim VelocidadActual as Integer 
Line 69:   objBicicleta.Modelo = "Montaña" 
Line 70:   objBicicleta.Precio = 200 
Line 71:   objBicicleta.NumeroDeVelocidades = 21 
Line 72:   
Line 73:   objBicicleta.Acelerar(5) 
Line 74:   objBicicleta.Frenar() 
Line 75:   
Line 76:   VelocidadActual = objBicicleta.ConsultarVelocidad()
Line 77:   
Line 78:   Response.write VelocidadActual
Line 79:   
Line 80:   
Line 81:           #End ExternalSource
Line 82:           
Line 83:           Public Sub New()
Line 84:               MyBase.New
Line 85:               Dim dependencies As System.Collections.ArrayList
Line 86:               If (ASP.test_aspx.__initialized = false) Then
Line 87:                   dependencies = New System.Collections.ArrayList
Line 88:                   dependencies.Add("c:\inetpub\wwwroot\pruebas\aspnet\test.aspx")
Line 89:                   ASP.test_aspx.__fileDependencies = dependencies
Line 90:                   ASP.test_aspx.__initialized = true
Line 91:               End If
Line 92:           End Sub
Line 93:           
Line 94:           Protected Overrides Property AutoHandlers As Integer
Line 95:               Get
Line 96:                   Return ASP.test_aspx.__autoHandlers
Line 97:               End Get
Line 98:               Set
Line 99:                   ASP.test_aspx.__autoHandlers = value
Line 100:              End Set
Line 101:          End Property
Line 102:          
Line 103:          Protected ReadOnly Property ApplicationInstance As System.Web.HttpApplication
Line 104:              Get
Line 105:                  Return CType(Me.Context.ApplicationInstance,System.Web.HttpApplication)
Line 106:              End Get
Line 107:          End Property
Line 108:          
Line 109:          Public Overrides ReadOnly Property TemplateSourceDirectory As String
Line 110:              Get
Line 111:                  Return "/pruebas/aspnet"
Line 112:              End Get
Line 113:          End Property
Line 114:          
Line 115:          Private Sub __BuildControlTree(ByVal __ctrl As System.Web.UI.Control)
Line 116:          End Sub
Line 117:          
Line 118:          Protected Overrides Sub FrameworkInitialize()
Line 119:              Me.__BuildControlTree(Me)
Line 120:              Me.FileDependencies = ASP.test_aspx.__fileDependencies
Line 121:              Me.EnableViewStateMac = true
Line 122:              Me.Request.ValidateInput
Line 123:          End Sub
Line 124:          
Line 125:          Public Overrides Function GetTypeHashCode() As Integer
Line 126:              Return 5381
Line 127:          End Function
Line 128:      End Class
Line 129:  End Namespace
Line 130:  
 




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 

  #5 (permalink)  
Antiguo 17/08/2005, 14:09
 
Fecha de Ingreso: julio-2005
Mensajes: 58
Antigüedad: 18 años, 9 meses
Puntos: 0
sigo sin resolver el problema de la clase, alguien podria ayudarme por favor ?
  #6 (permalink)  
Antiguo 17/08/2005, 15:26
Avatar de xknown  
Fecha de Ingreso: diciembre-2004
Ubicación: Cusco - Perú
Mensajes: 2.248
Antigüedad: 19 años, 4 meses
Puntos: 7
Estás haciendo mal las cosas, todo el código que está dentro de los tags <script runat="server"></script> es englobado por una clase que se genera automáticamente al hacer el primer Request (puedes ver la clase que se genera haciendo click en: "Show Complete Compilation Source"), por lo tanto dentro de dicho bloque sólo puedes poner declaraciones de variables, propiedades, estructuras, enumerados, etc; pero no puedes poner por ejm: objBicicleta.Modelo = "Montaña"

Te sugiero que crees tus clases en otros archivos, y luego utilizes dichas clases según convenga, en métodos que creas por conveniente

Saludos
__________________
Alex Concha
Buayacorp - Programación y Diseño
  #7 (permalink)  
Antiguo 17/08/2005, 17:19
 
Fecha de Ingreso: julio-2005
Mensajes: 58
Antigüedad: 18 años, 9 meses
Puntos: 0
gracias por responder...

no se exactamente como hacer lo que me dices, pero mira, hice esto:

Código HTML:
<script runat="server" language="VB">
Public Class Bicicleta 
    Public Modelo as String 
    Public Precio as Double 
    Public NumeroDeVelocidades as Integer 
    Private Velocidad as Integer 

    Public Sub Acelerar(ByVal km As Integer) 
       Velocidad = Velocidad + km 
    End Sub 

    Public Sub Frenar() 
       If Velocidad > 0 Then 
          Velocidad = Velocidad -1 
       End If 
    End Sub 

    Public Function ConsultarVelocidad() As Integer 
       Return Velocidad 
    End Function 
End Class

</script>

<%
Dim objBicicleta as Bicicleta = New Bicicleta 

Dim VelocidadActual as Integer 
objBicicleta.Modelo = "Montaña" 
objBicicleta.Precio = 200 
objBicicleta.NumeroDeVelocidades = 21 

objBicicleta.Acelerar(8) 
objBicicleta.Frenar() 

VelocidadActual = objBicicleta.ConsultarVelocidad()

MyLabel.Text = VelocidadActual

%>


<form id="MyForm" runat="server">
   <asp:label id="MyLabel" runat="server"></asp:label>
</form>

Asi si funciono bien, como puedo hacerlo por separado como tu mencionas, como puedes ver, quite el Response.write y lo cambie por una etiqueta de salida de HTML, he estado mucho tiempo en esto, pero no tengo claros muchos conceptos, me harias el favor de decirme como separar el codigo como tu lo sugieres...

muchas gracias
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.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:59.