次の方法で共有


非共有メンバーを参照するには、オブジェクト参照が必要です

コード内で非共有メンバーを参照しましたが、オブジェクト参照を指定していませんでした。 共有されていないメンバーを修飾するためにクラス名自体は使用できません。 インスタンスは最初にオブジェクト変数として宣言してから、変数名によって参照しなければなりません。

エラー ID: BC30469

このエラーを解決するには

  1. インスタンスをオブジェクト変数として宣言します。

  2. 変数名によってインスタンスを参照します。

Imports System

Namespace Ecommerce
    Public Class Customer
        Private Property AccountNumber As Integer

        Public Sub New (accountNumber As Integer)
            AccountNumber = accountNumber
        End Sub

        Public Function GetAccountNumber ()
            return AccountNumber + accountNumber
        End Function
    End Class
End Namespace

Module Program
Sub Main(args As String())
        ' Declaring the instance as an object variable:
        Dim firstCustomer As New Ecommerce.Customer(1)
        Dim firstCustomerAccountNumber As Integer

        ' You must not use Ecommerce.Customer.GetAccountNumber() because you
        ' cannot access non-shared Function 'GetAccountNumber' in shared context.
        ' Otherwise, use the instance you've just created to call the function:
        firstCustomerAccountNumber = firstCustomer.GetAccountNumber()
        Console.WriteLine(firstCustomerAccountNumber)
    End Sub
End Module

関連項目