我对vb.net很新,所以我并不完全熟悉如何从一种形式调用公共函数。当鼠标悬停在" myIniFile"和" initGPIB"在类,frmReadTemp中,我收到错误消息 - 未声明。由于其保护级别,它可能无法访问。但是,该函数和sub在另一个类中是Public。
Public Class frmReadTemp
Private Sub cmdRun_Click(sender As Object, e As EventArgs) Handles cmdRun.Click
Dim err_code As eCode = eCode.ERR_NONEr
myInifile()
err_code = initGPIB()
End Sub
End Class
Public Class PowerSupply_setup
#Region "Variable Declarations"
Dim myIniFile As IniFile
Public instrInfo As New ArrayList
Public myiniObj As iniObj
WithEvents myPS As IPowerSupply
WithEvents myTestSystem As New testSystemObj(4)
#End Region
Public Sub loadIniFile()
Dim tenable As String
'Power Supply
myiniObj.ps_addr.PrimaryAddress = myIniFile.GetInteger("GPIB_INSTRUMENTS", "POWERSUPPLY_ADDR", 6)
myiniObj.ps_addr.ComInterface = 1
myiniObj.ps_type = myIniFile.GetString("GPIB_INSTRUMENTS", "POWERSUPPLY_TYPE", "HP6632").ToUpper
tenable = myIniFile.GetString("GPIB_INSTRUMENTS", "POWERSUPPLY_AVAILABLE", "NO")
myiniObj.ps_available = IIf(tenable.ToUpper.Trim.StartsWith("NO"), False, True)
'myiniObj.ps_available = IIf((myIniFile.GetString("GPIB_INSTRUMENTS", "POWERSUPPLY_AVAILABLE", 0)) = "0", False, True)
myiniObj.ps_port = myIniFile.GetInteger("GPIB_INSTRUMENTS", "POWERSUPPLY_PORT", 1)
myiniObj.ps_ov_offset = Val(myIniFile.GetInteger("GPIB_INSTRUMENTS", "POWERSUPPLY_OVERVOLTAGE_OFFSET", "3.0"))
myiniObj.ps_current_limit = Val(myIniFile.GetInteger("GPIB_INSTRUMENTS", "POWERSUPPLY_CURRENT_LIMIT", "3.0"))
End Sub
Public Function initGPIB() As swiErr.eCode
Dim err_code As eCode = eCode.ERR_NONE
If (instrInfo.Count = 0) Then
'init Power Supply
If (err_code = eCode.ERR_NONE And myiniObj.ps_available) Then
Dim iInfo2(1) As String
If (myiniObj.ps_type.ToUpper.Contains("HP6632")) Then
myPS = New HP6632(myiniObj.ps_addr)
err_code = myPS.ErrCode
ElseIf (myiniObj.ps_type.ToUpper.Contains("E3631")) Then
myPS = New E3631A(myiniObj.ps_addr)
err_code = myPS.ErrCode
ElseIf (myiniObj.ps_type.ToUpper.Contains("E66311")) Then
myPS = New E66311B(myiniObj.ps_addr)
err_code = myPS.ErrCode
ElseIf (myiniObj.ps_type.ToUpper.Contains("E66319")) Then
myPS = New E66311B(myiniObj.ps_addr)
err_code = myPS.ErrCode
ElseIf (myiniObj.ps_type.ToUpper.Contains("SCPI")) Then
myPS = New SCPI_PS(myiniObj.ps_addr)
err_code = myPS.ErrCode
Else
myPS = Nothing
err_code = eCode.ERR_INST_PS_NOT_SUPPORTED
End If
If (err_code = eCode.ERR_NONE) Then
iInfo2(0) = myPS.instrumentName
iInfo2(1) = myPS.gpibAddress.PrimaryAddress
myPS.selectPort = Me.myiniObj.ps_port
instrInfo.Add(iInfo2)
myPS.OverVoltageOffset = myiniObj.ps_ov_offset
myPS.setPwrOff()
End If
If (err_code = eCode.ERR_INST_GPIB_SEARCH) Then err_code = eCode.ERR_INST_MISSING_PWRSUPPLY
Else
'create object but set availability to false
myPS = New E3631A()
End If
Me.myTestSystem.instrument.PS = myPS
End If
Return err_code
End Function
End Class
答案 0 :(得分:1)
变量myIniFile
和方法initGPIB
确实是公开的,因此可以从PowerSupply_setup
类的外部访问它们。但是,由于它们是该类的成员,因此必须通过该类的对象引用它们。例如:
Dim mySetup As New PowerSupply_setup()
err_code = mySetup.initGPIB()
由于它们是实例成员(不是Shared
),因此必须创建类的实例(对象),然后才能通过该对象访问它们。如果您创建了该类的多个对象,那么您将拥有该成员的多个副本。
如果您只需要一个成员的副本,并且您不想创建一个只是为了使用它们的对象,您可以选择将它们视为该类的Shared
成员:< / p>
Public Class PowerSupply_setup
Public Shared Sub loadIniFile()
' ...
End Sub
' ...
End Class
然后您可以通过班级名称直接访问它们:
err_code = PowerSupply_setup.initGPIB()
就个人而言,我建议不要这样做,除非确实有必要,因为它会导致全球状态,这可能是危险的。但是,如果你确实想要走这条路,而且你甚至不想使用类名来访问这些成员,你也可以将它们放在Module
中,而不是放在git config core.ignorecase true
中。类。
答案 1 :(得分:0)
这里有不同的事情: