Oppure

Loading
19/10/11 7:59
karl93
E come devo modificare TermYnator?


Sto provando in questo momento il consiglio di nessuno, quindi appena finisco vi faccio sapere.



Grazie sempre a tutti :D
aaa
19/10/11 9:58
karl93
Non riesco a risolvere...Come posso fare?
aaa
19/10/11 10:14
Ma hai letto quello che ti ho scritto?

Non riesci a risolvere ma hai scritto qualcosa, hai provato o non vuoi neanche farci vedere le righe che hai scritto e provato?
19/10/11 10:35
karl93
Ovvio ke ho provato...e poi xkè non dovrei far vedere il codice, ho scritto prima tutto quel codice, non ho problemi a farlo pure ora.

Cmq in pratica ho creato una variabile booleana di nome "eseguito" per controllare se il codice è stato eseguito precedentemente grazie ad un "IF". Però non so se ho scritto bene.

Ecco il codice:


Private Sub Command4_Click()

Label1.Caption = ""
For i = 1 To N - 1
For j = i + 1 To N
If Numeri(i) > Numeri(j) Then
Scambia Numeri(i), Numeri(j)
End If
Next j
Next i
eseguito = True

End Sub
aaa
19/10/11 12:09
Postato originariamente da karl93:

Ovvio ke ho provato...e poi xkè non dovrei far vedere il codice, ho scritto prima tutto quel codice, non ho problemi a farlo pure ora.


E allora mostra il codice e discutine, non scrivere "non ci riesco" ...

... grazie ad un "IF".


E dove sarebbe questa If ?
19/10/11 16:02
TermYnator
Premesso che non si riesce a capire bene quello che vuoi fare, personalmente procederei con questo ordine.
a) Evitare di usare dati in modo generico, che VB assegna al tipo variant. Questo per evitare errori irreversibili che nel tuo codice sono all'ordine del giorno. Ad esempio, il frammento:

numeri(i)=inputbox""

Sebbene VB operi conversioni implicite (ovvero converta automaticamente il dato nel caso sia compatibile) se digiti un carattere stringa, verrà generato un blocco dell'applicazione, perchè inputbox restituisce un valore stringa mentre la matrice è di tipo integer. Abituati quindi ad usare tipi specifici.

L'assegnazione di un tipo più specifico infatti, se da un canto allunga il codice, ti consente di intercettare immissioni non valide. L'output della finestra, dichiaralo con una variabile stringa (nel mio esempio "Res", che analizzi poi con un operatore isnumeric (res) in un costrutto if.

b) Evita di dichiarare variabili globali per quanto possibile. anche questo tipo di dichiarazione, può generare errori imprevedibili. Nel tuo codice, ci sono molte , (ad esempio il contatore "i";) che hanno uno spazio di visibilità inutilmente esteso. anche questo, può generare comportamenti imprevisti.

In ultimo, (come giustamente ti fa notare Nessuno), abituati ad usare "flags", ovvero variabili di controllo, per memorizzare se una operazione è stata fatta.
Ti posto un esempio fatto al volo di come il tuo codice potrebbe essere modificato (copiaincollalo nella tua appicazione, e vedi come si comporta):

Option Explicit
Dim N As Integer, tot As Integer, totpari As Integer, totdispari As Integer
Dim indicipari As Integer, indicidispari As Integer
Dim Numeri(1 To 30) As Integer
Private Type Serie
   Numeri(1 To 30) As Integer
   Worked As Boolean
   Created As Boolean
   Sorted As Boolean
End Type
Private S As Serie
Private Sub Command1_Click()
Dim Res As String, CountMsg As String, NumMsg As String, I As Integer, TmpInt As Integer
CountMsg = "Annullare la creazione della serie?"
NumMsg = "annullare l'inserimento dei numeri?"
Reset
Res = InputBox("quanti numeri vuoi inserire?" & vbCrLf & _
"(inserire un valore minore di 30)", "richiesta numero dati")
If ControlData(Res, CountMsg, N) Then
   For I = 1 To N
      Do
         Res = InputBox("inserisci un numero", "inserimento dati")
         If ControlData(Res, NumMsg, TmpInt) Then
            Exit Do
         Else
            If N = 0 Then
               Exit Sub
            End If
         End If
      Loop
      S.Numeri(I) = TmpInt
   Next I
   S.Created = True
