Jump to content



Photo

Visual Basic - Need help showing multiple words

Answered Go to the full post

  • Please log in to reply
11 replies to this topic

#1 atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 18 August 2013 - 23:39

This is my Code, and i have been trying to make thebutton so that it will automatically show everything from Mercury venus and etc and i have make it so that it show Mercury - Pluto in seperate lines but than i am not able to make it to show the name instead it only show String[]Array in every line any ideas how to fix it so that instead of string[]Array it will show each name correctly. Below is the picture attached as an example of how thefinal result will look like
Public Class Form1
 
    Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
        'Number of planets before Pluto was "let go"
        Const intNUM_PLANETS As Integer = 9
        'Planet names
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
        'diameter of each planet in miles
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
        strArrayPlanetNames = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        For intX = 0 To intNUM_PLANETS - 1
            'output name and size 
            lstPlanets.Items.Add(strArrayPlanetNames)
            'output bar chart on next line
        Next
    End Sub
 
End Class
 

Attached Images

  • Screenshot after button clicked.png


Best Answer atyemail , 19 August 2013 - 00:57

Public Class Form1
 
    Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
 
        'Number of planets before Pluto was "let go"'
        Const intNUM_PLANETS As Integer = 9
 
        'Planet names'
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        'diameter of each planet in miles'
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
 
        For intX = 0 To intNUM_PLANETS - 1
            'output name and size'
            lstPlanets.Items.Add(strArrayPlanetNames(intX))
            lstPlanets.Items.Add(dblArrayPlanetSizes(intX))
            'output bar chart on next line'
        Next
 
    End Sub
 
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstPlanets.Items.Clear()
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub
End Class
So this is basically what i have done but then i want to make it that the Mercury 3031 but right now it's Mercury then below it 3031 and below it Venus and below it 7521 so yeah i want to make the Planet names beside the dblArrayPlanetSizes
Go to the full post



#2 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 7
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 19 August 2013 - 00:00

It looks like you're passing your string array into the list box, what you actually want to do is pass the string at each location in the array.

See my change to your code:
 

Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click

    'Number of planets before Pluto was "let go"'
    Const intNUM_PLANETS As Integer = 9

    'Planet names'
    Dim strArrayPlanetNames() As String =
    {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}

    'diameter of each planet in miles'
    Dim dblArrayPlanetSizes() As Double =
    {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}

    strArrayPlanetNames = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}

    For intX = 0 To intNUM_PLANETS - 1
        'output name and size'
          
        'Use the string, not the array...'
        ' vvv CHANGED THIS LINE BELOW vvv'
        lstPlanets.Items.Add(strArrayPlanetNames(intX))

        'output bar chart on next line'
    Next

End Sub

If you don't mind some constructive criticism, a few pointers:

  • Why set the contents of strArrayPlanetNames twice? You've doing it at the top of the function, and then again after you've declared the array of planet sizes?
  • It's generally preferred not to include type information in variable names in a strongly typed language. So normally variables called "planetNames" and "x" would be preferred instead of "strArrayPlanetNames" and "intX" respectively. If you need the type information, your IDE (e.g. Visual Studio) can provide you with this.
  • Please put your code inside "[ code]CODE GOES HERE[ /code]" tags in your posts (like I did). Makes it easier for the rest of us to read :).


#3 OP atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 19 August 2013 - 00:11

Hey thanks Majestic i'll try to do that on the future but then once again if you only put 0 it will only show the Mercury but i want it to show everything First Mercury Second Venus then Third Earth and so on i tried doing from 0 - 9 does not work also what else can i do to show all 0 - 9



#4 Eric

Eric

    Neowinian Senior

  • Tech Issues Solved: 11
  • Joined: 02-August 06
  • Location: Greenville, SC

Posted 19 August 2013 - 00:17

You should be using strArrayPanetNames(intX)

 

The default value converting an arraylist to a string is the type's name. That's why you're ending up with the undesired result.

 

