Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [vb.net] Prendere un campo da un oggetto

    Salve ragazzi,
    Ho creato un oggetto di questo tipo


    Codice PHP:
    For as Integer 0 to 10
    Dim Profilo 
    as New Profilo
    Profilo
    .idutente i.toString
    Profilo
    .username "User"i.toString

    Next 
    e ho una variabile
    Codice PHP:
    Dim profiloSelezionato as String  "1" 
    con questa variabile che si trova all'esterno del mio for come faccio a sapere che username ho con Profilo.idutente uguale alla mia variabile profiloSelezionato?


    Grazie mille

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Fai queste modifiche:

    codice:
    Public Class Profilo
    
        Private myusername As String
        Private myid As Integer
    
        Public Property idutente() As Integer
            Get
                Return myid
            End Get
            Set(ByVal value As Integer)
                myid = value
            End Set
        End Property
    
        Public Property UserName() As String
            Get
                Return myusername
            End Get
            Set(ByVal value As String)
                myusername = value
            End Set
        End Property
    
    End Class
    codice:
     Dim Profilo(10) As Profilo
                For i As Integer = 0 To 10
                    Profilo(i) = New Profilo
                    Profilo(i).idutente = i
                    Profilo(i).UserName = "User" & i.ToString
                Next
     Dim profiloSelezionato As Integer = 1
     Dim userSelezionato As String = Profilo(profiloSelezionato).UserName
    idutente diventa un intero, inutile scomodare una variabile stringa!

    Oppure molto più elegante:

    codice:
    Public Class Profilo
    
        Private myusername() As String
        Private myid() As Integer
    
        Sub New(ByVal numeroutenti As Integer)
            ReDim myusername(numeroutenti)
            ReDim myid(numeroutenti)
        End Sub
    
        Public Property idutente(ByVal i As Integer) As Integer
            Get
                Return myid(i)
            End Get
            Set(ByVal value As Integer)
                myid(i) = value
            End Set
        End Property
    
        Public Property UserName(ByVal i As Integer) As String
            Get
                Return myusername(i)
            End Get
            Set(ByVal value As String)
                myusername(i) = value
            End Set
        End Property
    
    End Class
    e..

    codice:
     Try
                Dim Profilo As New Profilo(10)
                For i As Integer = 0 To 10
                    Profilo.idutente(i) = i
                    Profilo.UserName(i) = "User" & i.ToString
                Next
                Dim profiloSelezionato As Integer = 1
                Dim userSelezionato As String = Profilo.UserName(profiloSelezionato)
            Catch ex As Exception
            End Try
    Sbagliare è umano, perseverare è diabolico.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.