[英]'Microsoft.Win32.Registry' is a 'type' but is used like a 'variable'
I'm trying to convert some VB.net code into C# and having issues trying to convert this one line. 我正在尝试将一些VB.net代码转换为C#,并且在尝试转换这一行时遇到问题。
VB.NET VB.NET
Dim myreg As Microsoft.Win32.Registry
I know it's a static so I can't create an instance but when I tried a VB.NET converter to C# the website gives me this: 我知道它是静态的,所以我不能创建一个实例但是当我尝试使用VB.NET转换器到C#时,网站给了我这个:
Microsoft.Win32.Registry myreg = null;
And gives me an error message: 'Microsoft.Win32.Registry' is a 'type' but is used like a 'variable' 并给我一个错误信息:'Microsoft.Win32.Registry'是'类型',但用作'变量'
In the last part of the Function in VB.NET, myreg is used: 在VB.NET中的函数的最后一部分中,使用了myreg:
Finally
myreg = Nothing
End Try
Here's the whole function: 这是整个功能:
Imports System.Data.OleDb
Imports System.Data
Imports Microsoft.Win32.RegistryKey
Imports MyLogger 'custom reference, not worried about this.
Private Function getRegistryString(ByVal sVal As String, Optional ByVal sKey As String = "") As String
Dim myreg As Microsoft.Win32.Registry
Dim s As String
Try
s = myreg.LocalMachine.OpenSubKey(sKey).GetValue(sVal)
Catch ex As Exception
l.EventLog(ex.Message, EventLogEntryType.Error)
Throw ex
Finally
myreg = Nothing
End Try
Return s
s = Nothing
End Function
Microsoft.Win32.Registry
is a static class so you cannot instantiate it. Microsoft.Win32.Registry
是一个静态类,因此您无法实例化它。
http://msdn.microsoft.com/library/microsoft.win32.registry.aspx http://msdn.microsoft.com/library/microsoft.win32.registry.aspx
Edit: Should have been a comment instead of an answer. 编辑:应该是评论而不是答案。 I'm still new to this site sorry. 我还是这个网站的新手抱歉。
In VB.NET, there is no way to define a shared (static) class. 在VB.NET中,无法定义共享 (静态)类。 In C#, however, you can specify that a class is static. 但是,在C#中,您可以指定类是静态的。 When a class is static, it forces all its members to be static as well. 当一个类是静态的时,它也强制所有成员都是静态的。 The compiler will give you an error if you try to declare a variable of a static type. 如果尝试声明静态类型的变量,编译器将给出错误。
In VB, however, since there is no such thing as a shared class, it always allows you to declare a variable of any type, even though it's entirely pointless because the instance will have no members. 但是,在VB中,由于没有共享类这样的东西,它总是允许你声明任何类型的变量,即使它完全没有意义,因为实例没有成员。
Instead of declaring a variable as type Registry
, you need to change the code to simply use the Registry
class name, itself. 您需要更改代码以简单地使用Registry
类名本身,而不是将变量声明为类型Registry
。 Setting the variable to Nothing
is meaningless because the variable never stores a reference to an object. 将变量设置为Nothing
是没有意义的,因为变量从不存储对对象的引用。 It's always Nothing
. 它永远都Nothing
。
Your function should read like this: 你的功能应该是这样的:
Private Function getRegistryString(ByVal sVal As String, Optional ByVal sKey As String = "") As String
Dim s As String
Try
s = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey).GetValue(sVal)
Catch ex As Exception
l.EventLog(ex.Message, EventLogEntryType.Error)
Throw ex
End Try
Return s
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.