vb.net基类 VBNet

vb.net或者c#.net中,派生类如何删除基类成员?

在.NET中,彻底的删除应该是不可能的

站在用户的角度思考问题,与客户深入沟通,找到天柱网站设计与天柱网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、做网站、企业官网、英文网站、手机端网站、网站推广、域名注册、网页空间、企业邮箱。业务覆盖天柱地区。

如果你反编译一下就可以知道

这个问题就好像两个人生个孩子,你说盲肠没什么用,还容易发生危险(需要手术),那生下来就没有多好,这个要求似乎优点不合理

vb.net派生类重写基类函数的问题?

vb.net的MyBase(mybase)与java的super能基本对应。

下图是一个例子:

VB.NET实例化类

可以,但是变量不能用ClassA或B来定义,可以用Object,或ClassA,ClassB的基类,,使用类的成员时,再把变量类型转换成相应的类

VB.NET窗体继承问题

在继承类中添加了空的事件过程,估计也会调用基类事件过程的,也就是两个事件过程都会被调用。可以试试在继承类构造函数中用 RemoveHandler 语句能不能撤销基类的事件过程的绑定。

另外一般的过程可以用override关键字隐藏基类的同名方法,比如基类调用的方法被继承类override重写后,就会调用继承类的方法了。所以可以把事件处理代码放在一普通过程中,由事件过程调用,这样继承类可以用空的同名方法覆盖基类方法了。

vb.net有没有类似vb6.0控件数组的功能

可以实现

首先创建一个Button类型控件数组:

1、创建“Windows应用程序”类型的工程,添加名为ButtonArray的类,并使该类继承 System.Collection.CollectionBase 类。System.Collections.CollectionBase类是.NET框架类库中为集合操作提供抽象的基类,通过对它的继承可以为我们的ButtonArray类具备集合增加、删除、索引的功能。

2、为ButtonArray类添加ParentForm属性,即控件组所在窗体,创建初始化函数(构造函数);

3、为控件数组类增加AddItem方法,该方法在控件数组类中添加成员;

4、为控件数组类增加RemoveItem方法,该方法在控件数组中删除一个成员。

示例代码:

Public Class ButtonArray

Inherits System.Collections.CollectionBase

Private ReadOnly ParentForm As System.Windows.Forms.Form

Public Sub New(ByVal pForm As System.Windows.Forms.Form)

ParentForm = pForm

End Sub

Default Public ReadOnly Property Item(ByVal index As Integer) As System.Windows.Forms.Button

Get

Return Me.List.Item(index) ' ButtonArray的List 属性从CollectionBase 继承

End Get

End Property

Public Sub AddItem()

Dim btnItem As New System.Windows.Forms.Button

Me.List.Add(btnItem)

ParentForm.Controls.Add(btnItem) '向窗体中增加控件

btnItem.Tag = Me.Count 'Count属性从CollectionBase 继承

btnItem.Top = Me.Count * 30

btnItem.Left = 200

btnItem.Text = "Button" Me.Count.ToString

AddHandler btnItem.Click, AddressOf btnItem_Click '绑定事件处理程序

End Sub

Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Button)

Me.List.Add(btnItem)

AddHandler btnItem.Click, AddressOf btnItem_Click '绑定事件处理程序

End Sub

Public Sub RemoveItem()

If Me.Count 0 Then

ParentForm.Controls.Remove(Me(Me.Count - 1))

Me.List.RemoveAt(Me.Count - 1)

End If

End Sub

Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)

'在这里编写控件数组对点击事件的响应

'例如:

MsgBox("点击:" sender.GetType().ToString CType(CType(sender, Button).Tag, String))

End Sub

End Class

使用创建的控件数组

在Form1中放置两个按钮Button1、Button2,分别测试控件数组的增添、删除。

双击Form添加代码:

Public Class Form1

Inherits System.Windows.Forms.Form

……Windows窗体设计器生成的代码……

Dim Buttons As New ButtonArray(Me)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Buttons.AddItem()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Buttons.RemoveItem()

End Sub

End Class

其他的控件数组也可以用类似的方式来实现

例如 Label控件数组

LabelArray.vb代码如下:

Public Class LabelArray

Inherits System.Collections.CollectionBase

Private ReadOnly ParentForm As System.Windows.Forms.Form

Public Sub New(ByVal pForm As System.Windows.Forms.Form)

ParentForm = pForm

End Sub

Default Public ReadOnly Property Item(ByVal index As Integer) As System.Windows.Forms.Label

Get

Return Me.List.Item(index) ' ButtonArray的List 属性从CollectionBase 继承

End Get

End Property

Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Label)

Me.List.Add(btnItem)

AddHandler btnItem.Click, AddressOf btnItem_Click '绑定事件处理程序

End Sub

Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)

'在这里编写控件数组对点击事件的响应

'例如:

MsgBox("点击:" sender.GetType().ToString CType(CType(sender, Label).Tag, String))

End Sub

End Class

使用创建的Label控件

在Form1中放置两个按钮Label1、Label2

双击Form添加代码:

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()

