Wrox Programmer Forums
|
BOOK: Beginning Microsoft Visual Basic 2010
This is the forum to discuss the Wrox book Beginning Visual Basic 2010 by Thearon Willis, Bryan Newsome; ISBN: 9780470502228
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual Basic 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 11th, 2011, 11:08 PM
Registered User
 
Join Date: Apr 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 4: Case Statement

Hello community Wrox!

I ran into a problem running the below code. What happens is the strFavoriteColor variable has a green line under it and the tooltip states it is being used before being declared and could be declared null at runtime. To my understanding of that according to the book this variable will have an empty value when executed. The program runs fine except when you click a name it states the "UsersName favorite color is ". It does not display the name. What I did to attempt to resolve the issues was double check my spelling of the names because I thought the names were not the same in the case statement and was returning false for each case evaluation. Does anyone have any ideas of the steps I may have overlooked?

Code:
Public Class Form1

    Private Sub lstData_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstData.SelectedIndexChanged
        'Declare Variables
        Dim strName As String
        Dim strFavoriteColor As String

        'Get the selected name
        strName = lstData.Items(lstData.SelectedIndex).ToString

        'User a select Case statement to get the favorite color of the selected name
        Select Case Name
            Case "Bryan"
                strFavoriteColor = "Madras Yellow"
            Case "Ashley"
                strFavoriteColor = "Sea Blue"
            Case "Jennifer"
                strFavoriteColor = "Morning Mist"
            Case "Eddie"
                strFavoriteColor = "Passionate Purple"
            Case "Katelyn"
                strFavoriteColor = "Red"
        End Select

        'Display the favorite color of the selected name
        MessageBox.Show(strName & "'s favorite color is " & strFavoriteColor, "Select Demo")
    End Sub
End Class
 
Old January 13th, 2011, 03:23 AM
Registered User
 
Join Date: Apr 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Answer Found

Hi all!

I made a mistake with my variable in the name Case.

Code:
Select Case strName
It did not recognize Name because Name is an object in the Form class as far as I understand.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Case Statement eastwest SQL Server 2005 8 August 6th, 2009 05:19 PM
Case statement cole SQL Language 3 May 8th, 2005 03:02 PM
case statement Hudson40 Access VBA 1 February 11th, 2005 11:31 AM
Using A CASE Statement fastcorvette Access 5 December 24th, 2003 01:39 PM
case statement jakeone Beginning PHP 10 August 19th, 2003 03:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.