Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET 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 August 5th, 2007, 12:19 PM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default vb 2003 to 2005 convert gone wrong

I have a program that used to work with my old version of visual studio but now i have upgraded to 2005 and Vista, but the programs stopped working.

The error i am getting is:
System.InvalidCastException was unhandled Conversion from string "" to type 'Boolean' is not valid.

Here's the code

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents btnMain As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnMain = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'btnMain
        '
        Me.btnMain.Location = New System.Drawing.Point(24, 16)
        Me.btnMain.Name = "btnMain"
        Me.btnMain.Size = New System.Drawing.Size(240, 23)
        Me.btnMain.TabIndex = 0
        Me.btnMain.Text = "Give me my Task Manager beee-ach"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 54)
        Me.Controls.Add(Me.btnMain)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMain.Click
        'read registry
        If CBool(ReadWriteCURegistry("Software\Microsoft\Wind ows\CurrentVersion\Policies\System", "DisableTaskMgr", False)) = False Then
            'change registry
            ReadWriteCURegistry("Software\Microsoft\Windows\Cu rrentVersion\Policies\System", "DisableTaskMgr", True, "1")
            btnMain.Text = "Enable task manager"
        Else
            ReadWriteCURegistry("Software\Microsoft\Windows\Cu rrentVersion\Policies\System", "DisableTaskMgr", True, "00")
            btnMain.Text = "Disable task manager"
        End If


    End Sub
    Private Function ReadWriteCURegistry(ByVal subkey As String, Optional ByVal value As String = "", Optional ByVal openToWrite As Boolean = False, Optional ByVal writetokey As String = "") As String
        Dim regKey As Microsoft.Win32.RegistryKey
        Dim key As Boolean

        Try
            regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(su bkey, openToWrite)
            If openToWrite Then
                regKey.SetValue(value, writetokey)
            End If
            key = regKey.GetValue(value)
            regKey.Close()
            Return key
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If CBool(ReadWriteCURegistry("Software\Microsoft\Wind ows\CurrentVersion\Policies\System", "DisableTaskMgr", False)) = False Then
            btnMain.Text = "Disable task manager"
        Else
            btnMain.Text = "Enable task manager"
        End If
    End Sub
End Class

I am getting this error on this line:
If CBool(ReadWriteCURegistry("Software\Microsoft\Wind ows\CurrentVersion\Policies\System", "DisableTaskMgr", False)) = False Then

I'm unsure how to deal with the issue, i've heard about the convert class but don't think i can use this here. Any help is greatly accepted.

Kane

 
Old August 5th, 2007, 02:14 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You may want to set Option Explicit and Option Strict on to have the compiler tell you about these problems at compile time. Take a look at this:

Private Function ReadWriteCURegistry(ByVal subkey As String,
Optional ByVal value As String = "", Optional ByVal openToWrite As
Boolean = False, Optional ByVal writetokey As String = "") As String

Dim key As Boolean

End Function

You declare the method as a String, yet to have it return a Boolean and finally you treat the result as a Boolean. Why not declare the method as a Boolean method?

Private Function ReadWriteCURegistry(ByVal subkey As String,
Optional ByVal value As String = "", Optional ByVal openToWrite As
Boolean = False, Optional ByVal writetokey As String = "") As Boolean

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 5th, 2007, 04:35 PM
Registered User
 
Join Date: Aug 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks Imar, i can't believe it was that simple.

Kane :D





Similar Threads
Thread Thread Starter Forum Replies Last Post
reg conn to sql server 2005 from vb.net 2005.. veda SQL Server 2005 2 July 1st, 2008 12:16 AM
VB 2005 Generics in a Project Updgraded from 2003 myserenity101 Pro Visual Basic 2005 5 October 24th, 2006 01:44 PM
VB 2005 connect to server 2003 with SQL database Derek_05 Visual Basic 2005 Basics 3 September 20th, 2006 10:10 AM
Conversion from .Net 2003 to .Net 2005 using VB.Ne coleenh Visual Basic 2005 Basics 0 September 19th, 2006 02:48 PM
Can I run VB .net 2005 with DOT NET 2003 APPLICATI kadesskade BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 2 August 8th, 2006 05:14 AM





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