Sign in to follow this  
Followers 0

Visual Basic - Need help showing multiple words

12 posts in this topic

Posted

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
 

post-503111-0-71963900-1376869167.png

Share this post


Link to post
Share on other sites

Posted

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 :).

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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.

1 person likes this

Share this post


Link to post
Share on other sites

Posted

It's Windows Forms and thanks dude

Share this post


Link to post
Share on other sites

Posted

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:

Share this post


Link to post
Share on other sites

Posted

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'.

Share this post


Link to post
Share on other sites

Posted

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.'

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

Nvm i got it working :p

Share this post


Link to post
Share on other sites

Posted

.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.