
正文
vb.net类的写法 vbnet类举例
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
vb.net 如何在一个项目中创建类,并且使用他?
在任意form类或模块中都可以创建类,跟建立函数和过程一样。
示例:
public class form1
‘创建一个属于form的子类,名称为【类名】。
public class 类名
’定义类成员text
public text as string
end class
‘定义一个【类名】类的公共变量。
dim 类名1 as 类名
’定义一个过程,使用【类名】类的text成员
public sub 过程
‘实例化【类名1】
类名1=new 类名
类名1.text=“赋值”
’定义一个【新类】类的私用变量,并实例化。
dim 新类1 as new 新类
新类1.name=“一个字符串”
新类1.age=12
end sub
end class
‘’‘创建一个与form同级的类,名称为【新类】
public class 新类
public sub new()
end sub
public name as string
public age as integer
end class
在模块中使用:
public class module1
dim a as new form1.类名
end module
相关问答
Q1: vb.net 写一个类?
Public Class Name1
Public Function fun1()
...
End Function
....
End Class
Q2: VB.NET 如何带参数构造函数对象或是类
public structure struc
public name as string
public shengao as integer
……
end structure
public items as struc()
readonly property people(argname as string) as struc
get
for each i as struc in items
if i.name=argname then reture i
next
end get
end property
struc可以用class,property可以用function,people通过参数返回一个对象,对象可以来源于某个数组的某个元素,也可以是其他来源。
people也可以是类的构造方法,而shengao等是类的成员,但你的写法是错误的,构造方法必须用new实例化
Q3: VB.NET中标准的实体类怎么写的,请教VB高手指导
'代码来源:代码街codejie.net
'由于在vb.net里变量名不区分大小写(StuName和stuname是一样的),所以个别变量名需要修改
'在vb.net中,模块级变量的命名原则是以m_开头
Public Class Student
Private m_stuNo As String '学生号字段
Private m_stuName As String '学生姓名字段
Private m_stuAge As Integer '学生年龄字段
Private m_stuAddress As String '学生住址字段
'无参构造方法
Public Sub New()
End Sub
'有参构造方法
Public Sub New(ByVal stuNo As String, ByVal stuName As String, ByVal stuAge As Integer, ByVal stuAddress As String)
m_stuNo = stuNo
m_stuName = stuName
m_stuAge = stuAge
m_stuAddress = stuAddress
End Sub
'学生号属性
Public Property StuNo() As String
Get
Return m_stuNo
End Get
Set(ByVal value As String)
m_stuNo = value
End Set
End Property
'学生姓名属性
Public Property StuName() As String
Get
Return m_stuName
End Get
Set(ByVal value As String)
m_stuName = value
End Set
End Property
'学生年龄属性
Public Property StuAge() As Integer
Get
Return m_stuAge
End Get
Set(ByVal value As Integer)
m_stuAge = value
End Set
End Property
'学生住址属性
Public Property StuAddress() As String
Get
Return m_stuAddress
End Get
Set(ByVal value As String)
m_stuAddress = value
End Set
End Property
End Class
关于vb.net类的写法和vbnet类举例的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