End If
End Sub
Private Function ControlData(StrVal As String, Msg As String, ReturnVal As Integer) As Boolean
If StrVal = "" Then
   If MsgBox(Msg, vbYesNo) = vbNo Then
      ReturnVal = 1
   Else
      MsgBox "Operazione annullata", vbExclamation
      ReturnVal = 0
   End If
   ControlData = False
ElseIf IsNumeric(StrVal) Then
   ControlData = True
   ReturnVal = CInt(StrVal)
End If
End Function
Private Sub ShowSub()
Dim I As Integer
If S.Created Then
   If S.Worked Then
      MsgBox "Serie gia elaborata.", vbInformation
   Else
      For I = 1 To N
         Label1.Caption = Label1.Caption & S.Numeri(I) & " "
         tot = tot + S.Numeri(I)
      If Numeri(I) Mod 2 = 0 Then
         totpari = totpari + S.Numeri(I)
      ElseIf Numeri(I) Mod 2 <> 0 Then
         totdispari = totdispari + S.Numeri(I)
      End If
      If I Mod 2 = 0 Then
         indicipari = indicipari + S.Numeri(I)
      ElseIf I Mod 2 <> 0 Then
         indicidispari = indicidispari + S.Numeri(I)
      End If
      Next I
      Label3.Caption = tot
      Label8.Caption = totpari
      Label9.Caption = totdispari
      Label14.Caption = indicidispari
      Label12.Caption = indicipari
      S.Worked = True
   End If
Else
   MsgBox "Nessuna serie inserita: inserire i vettori", vbInformation
End If
End Sub
Private Sub Command2_Click()
ShowSub
End Sub
Private Sub Command4_Click()
SortSub
End Sub
Private Sub SortSub()
Dim J As Integer, I As Integer
If S.Created Then
   If S.Sorted Then
      MsgBox "La serie di vettori è gia ordinata!", vbInformation
   Else
      For I = 1 To N - 1
      For J = I + 1 To N
      If S.Numeri(I) > S.Numeri(J) Then
         Scambia S.Numeri(I), S.Numeri(J)
      End If
      Next J
      Next I
   End If
   S.Sorted = True
Else
   MsgBox "Nessuna serie da ordinare: inserire una serie", vbInformation
End If
End Sub
Private Sub Scambia(ByRef x As Integer, ByRef Y As Integer)
Dim appoggio As Integer
appoggio = x
x = Y
Y = appoggio
End Sub
Private Sub Reset()
Erase S.Numeri
S.Sorted = False
S.Created = False
S.Worked = False
Label1.Caption = ""
N = 0
tot = 0
totpari = 0
indicipari = 0
indicidispari = 0
S.Created = False
End Sub

Ultima modifica effettuata da TermYnator 19/10/11 16:12
aaa
19/10/11 16:09
karl93
Ok...credo ke questo sia un livello molto superiore a quello che ho studiato fino ad ora. Comunque ora lo studio e lo provo. Poi vi faccio sapere se qualcs non mi è chiaro.


Grazie TermYnator :asd:
Ultima modifica effettuata da karl93 19/10/11 16:12
aaa
20/10/11 9:54
karl93
Questa funzione controlla se le operazioni sono già state effettuate???

Private Function ControlData(StrVal As String, Msg As String, ReturnVal As Integer) As Boolean
If StrVal = "" Then
   If MsgBox(Msg, vbYesNo) = vbNo Then
      ReturnVal = 1
   Else
      MsgBox "Operazione annullata", vbExclamation
      ReturnVal = 0
   End If
   ControlData = False
ElseIf IsNumeric(StrVal) Then
   ControlData = True
   ReturnVal = CInt(StrVal)
End If
End Function
aaa