MyBase.New()

'该调用是 Windows 窗体设计器所必需的。

InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

'用来绑定label

BindArray()

End Sub

……Windows窗体设计器生成的其他代码……

#End Region

Dim Labels As New LabelArray(Me)

Public Sub BindArray()

Me.Label1.Tag = "1111"

Me.Label2.Tag = "222"

Labels.AddItem(Me.Label1)

Labels.AddItem(Me.Label2)

End Sub

End Class

然后可以测试点击两个label可以显示相应的Tag的信息。

VB.NET没有操作XML的基类吗?高手救命啊

使用System.XML

Imports Microsoft.VisualBasic

Imports System

Imports System.IO

Imports System.Xml

namespace HowTo.Samples.XML

public class WriteXmlFileSample

private const document as string = "newbooks.xml"

shared sub Main()

Dim myWriteXmlFileSample as WriteXmlFileSample

myWriteXmlFileSample = new WriteXmlFileSample()

myWriteXmlFileSample.Run(document)

end sub

public sub Run(args As String)

Dim myXmlTextReader as XmlTextReader = nothing

Dim myXmlTextWriter as XmlTextWriter = nothing

try

myXmlTextWriter = new XmlTextWriter (args, nothing)

myXmlTextWriter.Formatting = System.Xml.Formatting.Indented

myXmlTextWriter.WriteStartDocument(false)

myXmlTextWriter.WriteDocType("bookstore", nothing, "books.dtd", nothing)

myXmlTextWriter.WriteComment("此文件表示书店库存数据库的另一个片断")

myXmlTextWriter.WriteStartElement("bookstore")

myXmlTextWriter.WriteStartElement("book", nothing)

myXmlTextWriter.WriteAttributeString("genre","autobiography")

myXmlTextWriter.WriteAttributeString("publicationdate","1979")

myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9")

myXmlTextWriter.WriteElementString("title", nothing, "The Autobiography of Mark Twain")

myXmlTextWriter.WriteStartElement("Author", nothing)

myXmlTextWriter.WriteElementString("first-name", "Mark")

myXmlTextWriter.WriteElementString("last-name", "Twain")

myXmlTextWriter.WriteEndElement()

myXmlTextWriter.WriteElementString("price", "7.99")

myXmlTextWriter.WriteEndElement()

myXmlTextWriter.WriteEndElement()

'向文件写 XML 并关闭编写器

myXmlTextWriter.Flush()

myXmlTextWriter.Close()

' 读取返回的文件并进行分析以确保正确生成 XML

myXmlTextReader = new XmlTextReader (args)

FormatXml (myXmlTextReader, args)

catch e as Exception

Console.WriteLine ("异常:{0}", e.ToString())

finally

Console.WriteLine()

Console.WriteLine("对文件 {0} 的处理已完成。", args)

If Not myXmlTextReader Is Nothing

myXmlTextReader.Close()

end if

'关闭编写器

If Not myXmlTextWriter Is Nothing

myXmlTextWriter.Close()

end if

End try

End Sub

private shared Sub FormatXml (reader as XmlTextReader, filename as String)

Dim piCount, docCount, commentCount, elementCount as Integer

Dim attributeCount, textCount, whitespaceCount as Integer

While reader.Read()

Select (reader.NodeType)

case XmlNodeType.ProcessingInstruction:

Format (reader, "ProcessingInstruction")

piCount += 1

case XmlNodeType.DocumentType:

Format (reader, "DocumentType")

docCount += 1

case XmlNodeType.Comment:

Format (reader, "Comment")

commentCount += 1

case XmlNodeType.Element:

Format (reader, "Element")

elementCount += 1

While reader.MoveToNextAttribute()

Format (reader, "Attribute")

end While

if (reader.HasAttributes)

attributeCount += reader.AttributeCount

end if

case XmlNodeType.Text:

Format (reader, "Text")

textCount += 1

case XmlNodeType.Whitespace:

whitespaceCount += 1

End Select

End While

' 显示该文件的统计信息

Console.WriteLine ()

Console.WriteLine("{0} 文件的统计信息", filename)

Console.WriteLine ()

Console.WriteLine("处理指令:" piCount)

Console.WriteLine("文档类型:" docCount)

Console.WriteLine("注释:" commentCount)

Console.WriteLine("元素:" elementCount)

Console.WriteLine("属性:" attributeCount)

Console.WriteLine("文本:" textCount)

Console.WriteLine("空白:" whitespaceCount)

End Sub

private shared Sub Format(byref reader as XmlTextReader , NodeType as String)

' 格式化输出

Console.Write(reader.Depth " ")

Console.Write(reader.AttributeCount " ")

Dim i as Integer

for i = 0 to reader.Depth - 1

Console.Write(Strings.chr(9))

Next

Console.Write(reader.Prefix NodeType "" reader.Name "" reader.Value)

Console.WriteLine()

End Sub

End Class

End Namespace

参考:


网站栏目:vb.net基类 VBNet
转载来源:http://ybzwz.com/article/hhphps.html