读书人

该用Shared仍是该用类

发布时间: 2012-10-31 14:37:32 作者: rapoo

该用Shared还是该用类?
一个读到cpu温度的代码,只是偶尔用一次,代码也很短,问题是我该放到shared里面还是专门用一个类来装它,因为要专门为这个引用System.Management。

' ' ' <summary>
' ' ' 取得CPU 度
' ' ' </summary>
Public Shared Function CPU_Temperature() As Integer
Try
Dim Q As String = "Select * From MSAcpi_ThermalZoneTemperature "
Dim mos As New ManagementObjectSearcher( "root\WMI ", Q)
For Each mo As ManagementObject In mos.Get
Return CInt(Convert.ToInt32(CInt(mo.GetPropertyValue( "CurrentTemperature ")) - 2732) / 10)
Next
Catch ex As Exception
End Try
Return 0
End Function


或者//////////////////////////////////////
Public Class CPU_Temperature
' ' ' <summary>
' ' ' 取得CPU 度
' ' ' </summary>
Public Shared Function CPU_Temperature() As Integer
Try
Dim Q As String = "Select * From MSAcpi_ThermalZoneTemperature "
Dim mos As New ManagementObjectSearcher( "root\WMI ", Q)
For Each mo As ManagementObject In mos.Get
Return CInt(Convert.ToInt32(CInt(mo.GetPropertyValue( "CurrentTemperature ")) - 2732) / 10)
Next
Catch ex As Exception
End Try
Return 0
End Function
End Class


[解决办法]
建议封装类,以便后面的扩充

读书人网 >VB Dotnet

热点推荐