none
How can I solve this problem RRS feed

  • Question

  • So hi thanks for reading my question. My question is how do I fix the duplicate radio button choices and how to I make sure that one of the radio buttons shows the correct answer

    My code:

    Partial Class Default2
        Inherits System.Web.UI.Page
    
        Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
    
            Dim Flagimageurl(6) As String ' creating the flag image url array'
            Flagimageurl(0) = "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"
            Flagimageurl(1) = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"
            Flagimageurl(2) = "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"
            Flagimageurl(3) = "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"
            Flagimageurl(4) = "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"
            Flagimageurl(5) = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"
    
    
            Dim CountryNames(6) As String 'creating the country names array'
            CountryNames(0) = "United Kingdom"
            CountryNames(1) = "France"
            CountryNames(2) = "Brazil"
            CountryNames(3) = "Spain"
            CountryNames(4) = "United States of America (USA)"
            CountryNames(5) = "Russia"
    
            flag.ImageUrl = Flagimageurl(6 * Rnd()) 'Choosing a random flag'
            RadioButton1.Text = CountryNames(5 * Rnd()) 'Randomly picks the country according to the CountryNames array'
            RadioButton2.Text = CountryNames(5 * Rnd())  ' with random * 6 you get blanks so you need to use random * 5'
            RadioButton3.Text = CountryNames(5 * Rnd())
            RadioButton4.Text = CountryNames(5 * Rnd())
    
            If RadioButton1.Text = RadioButton2.Text Then 'Makes sure that the radiobuttons don't show duplicate answers'
                RadioButton2.Text = CountryNames(5 * Rnd())
            End If
    
            If RadioButton2.Text = RadioButton1.Text Then
                RadioButton1.Text = CountryNames(5 * Rnd())
            End If
    
            If RadioButton3.Text = RadioButton1.Text Or RadioButton3.Text = RadioButton2.Text Then
                RadioButton3.Text = CountryNames(5 * Rnd())
            End If
    
            If RadioButton4.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Then
                RadioButton4.Text = CountryNames(5 * Rnd())
            End If
    
        End Sub
    End Class



    • Edited by Amciver99 Tuesday, July 4, 2017 8:32 AM v
    Tuesday, July 4, 2017 8:27 AM

