How do I access private constants in class attributes in VB.Net?
I have an API that I created and currently utilize successfully in C#. I am trying to create an example of interacting with the API in VB.NET (so that the QA without C# experience can still utilize it for creating automated tests).
In C# I do the following
[TestingForm(FormName= "Land Lines", CaseType= _caseType
, Application= ApplicationNameCodes.WinRDECode, HasActions= true)]
public class LandLines : RDEMainForm
{
// .. Irrelevant Code .. //
private const string _caseType = "Land Lines";
}
As someone with zero VB.Net experience, I created the following to try and mimic it
<TestingForm(Application:=ApplicationNames.WinRDE, FormName:=FORM_NAME, CaseType:=CASE_TYPE, HasActions:=True, IncludeBaseClassActions:=False)>
Public Class Permits
Inherits TestingBase
#Region "Constants"
Private Const FORM_开发者_StackOverflow社区NAME As String = "Permits" 'Display name for the test class (in the editor)
Private Const CASE_TYPE As String = "permits" 'Unique code for this test class, used when reading/saving test plans
#End Region
End Class
This gives me a compile time error because it claims that FORM_NAME
and CASE_TYPE
is not defined, even though the class has it defined inside.
How can I use the defined constants inside the class in the class attributes?
I'm actually quite surprised that the C# example compiles (but I checked it indeed does).
In VB.Net that type of access (a private member outside the type even in an attribute) is simply not legal. Instead you need to make it Friend
and qualify it's access
<TestingForm(Application:=ApplicationNames.WinRDE, FormName:=Permits.FORM_NAME, CaseType:=Permits.CASE_TYPE, HasActions:=True, IncludeBaseClassActions:=False)>
Public Class Permits
Inherits TestingBase
Friend Const FORM_NAME As String = "Permits" 'Display name for the test class (in the editor)
FriendConst CASE_TYPE As String = "permits" 'Unique code for this test class, used when reading/saving test plans
End Class
精彩评论