CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Posts
    1

    Question Database implementation: having trouble with castException for this function

    I keep getting either a Infinite loop error or a invalidCastException for this function not sure why.
    I am looking to create a function that can be used in Click events to change the color of the background.

    Code:
    Function BackEndColors(ByVal strTheme As String)
            ''Checks to see if user selected ThemeColor''
            ''CStr converts data table row 0 , item 0 to a string - inline for better performance
            strTheme = CStr(dtThemeColor.Rows(0).Item(0))
    
            If Not (strTheme <> Nothing) Then
                ''if the user has a preference set in their user account information
    
                ''nested If''
                ''makes color of background fit users preference''
                If strTheme = "Default" Then
                    pnlTitle.BackColor = Color.SteelBlue
                    Me.BackColor = Color.FromArgb(122, 154, 186)
                    lilRegister.LinkColor = Color.LightSteelBlue
    
                ElseIf strTheme = "Green" Then
                    pnlTitle.BackColor = Color.DarkOliveGreen
                    Me.BackColor = Color.OliveDrab
                    lilRegister.LinkColor = Color.Navy
    
                ElseIf strTheme = "Blue" Then
                    pnlTitle.BackColor = Color.MidnightBlue
                    Me.BackColor = Color.DarkSlateBlue
                    lilRegister.LinkColor = Color.PowderBlue
    
                ElseIf strTheme = "LightBlue" Then
                    pnlTitle.BackColor = Color.FromArgb(0, 105, 183)
                    Me.BackColor = Color.FromArgb(69, 132, 207)
                    lilRegister.LinkColor = Color.Navy
    
                End If
            Else ''If user didn't select color''
                ''Default Theme''
                BackEndColors("default")
    
                ''Default ThemeColor''
                strTheme = ""
            End If
    
            ''null pointer - Void Function''
            ''needed for NullPointerException''
            Return Nothing
    
        End Function

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Database implementation: having trouble with castException for this function

    Where are you getting the error?

    You have this defined as a function but you have not defined the type the function is to return and the only code I see that returns something actually returns Nothing which makes no sense at all.

    You also have the function set to take a parameter but then you do nothing with that parameter.

    You call the function again in your else case and since the condition that caused the else to trigger has not changed then it will fall into an infinite recursion until it hangs or crashes.

    Overall the design is very poor and needs to be re-written
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Database implementation: having trouble with castException for this function

    InvalidCastException usually refers to a conversion that cannot be made. So somewhere in either your DB or your function you have incompatible types, or trying to convert something into a wrong type. It can also mean that you may have a NULL value in your table

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured