Linux(mono)でVB、プロパティ

次はプロパティを使っていきます。
AnimalクラスにPrivateの_myname変数を追加して、変数にアクセスするためのMynameプロパティを実装しました。Getは値の取得、Setは値の設定です。

takk@deb9:~/vbtest$ cat -n test.vb
     1  Imports System.Console
     2
     3  Public Class Animal
     4          Public classname As String = "哺乳類"
     5          Private _myname As String
     6          Public Property Myname() As String
     7                  Get
     8                          Return _myname
     9                  End Get
    10                  Set(ByVal value As String)
    11                          _myname = value
    12                  End Set
    13          End Property
    14          Sub Tell()
    15                  WriteLine( classname & "です")
    16          End Sub
    17  End Class
    18
    19  Public Class Dog : inherits Animal
    20          Public familyname As String = "犬"
    21          Sub Tell()
    22                  WriteLine( MyBase.classname & "で" & familyname & "です。" & _
    23                          "名前を" & Myname & "といいます。" )
    24          End Sub
    25          Sub Bark()
    26                  WriteLine( "Bowwow" )
    27          End Sub
    28  End Class
    29
    30  Public Class TestClass
    31          <STAThread()> _
    32          Shared Sub Main()
    33                  dim dog as new Dog()
    34                  dog.Myname = "ケンケン"
    35                  dog.Tell()
    36                  dog.Bark()
    37          End Sub
    38
    39  End Class
takk@deb9:~/vbtest$

実行すると、

takk@deb9:~/vbtest$ vbnc test.vb

~省略~

takk@deb9:~/vbtest$ mono test.exe
哺乳類で犬です。名前をケンケンといいます。
Bowwow
takk@deb9:~/vbtest$

Animalクラスに_myname変数を組み込んだのは、動物が何か分からなくても名前が付けることができるからです。
逆に哺乳類という分類は変わることがないので、リードオンリーのプロパティにしてみます。

takk@deb9:~/vbtest$ cat -n test.vb
     1  Imports System.Console
     2
     3  Public Class Animal
     4          Private _classname As String = "哺乳類"
     5          Private _myname As String
     6          Public ReadOnly Property Classname() As String
     7                  Get
     8                          Return _classname
     9                  End Get
    10          End Property
    11          Public Property Myname() As String
    12                  Get
    13                          Return _myname
    14                  End Get
    15                  Set(ByVal value As String)
    16                          _myname = value
    17                  End Set
    18          End Property
    19          Sub Tell()
    20                  WriteLine( classname & "です")
    21          End Sub
    22  End Class
    23
    24  Public Class Dog : inherits Animal
    25          Public familyname As String = "犬"
    26          Sub Tell()
    27                  WriteLine( MyBase.classname & "で" & familyname & "です。" & _
    28                          "名前を" & Myname & "といいます。" )
    29          End Sub
    30          Sub Bark()
    31                  WriteLine( "Bowwow" )
    32          End Sub
    33  End Class
    34
    35  Public Class TestClass
    36          <STAThread()> _
    37          Shared Sub Main()
    38                  dim dog as new Dog()
    39                  dog.Classname = "爬虫類"
    40          End Sub
    41
    42  End Class
takk@deb9:~/vbtest$

ビルドしてみます。

takk@deb9:~/vbtest$ vbnc test.vb
Visual Basic.Net Compiler version 0.0.0.5943 (Mono 4.0.1 - tarball)
Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved.

vbnc : Command line : error VBNC99999: Unexpected error: The method or operation is not implemented.
  at vbnc.CecilHelper.GetCorrectMember (Mono.Cecil.MethodReference Member, Mono.Cecil.TypeReference Type, System.Boolean Emittable) [0x0005f] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.PropertyAccessClassification.GenerateCode (vbnc.EmitInfo Info) [0x000bb] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.AssignmentStatement.GenerateCode (vbnc.EmitInfo Info) [0x00025] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.CodeBlock.GenerateCode (vbnc.EmitInfo Info) [0x0014a] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.CodeBlock.GenerateCode (vbnc.IMethod Method) [0x00061] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.MethodBaseDeclaration.GenerateCode (vbnc.EmitInfo Info) [0x000ba] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.MethodDeclaration.GenerateCode (vbnc.EmitInfo Info) [0x00007] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.SubDeclaration.GenerateCode (vbnc.EmitInfo Info) [0x00046] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.Helper.GenerateCodeCollection (System.Collections.IEnumerable Collection, vbnc.EmitInfo Info) [0x00037] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.BaseObjects`1[T].GenerateCode (vbnc.EmitInfo Info) [0x00001] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.AssemblyDeclaration.Emit (vbnc.TypeDeclaration Type) [0x00031] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.AssemblyDeclaration.Emit () [0x0005f] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
  at vbnc.Compiler.Compile () [0x005b3] in <d77f4191aaaa4ed9a9a36d67dd32844d>:0
Compilation took 00:00:00.4572370
takk@deb9:~/vbtest$

リードオンリーのプロパティを書き換えしようとしたら、けたたましくエラーが出ました。

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA