visual studio 2015
Public Class Form1 'File name constant Private Const strFileName As String = 'C:\Temp\Hello.txt'定义模块级别的常量,如下图:
按钮1中的代码Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click 'Using a constant MessageBox.Show('1: ' & strFileName, 'Constants Demo')End Sub
按钮2中的代码Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click 'Using a constant MessageBox.Show('2: ' & strFileName, 'Constants Demo')End Sub
按钮3中的代码Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click 'Using a constant MessageBox.Show('3: ' & strFileName, 'Constants Demo')End Sub
界面及执行结果,效果图如下:
Public Const intHoursAsleepPerDay As Integer = 8Public Const intRichardsTypicalState As DayAction = DayAction.AtWork
Public Structure Customer 'Public members Public FirstName As String Public LastName As String Public Email As StringEnd Structure
完整代码如下:Public Structure Customer 'Public members Public FirstName As String Public LastName As String Public Email As String Public Overrides Function ToString() As String Return Name & ' (' & Email & ')' End FunctionEnd Structure
Private Sub btnListCustomer_Click(sender As Object, e As EventArgs) Handles btnListCustomer.Click 'Create a new customer Dim objCustomer As Customer objCustomer.FirstName = 'Michael' objCustomer.LastName = 'Dell' objCustomer.Email = 'mdell@somecompany.com' 'Display the customer DisplayCustomer(objCustomer)End Sub''===========================Public Sub DisplayCustomer(customer As Customer) 'Display the customer details on the form txtFirstName.Text = customer.FirstName txtLastName.Text = customer.LastName txtEmail.Text = customer.EmailEnd Sub
'Name property Public ReadOnly Property Name() As String Get Return FirstName & ' ' & LastName End Get End Property