All replies

  • My question is how do I fix the duplicate radio button choices and how to I make sure that one of the radio buttons shows the correct answer

    Your code is out of date - you should be using the .Net random object.  See:
    https://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx

    You can assign the text to the radio buttons using an array of unique random numbers. There are many ways to create unique random numbers.  One option is to add random numbers into an array, checking that each one is not already in the array.  See:
    https://msdn.microsoft.com/en-us/library/bb384015.aspx

    So the process is:

    Create that Random instance
    Create and initialise the array
    For I = 0 to 5
        Do
            Get the next random number
        Loop until that number is not already in the array
        Insert it into the array at location I
    Next I

    Once you have the array of unique random numbers you can work out the rest of the code.  Do not do this by writing more code: do it by tracing out the required steps, in detail, using pencil and paper.  After you have worked that through it will be possible to write the code.

    Tuesday, July 4, 2017 9:14 AM
  • WHY IS MY CODE OUT OF DATE?
    Tuesday, July 4, 2017 9:24 AM
  • WHY IS MY CODE OUT OF DATE?

    Your code is not really out of date, it is less used.

    However, what you miss in your form is handling the post back, but in fact is that less done in this forum. 

    Also is the 2003 style of code behind with real VB or C# less done currently. 

    Therefore try the ASP.Net forum for a larger audience for your question.

    http://forums.asp.net


    Success
    Cor

    Tuesday, July 4, 2017 9:42 AM
  • Partial Class Default2
        Inherits System.Web.UI.Page
    
        Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
    
            Dim Flagimageurl(5) As String ' creating the flag image url array'
            Flagimageurl(0) = "https://www.cia.gov/library/publications/the-world-factbook/graphics/flags/large/uk-lgflag.gif"
            Flagimageurl(1) = "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/Flag_of_France.svg/800px-Flag_of_France.svg.png"
            Flagimageurl(2) = "https://upload.wikimedia.org/wikipedia/en/thumb/0/05/Flag_of_Brazil.svg/720px-Flag_of_Brazil.svg.png"
            Flagimageurl(3) = "https://upload.wikimedia.org/wikipedia/en/thumb/9/9a/Flag_of_Spain.svg/750px-Flag_of_Spain.svg.png"
            Flagimageurl(4) = "https://upload.wikimedia.org/wikipedia/en/thumb/a/a4/Flag_of_the_United_States.svg/1235px-Flag_of_the_United_States.svg.png"
            Flagimageurl(5) = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/Flag_of_Russia_%28Kremlin.ru%29.svg/1024px-Flag_of_Russia_%28Kremlin.ru%29.svg.png"
    
    
            Dim CountryNames(5) As String 'creating the country names array'
            CountryNames(0) = "United Kingdom"
            CountryNames(1) = "France"
            CountryNames(2) = "Brazil"
            CountryNames(3) = "Spain"
            CountryNames(4) = "United States of America (USA)"
            CountryNames(5) = "Russia"
    
            flag.ImageUrl = Flagimageurl(5 * Rnd()) 'Choosing a random flag'
            RadioButton1.Text = CountryNames(5 * Rnd()) 'Randomly picks the country according to the CountryNames array'
            RadioButton2.Text = CountryNames(5 * Rnd())  ' with random * 6 you get blanks so you need to use random * 5'
            RadioButton3.Text = CountryNames(5 * Rnd())
            RadioButton4.Text = CountryNames(5 * Rnd())
    
            If RadioButton1.Text = RadioButton2.Text Then 'Makes sure that the radiobuttons don't show duplicate answers'
                RadioButton2.Text = CountryNames(5 * Rnd())
            End If
    
            If RadioButton2.Text = RadioButton1.Text Then
                RadioButton1.Text = CountryNames(5 * Rnd())
            End If
    
            If RadioButton3.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton4.Text Then
                RadioButton3.Text = CountryNames(5 * Rnd())
            End If
    
            If RadioButton4.Text = RadioButton1.Text Or RadioButton2.Text = RadioButton3.Text Then
                RadioButton4.Text = CountryNames(5 * Rnd())
            End If
    
        End Sub
    End Class
    
    this is my code
    Tuesday, July 4, 2017 9:55 AM
  • WHY IS MY CODE OUT OF DATE?

    Because you are using the RND() function instead of the .Net Random class.  See:
    https://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx

    Tuesday, July 4, 2017 10:24 AM
  • this is my code
    If you have changed that from what was first posted, then you should identify what is different, to save people from trying to compare it line-by-line.
    Tuesday, July 4, 2017 10:25 AM
  • code: https://github.com/alexmciver/Alex-M-Flags/blob/master/Default2.aspx.vb

    I have added new countries(flags and names) but the radio buttons are not always showing the correct answer for that flag and they are showing duplicate answers.

    Wednesday, July 5, 2017 10:01 AM
  • But your code is about controls in ASP.Net, that is not handled here, I gave already the link to that forum.

    Success
    Cor

    Wednesday, July 5, 2017 11:44 AM
  • The easiest way to solve this is to use a List(Of String) instead of an array.  After adding the items to the list, randomly select an index, retrieve that item, then remove it from the list.  This ensures that you can never get a duplicate item.

    For example:

    Dim countryNames As New List(Of String)
    countryNames.Add("United Kingdom")
    countryNames.Add("France")
    countryNames.Add("Brazil")
    countryNames.Add("Spain")
    countryNames.Add("United States")
    countryNames.Add("Russia")
    
    Dim r As New Random
    Dim getAndRemoveRandomEntry = Function(items As IList)
                                      Dim index = r.Next(items.Count)
                                      Dim result = items(index)
                                      items.RemoveAt(index)
                                      Return result
                                  End Function
    
    RadioButton1.Text = getAndRemoveRandomEntry(countryNames)
    RadioButton2.Text = getAndRemoveRandomEntry(countryNames)
    RadioButton3.Text = getAndRemoveRandomEntry(countryNames)
    RadioButton4.Text = getAndRemoveRandomEntry(countryNames)
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, July 5, 2017 3:17 PM
    Moderator
  • The easiest way to solve this is to use a List(Of String) instead of an array.  After adding the items to the list, randomly select an index, retrieve that item, then remove it from the list.  This ensures that you can never get a duplicate item.

    For example:

    Dim countryNames As New List(Of String)
    countryNames.Add("United Kingdom")
    countryNames.Add("France")
    countryNames.Add("Brazil")
    countryNames.Add("Spain")
    countryNames.Add("United States")
    countryNames.Add("Russia")
    
    Dim r As New Random
    Dim getAndRemoveRandomEntry = Function(items As IList)
                                      Dim index = r.Next(items.Count)
                                      Dim result = items(index)
                                      items.RemoveAt(index)
                                      Return result
                                  End Function
    
    RadioButton1.Text = getAndRemoveRandomEntry(countryNames)
    RadioButton2.Text = getAndRemoveRandomEntry(countryNames)
    RadioButton3.Text = getAndRemoveRandomEntry(countryNames)
    RadioButton4.Text = getAndRemoveRandomEntry(countryNames)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    I really don't understand the moderation of this forum anymore. 

    The question is shown about a Web form and still some give replies if it is a windows forms question while I've 3 times written about. 

    Do some really know only something about Windows Forms and don't recognize a Page. 


    Success
    Cor

    Wednesday, July 5, 2017 3:32 PM
  • @Cor:

    The fact that it is a web form is irrelevant to the question.  The question is specifically a language issue and the answer would apply to any kind of project.

    A redirect to the ASP.Net forum is only necessary when the issue is dependent on ASP.Net specific features.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, July 6, 2017 3:36 PM
    Moderator
  • @Cor:

    The fact that it is a web form is irrelevant to the question.  The question is specifically a language issue and the answer would apply to any kind of project.

    A redirect to the ASP.Net forum is only necessary when the issue is dependent on ASP.Net specific features.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    No it is not, you did not read what I wrote. In an ASP.Net solution it is very important to make the data persistent. In an ASP.Net application a form has no state. 

    As well is it very important how the IsPostBack is handled. 

    It seems to me you have never handled this and wrote the above simply as an assumption. 

    Of course I can handle this and my code would be very different from yours. But I know that as soon as I do that my answer is moved to OT


    Success
    Cor



    Thursday, July 6, 2017 3:57 PM
  • @Cor:

    The fact that it is a web form is irrelevant to the question.  The question is specifically a language issue and the answer would apply to any kind of project.

    A redirect to the ASP.Net forum is only necessary when the issue is dependent on ASP.Net specific features.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    No it is not, you did not read what I wrote. In an ASP.Net solution it is very important to make the data persistent. In an ASP.Net application a form has no state. 

    As well is it very important how the IsPostBack is handled. 

    It seems to me you have never handled this and wrote the above simply as an assumption. 

    Of course I can handle this and my code would be very different from yours. But I know that as soon as I do that my answer is moved to OT


    Success
    Cor



    I'm not getting into a big debate with you so this is the last post on the subject.

    The question has nothing to do with persistence - you have no idea how the rest of the page is designed, if it is using partial postbacks or which controls are configured to cause postbacks or if it is executing all client-side JS without any postbacks.  You make a lot of assumptions that have nothing to do with the question.  The question is specifically about how to eliminate duplicate values when randomly selecting items from a list and the answer I provided does exactly that.  End of discussion.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, July 6, 2017 4:26 PM
    Moderator