By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,061 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,061 IT Pros & Developers. It's quick & easy.

What's wrong with this registry code

P: n/a
Public Shared Function GetValue(ByVal keyName As String, ByVal valueName As
String, Optional ByVal badKey As Object = "") As Object

Dim UserKey As RegistryKey =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\ keyName)

If Not UserKey Is Nothing Then

GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())

lCurrentUserKey.Close()

Else

GetValue = badKeyReturn

End If

End Function

If the value in the registry is a Boolean, say False,

lCurrentUserKey.GetValue returns a string, like "False".

I'd like the routine to return the type stored (Boolean in this example)

so I tried to set the return to agree with badKey

but the compiler doesn't like

badKeyReturn.GetType() , says it's not defined

I've played around enough to know the problem is that CType expects a type
and doesn't believe I've given it one.

Know how to fix it??

Thanks


Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
" Just Me" <gr****@a-znet.com> schrieb:
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\ keyName)


Are you sure that there should be a space in front of "keyName"?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #2

P: n/a
Sorry, I reduced the names so the text would fit on the page and introduced
the space.
I've since generated a work-around but I still do not understand how to
write

Value = CType( bad1, bad2.GetType())
or
Value = CType( bad1, Type.GetType(bad2))

That is, given a variable, bad2, how to convert the type of another
variable, bad1, to agree in type - assuming of course that they are
compatible variables.

In the above Value and bad2 are boolean and bad1 is string (like "False".
Also reading the terms "Type Object" and "Type" confuse me.

Thanks
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
" Just Me" <gr****@a-znet.com> schrieb:
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\ keyName)


Are you sure that there should be a space in front of "keyName"?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
Instead of doing:

GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())

Try doing:

GetValue = UserKey.GetValue(valueName, badKey).GetType()

This will give you the returned type from user key. However, assuming that
GetValue() will return "False", the above code will return System.String
(because "False" will be interpreted as a string). You will proabably have
to write some logic to take into account the strings "false" and "true" to be
converted into boolean types.

Here are the differences between System.Type and System.Object

http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...classtopic.asp

"Just Me" wrote:
Public Shared Function GetValue(ByVal keyName As String, ByVal valueName As
String, Optional ByVal badKey As Object = "") As Object

Dim UserKey As RegistryKey =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\ keyName)

If Not UserKey Is Nothing Then

GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())

lCurrentUserKey.Close()

Else

GetValue = badKeyReturn

End If

End Function

If the value in the registry is a Boolean, say False,

lCurrentUserKey.GetValue returns a string, like "False".

I'd like the routine to return the type stored (Boolean in this example)

so I tried to set the return to agree with badKey

but the compiler doesn't like

badKeyReturn.GetType() , says it's not defined

I've played around enough to know the problem is that CType expects a type
and doesn't believe I've given it one.

Know how to fix it??

Thanks


Nov 21 '05 #4

P: n/a

GetValue() will return "False", the above code will return System.String
(because "False" will be interpreted as a string). You will proabably
have
to write some logic to take into account the strings "false" and "true" to
be
converted into boolean types.


I was tring to generate the logic you mentioned above.
I'm trying to convert the return from GetValue to the type of badKey since
badKey will always have the desired type.
If I remember correctly, badKey.GetType() returns a Type Object and CType
expects a Type so I get an error here.
GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())

Thanks for replying


Nov 21 '05 #5

P: n/a
CType is compiled inline is probably the problem.

CType(UserKey.GetValue(valueName, badKey), badKey.gettype())

at compile time badKey is not a type

is there a way other than ctype?

" Just Me" <gr****@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Public Shared Function GetValue(ByVal keyName As String, ByVal valueName
As String, Optional ByVal badKey As Object = "") As Object

Dim UserKey As RegistryKey =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\ keyName)

If Not UserKey Is Nothing Then

GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())

lCurrentUserKey.Close()

Else

GetValue = badKeyReturn

End If

End Function

If the value in the registry is a Boolean, say False,

lCurrentUserKey.GetValue returns a string, like "False".

I'd like the routine to return the type stored (Boolean in this example)

so I tried to set the return to agree with badKey

but the compiler doesn't like

badKeyReturn.GetType() , says it's not defined

I've played around enough to know the problem is that CType expects a type
and doesn't believe I've given it one.

Know how to fix it??

Thanks


Nov 21 '05 #6

P: n/a
CType() and DirectCast() are both compiled inline, which increases
performance rather than using Reflection which would be slower.

