OVH Cloud OVH Cloud

classe

6 réponses
Avatar
scalpa
peut on créer un module de classe qui fabriquerait des nombres aléatoires et
les renverrai dans un tableau ? j'ai essayé ceci mais ca ne marche pas...

Public Function Rand(ByVal Low As Long, ByVal High As Long, ByVal Deci As
Boolean, ByVal Combien As Integer) As Variant

Dim Tableau(1 To Combien) As Variant
Select Case Deci
Case False
For i = 1 To Combien
Rand = Int((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
Case Else
For i = 1 To Combien
Rand = ((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
End Select

End Function

Je n'arrive pas à afficher les nombres sur ma form1.

merci

6 réponses

Avatar
Jean-Marc
"scalpa" a écrit dans le message de
news:
peut on créer un module de classe qui fabriquerait des nombres aléatoires


et
les renverrai dans un tableau ? j'ai essayé ceci mais ca ne marche pas...

Public Function Rand(ByVal Low As Long, ByVal High As Long, ByVal Deci As
Boolean, ByVal Combien As Integer) As Variant

Dim Tableau(1 To Combien) As Variant
Select Case Deci
Case False
For i = 1 To Combien
Rand = Int((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
Case Else
For i = 1 To Combien
Rand = ((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
End Select

End Function

Je n'arrive pas à afficher les nombres sur ma form1.



Hello, ça marchera mieux comme ceci:


Public Function Rand(ByRef Tableau() As Variant, ByVal Low As Long, ByVal
High As Long, ByVal Deci As Boolean, ByVal Combien As Integer) As Variant

ReDim Tableau(1 To Combien)
Dim i As Long

Select Case Deci
Case False
For i = 1 To Combien
Rand = Int((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
Case Else
For i = 1 To Combien
Rand = ((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
End Select
End Function

Et l'appel:

Dim t() As Variant

Rand t(), 1, 100, False, 10

--
Jean-marc
"There are only 10 kind of people
those who understand binary and those who don't."
Avatar
ng
Salut,

Je te conseillerai en outre de typer correctement ton retour de fonction
(éviter le Variant !).

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/

Jean-Marc wrote:
"scalpa" a écrit dans le message de
news:
peut on créer un module de classe qui fabriquerait des nombres
aléatoires et les renverrai dans un tableau ? j'ai essayé ceci mais
ca ne marche pas...

Public Function Rand(ByVal Low As Long, ByVal High As Long, ByVal
Deci As Boolean, ByVal Combien As Integer) As Variant

Dim Tableau(1 To Combien) As Variant
Select Case Deci
Case False
For i = 1 To Combien
Rand = Int((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
Case Else
For i = 1 To Combien
Rand = ((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
End Select

End Function

Je n'arrive pas à afficher les nombres sur ma form1.



Hello, ça marchera mieux comme ceci:


Public Function Rand(ByRef Tableau() As Variant, ByVal Low As Long,
ByVal High As Long, ByVal Deci As Boolean, ByVal Combien As Integer)
As Variant

ReDim Tableau(1 To Combien)
Dim i As Long

Select Case Deci
Case False
For i = 1 To Combien
Rand = Int((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
Case Else
For i = 1 To Combien
Rand = ((High - Low + 1) * Rnd) + Low
Tableau(i) = Rand
Next i
End Select
End Function

Et l'appel:

Dim t() As Variant

Rand t(), 1, 100, False, 10


Avatar
Jean-Marc
Oui c'est sur. J'ai même pas regardé le type de retour quand j'ai
corrigé la fonction! Dans ce cas d'ailleurs, le meilleur choix est une
fonction qui retourne juste un booléen histoire de dire que tout est
ok, qq chose comme:

Public Function Rand(ByVal Low As Long, ByVal High As Long, ByVal
Deci As Boolean, ByVal Combien As Integer) As Boolean
' .. declaration
On error goto Err_Rand
' .. code

Rand = TRue
Sortie_OK:
exit function
Err_Rand:
Rand = False
Resume Sortie_OK
End Function


puis

Dim Result as Boolean

Result = Rand(.., .., ..)
If result Then
' OK
Else
' something goes wrong
' error handling ...
End If

--
Jean-marc
"There are only 10 kind of people
those who understand binary and those who don't."


"ng" a écrit dans le message de
news:
Salut,

Je te conseillerai en outre de typer correctement ton retour de fonction
(éviter le Variant !).

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/

Jean-Marc wrote:
> "scalpa" a écrit dans le message de
> news:
>> peut on créer un module de classe qui fabriquerait des nombres
>> aléatoires et les renverrai dans un tableau ? j'ai essayé ceci mais
>> ca ne marche pas...
>>
>> Public Function Rand(ByVal Low As Long, ByVal High As Long, ByVal
>> Deci As Boolean, ByVal Combien As Integer) As Variant
>>
>> Dim Tableau(1 To Combien) As Variant
>> Select Case Deci
>> Case False
>> For i = 1 To Combien
>> Rand = Int((High - Low + 1) * Rnd) + Low
>> Tableau(i) = Rand
>> Next i
>> Case Else
>> For i = 1 To Combien
>> Rand = ((High - Low + 1) * Rnd) + Low
>> Tableau(i) = Rand
>> Next i
>> End Select
>>
>> End Function
>>
>> Je n'arrive pas à afficher les nombres sur ma form1.
>
> Hello, ça marchera mieux comme ceci:
>
>
> Public Function Rand(ByRef Tableau() As Variant, ByVal Low As Long,
> ByVal High As Long, ByVal Deci As Boolean, ByVal Combien As Integer)
> As Variant
>
> ReDim Tableau(1 To Combien)
> Dim i As Long
>
> Select Case Deci
> Case False
> For i = 1 To Combien
> Rand = Int((High - Low + 1) * Rnd) + Low
> Tableau(i) = Rand
> Next i
> Case Else
> For i = 1 To Combien
> Rand = ((High - Low + 1) * Rnd) + Low
> Tableau(i) = Rand
> Next i
> End Select
> End Function
>
> Et l'appel:
>
> Dim t() As Variant
>
> Rand t(), 1, 100, False, 10




Avatar
scalpa
bonjour pourquoi éviter le variant ? merci
Je te conseillerai en outre de typer correctement ton retour de fonction
(éviter le Variant !).


Avatar
scalpa
merci de votre aide je vais étudier tout ça !!
Je ferai aussi un essai avec property...
Je ne comprends pas la différence entre les deux solutions.....functiun ou
property ?
Avatar
Patrice Henrio
Le variant est un type qui peut recevoir n'importe quel type donc on imagine
que nécessairement le traitement afférent sera plus long : vérification du
type, adressage par adresse même pour des byte ou des integer ...alors
qu'avec un type défini, la vérification se fait lors de la compilation avec
même le transtypage nécessaire en cas de besoin.

"scalpa" a écrit dans le message de news:
41ac8506$0$30416$
bonjour pourquoi éviter le variant ? merci
Je te conseillerai en outre de typer correctement ton retour de fonction
(éviter le Variant !).