Is this Windows Forms or WPF? There's an easier trick if it's WPF.



#5 OP atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 19 August 2013 - 00:20

It's Windows Forms and thanks dude



#6 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 7
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 19 August 2013 - 00:29

Hey thanks Majestic i'll try to do that on the future but then once again if you only put 0 it will only show the Mercury but i want it to show everything First Mercury Second Venus then Third Earth and so on i tried doing from 0 - 9 does not work also what else can i do to show all 0 - 9


Whoops my mistake! Replace the zero with "intX" and you're good to go!

I updated my post :blush:



#7 OP atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 19 August 2013 - 00:47

Anyway any idea why most of my codes always get this Error 1 Operator '+' is not defined for types 'Integer' and '1-dimensional array of Double'.



#8 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 7
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 19 August 2013 - 00:54

Sounds like a similar problem, you're trying to add an integer to an array object, rather than an element in the array. At a guess, you've probably got a line in your code that looks like this:
 
' WRONG! Can't add an integer to an array.'
something = intSomeNumber + dblArrayOfNumbers
You're probably trying to do this:
' RIGHT! Add together an integer and an element in the array...'
something = intSomeNumber + dblArrayOfNumbers(intY)
'                                            ^^^^^^'
'                                            Note where brackets are used to indicate which number in the array youre trying to access.'


#9 OP atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 19 August 2013 - 00:57   Best Answer

Public Class Form1
 
    Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
 
        'Number of planets before Pluto was "let go"'
        Const intNUM_PLANETS As Integer = 9
 
        'Planet names'
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        'diameter of each planet in miles'
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
 
        For intX = 0 To intNUM_PLANETS - 1
            'output name and size'
            lstPlanets.Items.Add(strArrayPlanetNames(intX))
            lstPlanets.Items.Add(dblArrayPlanetSizes(intX))
            'output bar chart on next line'
        Next
 
    End Sub
 
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstPlanets.Items.Clear()
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub
End Class
So this is basically what i have done but then i want to make it that the Mercury 3031 but right now it's Mercury then below it 3031 and below it Venus and below it 7521 so yeah i want to make the Planet names beside the dblArrayPlanetSizes


#10 OP atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 19 August 2013 - 00:58

Nvm i got it working :p



#11 OP atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 19 August 2013 - 01:25

.



#12 OP atyemail

atyemail

    Neowinian

  • Tech Issues Solved: 2
  • Joined: 09-August 13

Posted 19 August 2013 - 02:25

So i have been struggling out trying to figure out how to make 1000 = * i did it with my other project but it uses loops :

For StoreCount = 1 To 5
            strSalesData = InputBox("Enter sales data for store " & StoreCount.ToString)
 
            If strSalesData <> "" Then
                intSales = CInt(strSalesData)
 
                strLine = "Store " & StoreCount.ToString & ": "
                For StarCount = 1 To intSales Step 100
                    strLine &= "* "
                Next
                lstChartOut.Items.Add(strLine)
            End If
        Next
but then i tried using different code and trying different thing also with this code does not seem to find a way out so right not this is my code what can i add to for each 1000 = *

 

Public Class Form1
 
    Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
 
        'Number of planets before Pluto was "let go"'
        Const intNUM_PLANETS As Integer = 9
 
        'Planet names'
        Dim strArrayPlanetNames() As String =
        {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"}
 
        'diameter of each planet in miles'
        Dim dblArrayPlanetSizes() As Double =
        {3031, 7521, 7926, 4223, 88846, 74898, 31763, 30800, 1430}
 
 
        For intX = 0 To intNUM_PLANETS - 1
 
            'output name and size'
            lstPlanets.Items.Add(strArrayPlanetNames(intX) & "      " & dblArrayPlanetSizes(intX))
 
            'output bar chart on next line'
        Next
        
    End Sub
 
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstPlanets.Items.Clear()
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub
End Class




Click here to login or here to register to remove this ad, it's free!