If you are using only primative types (as much as I hate hardcoding things),
you might consider using a select case statement:

Dim mytype As String
myType = badKey.GetType().ToString()

Select Case myType
Case "System.Boolean"
GetValue = CType(UserKey.GetValue(valueName, badKey), System.Boolean)
Case "System.Int32"
'etc.
End Select

This will work if you have a limited number of types your are working with
(primatives). Alternatively, you can use reflection in the System.Reflection
namespace and try to create that way, which will require some work to do.

"Just Me" wrote:
CType is compiled inline is probably the problem.

CType(UserKey.GetValue(valueName, badKey), badKey.gettype())

at compile time badKey is not a type

is there a way other than ctype?

" Just Me" <gr****@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Public Shared Function GetValue(ByVal keyName As String, ByVal valueName
As String, Optional ByVal badKey As Object = "") As Object

Dim UserKey As RegistryKey =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\ keyName)

If Not UserKey Is Nothing Then

GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())

lCurrentUserKey.Close()

Else

GetValue = badKeyReturn

End If

End Function

If the value in the registry is a Boolean, say False,

lCurrentUserKey.GetValue returns a string, like "False".

I'd like the routine to return the type stored (Boolean in this example)

so I tried to set the return to agree with badKey

but the compiler doesn't like

badKeyReturn.GetType() , says it's not defined

I've played around enough to know the problem is that CType expects a type
and doesn't believe I've given it one.

Know how to fix it??

Thanks



Nov 21 '05 #7

P: n/a
Thanks for your continuing interest. I think I solved it thusly:

Convert.ChangeType does not appear to be "compile time"

GetValue = Convert.ChangeType(lCurrentUserKey.GetValue(valueN ame,
badKeyReturn), badKeyReturn.GetType())

Comment?

Thanks again

"rmacias" <rm*****@newsgroup.nospam> wrote in message
news:69**********************************@microsof t.com...
CType() and DirectCast() are both compiled inline, which increases
performance rather than using Reflection which would be slower.

If you are using only primative types (as much as I hate hardcoding
things),
you might consider using a select case statement:

Dim mytype As String
myType = badKey.GetType().ToString()

Select Case myType
Case "System.Boolean"
GetValue = CType(UserKey.GetValue(valueName, badKey), System.Boolean)
Case "System.Int32"
'etc.
End Select

This will work if you have a limited number of types your are working with
(primatives). Alternatively, you can use reflection in the
System.Reflection
namespace and try to create that way, which will require some work to do.

"Just Me" wrote:
CType is compiled inline is probably the problem.

CType(UserKey.GetValue(valueName, badKey), badKey.gettype())

at compile time badKey is not a type

is there a way other than ctype?

" Just Me" <gr****@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Public Shared Function GetValue(ByVal keyName As String, ByVal
> valueName
> As String, Optional ByVal badKey As Object = "") As Object
>
> Dim UserKey As RegistryKey =
> Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\
> keyName)
>
> If Not UserKey Is Nothing Then
>
> GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())
>
> lCurrentUserKey.Close()
>
> Else
>
> GetValue = badKeyReturn
>
> End If
>
> End Function
>
> If the value in the registry is a Boolean, say False,
>
> lCurrentUserKey.GetValue returns a string, like "False".
>
> I'd like the routine to return the type stored (Boolean in this
> example)
>
> so I tried to set the return to agree with badKey
>
> but the compiler doesn't like
>
> badKeyReturn.GetType() , says it's not defined
>
>
>
> I've played around enough to know the problem is that CType expects a
> type
> and doesn't believe I've given it one.
>
>
>
> Know how to fix it??
>
>
>
> Thanks
>
>
>
>
>
>


Nov 21 '05 #8

P: n/a
Excellent find! I was interested if your situation because the nature of the
programs I write at work must be very dynamic and flexible. I use a lot of
reflection in my .NET apps and I can envision myself running into your
situation.

I've never used the Convert.ChangeType() method before, although I do use
the other Convert.ToXXX methods frequently. I'll have to remember that one.

Thanks!

"Just Me" wrote:
Thanks for your continuing interest. I think I solved it thusly:

Convert.ChangeType does not appear to be "compile time"

GetValue = Convert.ChangeType(lCurrentUserKey.GetValue(valueN ame,
badKeyReturn), badKeyReturn.GetType())

Comment?

Thanks again

"rmacias" <rm*****@newsgroup.nospam> wrote in message
news:69**********************************@microsof t.com...
CType() and DirectCast() are both compiled inline, which increases
performance rather than using Reflection which would be slower.

