我想知道如何将属性放在其他属性中,如下例所示。
我试过这个:
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Design
Imports System.ComponentModel
Namespace ClassTest_ParentProperty
Public Class Class_Parent : Inherits Control
Public Property MyProperties_Parent As Class_Child
Public Sub New()
MyBase.BackColor = Color.DarkSlateBlue
End Sub
End Class
Public Class Class_Child
Public Var_MyColor As Color = Color.Empty
Public Var_MyText As New String(Nothing)
Public Var_MySize As New Size(50, 50)
Public Property MyColor As Color
Get
Return Var_MyColor
End Get
Set(value As Color)
Var_MyColor = value
End Set
End Property
Public Property MyText As String
Get
Return Var_MyText
End Get
Set(value As String)
Var_MyText = value
End Set
End Property
Public Property MySize As Size
Get
Return Var_MySize
End Get
Set(value As Size)
Var_MySize = value
End Set
End Property
End Class
End Namespace
但输出是这样的: The readonly property without other properties inside
我为此搜索了很多,但遗憾的是我找不到答案。
答案 0 :(得分:0)
你的概念有点错误......有一些名为Class
es的东西是类型的数据......就像String
一样。 String
是一种具有自己的工作,目的(在本例中,代表文本)等的数据
所以根据这个问题,Font
属性的类型被称为Font
(System.Drawing.Font
)。
好的,这可能会令人困惑,所以让我们采取其他的东西......例如,Location
财产。 Location
的类型是Point
(System.Drawing.Point
)。 Point
是一个班级。一个班级可以持有房产。
以Form
为例。 Form
也是一个具有自己属性的类。其中一个Form
的属性是Font
,这是另一个名为System.Drawing.Font
的类的实例,而且还有一些属性,如Size
,Name
等。
要创建一个类,有几种方法:
要通过IDE创建一个类,请转到“PROJECT”选项卡并选择“Add Class ...”。如果您没有看到,请尝试点击“添加新项目...”并搜索“课程”。
要通过代码创建一个类,您可以开始输入一个类。实际上,这不是一个大问题,你只需要一个现有的代码文件,例如你的主表单,然后在表格End Class
之后,你开始输入类似Public Class MyClassName
的内容,然后按回车键。我的意思是一个例子:
Public Class Form1
'...
'...
'...
End Class
Public Class MyClassName 'Here YOUR Class starts.
'...
End Class 'This ends the MyClassName block.
<小时/> 如果您想知道如何在类中创建自己的属性,请查看此示例:(注意
[...]
表示一些可选的东西)
Public Class MyClassName 'This is your Class's beginning.
'To show you how to create Properties, look at this:
Public Property MyProperty1 As [New] String [= "some default value"] 'Here your Property is named MyProperty and the type is String.
'MyProperty1 is an one-liner. These do the storing and returning of values automatically. There are Property blocks also, like this:
Public Property MyProperty2 As String 'This is a Property block.
Get 'This is the code that'll be executed for getting the value. This will return a value in the end, just like a "Function".
'You can do stuff here too.
Return "Hello there!!!"
End Get 'This ends the Get block.'
Set(value As String) 'This is the code that'll be executed for setting the value. Note that the data type (String) should be the same as of the whole Property.
'Do stuff here to use this NewValue.
End Set 'This ends the Set block.'
End Property 'This ends the MyProperty2 block.'
End Class 'This ends the MyClassName block.
希望这很有用!