What's wrong with this registry code

J

Just Me

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("Software\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
 
H

Herfried K. Wagner [MVP]

Just Me said:
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\JUNK\ keyName)

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

Just Me

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
 
G

Guest

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/d...n-us/cpref/html/frlrfsystemtypeclasstopic.asp
http://msdn.microsoft.com/library/d...us/cpref/html/frlrfsystemobjectclasstopic.asp
 
J

Just Me

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
 
J

Just Me

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?
 
G

Guest

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

Just Me

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(valueName,
badKeyReturn), badKeyReturn.GetType())

Comment?



Thanks again
 
G

Guest

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!
 
G

Guest

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top