If you are using only primative types (as much as I hate hardcoding
things),
you might consider using a select case statement:

Dim mytype As String
myType = badKey.GetType().ToString()

Select Case myType
Case "System.Boolean"
GetValue = CType(UserKey.GetValue(valueName, badKey), System.Boolean)
Case "System.Int32"
'etc.
End Select

This will work if you have a limited number of types your are working with
(primatives). Alternatively, you can use reflection in the
System.Reflection
namespace and try to create that way, which will require some work to do.

"Just Me" wrote:
CType is compiled inline is probably the problem.

CType(UserKey.GetValue(valueName, badKey), badKey.gettype())

at compile time badKey is not a type

is there a way other than ctype?

" Just Me" <gr****@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Public Shared Function GetValue(ByVal keyName As String, ByVal
> valueName
> As String, Optional ByVal badKey As Object = "") As Object
>
> Dim UserKey As RegistryKey =
> Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\
> keyName)
>
> If Not UserKey Is Nothing Then
>
> GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())
>
> lCurrentUserKey.Close()
>
> Else
>
> GetValue = badKeyReturn
>
> End If
>
> End Function
>
> If the value in the registry is a Boolean, say False,
>
> lCurrentUserKey.GetValue returns a string, like "False".
>
> I'd like the routine to return the type stored (Boolean in this
> example)
>
> so I tried to set the return to agree with badKey
>
> but the compiler doesn't like
>
> badKeyReturn.GetType() , says it's not defined
>
>
>
> I've played around enough to know the problem is that CType expects a
> type
> and doesn't believe I've given it one.
>
>
>
> Know how to fix it??
>
>
>
> Thanks
>
>
>
>
>
>


Nov 21 '05 #9

P: n/a
Excellent find! I was interested because the nature of the programs at I
write require them to be dynamic and flexiable. I use a lot of reflection
and can envision myself running into the same problem as you did. I've never
used the Convert.ChangeType() method before, although I do use the over
Convert.ToXXX methods frequently. Thanks for the info!

"Just Me" wrote:
Thanks for your continuing interest. I think I solved it thusly:

Convert.ChangeType does not appear to be "compile time"

GetValue = Convert.ChangeType(lCurrentUserKey.GetValue(valueN ame,
badKeyReturn), badKeyReturn.GetType())

Comment?

Thanks again

"rmacias" <rm*****@newsgroup.nospam> wrote in message
news:69**********************************@microsof t.com...
CType() and DirectCast() are both compiled inline, which increases
performance rather than using Reflection which would be slower.

If you are using only primative types (as much as I hate hardcoding
things),
you might consider using a select case statement:

Dim mytype As String
myType = badKey.GetType().ToString()

Select Case myType
Case "System.Boolean"
GetValue = CType(UserKey.GetValue(valueName, badKey), System.Boolean)
Case "System.Int32"
'etc.
End Select

This will work if you have a limited number of types your are working with
(primatives). Alternatively, you can use reflection in the
System.Reflection
namespace and try to create that way, which will require some work to do.

"Just Me" wrote:
CType is compiled inline is probably the problem.

CType(UserKey.GetValue(valueName, badKey), badKey.gettype())

at compile time badKey is not a type

is there a way other than ctype?

" Just Me" <gr****@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
> Public Shared Function GetValue(ByVal keyName As String, ByVal
> valueName
> As String, Optional ByVal badKey As Object = "") As Object
>
> Dim UserKey As RegistryKey =
> Microsoft.Win32.Registry.CurrentUser.OpenSubKey("S oftware\JUNK\
> keyName)
>
> If Not UserKey Is Nothing Then
>
> GetValue = CType(UserKey.GetValue(valueName, badKey), badKey.GetType())
>
> lCurrentUserKey.Close()
>
> Else
>
> GetValue = badKeyReturn
>
> End If
>
> End Function
>
> If the value in the registry is a Boolean, say False,
>
> lCurrentUserKey.GetValue returns a string, like "False".
>
> I'd like the routine to return the type stored (Boolean in this
> example)
>
> so I tried to set the return to agree with badKey
>
> but the compiler doesn't like
>
> badKeyReturn.GetType() , says it's not defined
>
>
>
> I've played around enough to know the problem is that CType expects a
> type
> and doesn't believe I've given it one.
>
>
>
> Know how to fix it??
>
>
>
> Thanks
>
>
>
>
>
>


Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.