473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What's wrong with this registry code

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
9 1256
" 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
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
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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
3
by: Mike Malter | last post by:
On my XP Professional dev machine, I am able to read the registry by giving permissions to the ASP.NET account on the local machine. When I transfer the code to my staging server (Windows 2003),...
7
by: MrNobody | last post by:
I was a Java developer so I'm used to using property files as a means to keep configuration settings for my apps. I'm wondering what options are there with ..NET? Some settings I want to include...
16
by: Larry Woods | last post by:
I need to persist user selections. I know that I can use the Registry, but I was hoping that there was some sort of "text based" solution. TIA, Larry Woods
2
by: Benoit Martin | last post by:
I'm developping a windows app that will be sold to different clients. The app connects to a Database located on my server. That app is installed on multiple machines for each client. Each client...
5
by: Steve Bottoms | last post by:
Hi, all... Trying to determine how to check the state of OpenSubKey when called. What is the return value if the requested subkey doesn't exist?? I try a GetType on the result but only get a...
4
by: Allen | last post by:
Installing VS2005 I received the following. Do you know what it implies? Thanks 03/22/06,21:12:08] Microsoft Visual Studio 2005 Professional Edition - ENU: ERROR:Error 1919.Error configuring...
2
by: =?Utf-8?B?QW5kcmUgS2xpbW92?= | last post by:
Hi! Situation: simple code read registry value (OpenSubKey -GetValue, as usual). Requested value exists in registry and has type DWORD, read into the variable UInt32, access rights granted. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.