Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
VB.NET
VB.NET General Discussion
Checking no two numbers are the same.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="MattP, post: 128005, member: 16051"] I'm not sure what you're trying to do with your For loop there. From your previous posts it seems like you're trying to create a lottery drawing or something similar. I've made a short example showing using the For loop to iterate over some use selected numbers (hard coded in the example). Once in the For loop the If statement checks to see if nums (the random array) contains the current user entered number. If the number is in the array then I'm incrementing a counter so I can tell how many matches I've got and add the value to a List(Of Integer) so I can check later what the matching values were. The code for IntersectButton.Click show how you can use LINQ's extension method Intersect to pull out the matching values. [CODE]Public Class Form1 Dim nums As Integer() Private Sub GenerateRandomButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateRandomButton.Click Dim rnd As New Random nums = Enumerable.Range(1, 100) _ .OrderBy(Function() rnd.Next) _ .Take(6) _ .ToArray() End Sub Private Sub ForLoopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles ForLoopButton.Click Dim matchCounter As Integer = 0 Dim matchingValues As New List(Of Integer) Dim vals As Integer() = {1, 11, 22, 33, 44, 55, 66, 77, 88, 99} For Each v As Integer In vals If nums.Contains(v) Then matchCounter += 1 matchingValues.Add(v) End If Next Select Case matchCounter Case 0 MessageBox.Show("There are no matches") Case 1 MessageBox.Show("There is 1 match" & _ Environment.NewLine & _ String.Format("The matching value is: {0}", matchingValues(0))) Case Else MessageBox.Show(String.Format("There are {0} matches", matchCounter) & _ Environment.NewLine & _ String.Format("The matching values are: {0}", String.Join(",", matchingValues))) End Select End Sub Private Sub IntersectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IntersectButton.Click Dim vals As Integer() = {1, 11, 22, 33, 44, 55, 66, 77, 88, 99} Dim matches = vals.Intersect(nums) Select Case matches.Count Case 0 MessageBox.Show("There are no matches") Case 1 MessageBox.Show("There is 1 match" & _ Environment.NewLine & _ String.Format("The matching value is: {0}", matches(0))) Case Else MessageBox.Show(String.Format("There are {0} matches", matches.Count) & _ Environment.NewLine & _ String.Format("The matching values are: {0}", String.Join(", ", matches))) End Select End Sub End Class[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
VB.NET
VB.NET General Discussion
Checking no two numbers are the same.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom