vb.net优化代码,VBNET的编程机制
如何用VB.NET StringBuilder优化字符串操作性能
有许多字符串操作技巧可以帮助你优化代码,以获得更好的性能。StringBuilder允许你执行重复的字符串操作,取得比手工操作更佳的性能。在本文中,我说明在VB.NET中利用StringBuilder的情况。应用StringBuilder的优点StringBuilder是一个可修改字符的缓冲器。当你不直接进行字符串操作而应用StringBuilder时,与每次重复生成一个含有修正的新字符串相比,这样可以获得更佳的性能。你不能总是希望性能得到提高,因为StringBuilder有少量的过度操作,会将指定字符串的内容复制到StringBuilder中,然后将由StringBuilder得到的结果值复制回字符串对象中。在进行二个或更多字符串操作时,我建议使用StringBuilder。(如果只有一项改变,将值复制进StringBuilder再拷贝出来并不会改善性能。)StringBuilder方法Append:允许你在缓冲器末端增加一个字符串。注意,在应用StringBuilder对象时不能使用操作符。
创新互联网站建设由有经验的网站设计师、开发人员和项目经理组成的专业建站团队,负责网站视觉设计、用户体验优化、交互设计和前端开发等方面的工作,以确保网站外观精美、网站制作、网站建设易于使用并且具有良好的响应性。
Insert:允许你在StringBuilder对象中增加一个字符串。(就像String.Insert一样。)
Remove:允许你从StringBuilder对象中删除一个字符串。(就像String.Remove一样。)
Replace:允许你替代StringBuilder对象的一部分。(就像String.Replace一样。)
实例在列表A的样本代码中,我建立一个字符串变量strOriginal来保存原始文本的值。我还定义StringBuilder对象BuilderStr,我将利用它对存储在srtOriginal中的文本进行字符串操作。定义BuilderStr时,我将它设定到strOriginal的值中。之后,我操作BuilderStr,给它添加一些文本(“is”,
”very”,”
important”),然后再利用Replace方法去掉”very”这个单词。(注意,在这个例子中,你使用Remove方法而非Replace方法。)最后,我用BuilderStr.ToString的值更新strOriginal,并在信息框中显示所得到的字符串。
vb.net做WINFORM程序如何运行更快?
可以考虑使用Cache(缓存)技术,对于不经常变化的数据可以采用静态Cache,对于可能经常被修改的数据可以采用数据库依赖的通知Cache更新技术。
VB.NET设置屏幕分辨率、颜色位数、刷新率 实例代码
这篇文章介绍了VB.NET设置屏幕分辨率、颜色位数、刷新率
实例代码,有需要的朋友可以参考一下
复制代码
代码如下:
Private
Declare
Function
GetDeviceCaps
Lib
"gdi32"
(ByVal
hdc
As
Long,
ByVal
nIndex
As
Long)
As
Long
Private
Declare
Function
ChangeDisplaySettings
Lib
"user32"
Alias
"ChangeDisplaySettingsA"
(lpDevMode
As
Any,
ByVal
dwflags
As
Long)
As
Long
Private
Const
CCDEVICENAME
As
Long
=
32
Private
Const
CCFORMNAME
As
Long
=
32
Private
Const
DM_BITSPERPEL
As
Long
=
H40000
Private
Const
DM_PELSWIDTH
As
Long
=
H80000
Private
Const
DM_PELSHEIGHT
As
Long
=
H100000
Private
Const
DM_DISPLAYFLAGS
As
Long
=
H200000
Private
Const
DM_DISPLAYFREQUENCY
=
H400000
Private
Const
CDS_FORCE
As
Long
=
H80000000
Private
Const
BITSPIXEL
As
Long
=
12
Private
Const
HORZRES
As
Long
=
8
Private
Const
VERTRES
As
Long
=
10
Private
Const
VREFRESH
=
116
Private
Type
DEVMODE
dmDeviceName
As
String
*
CCDEVICENAME
dmSpecVersion
As
Integer
dmDriverVersion
As
Integer
dmSize
As
Integer
dmDriverExtra
As
Integer
dmFields
As
Long
dmOrientation
As
Integer
dmPaperSize
As
Integer
dmPaperLength
As
Integer
dmPaperWidth
As
Integer
dmScale
As
Integer
dmCopies
As
Integer
dmDefaultSource
As
Integer
dmPrintQuality
As
Integer
dmColor
As
Integer
dmDuplex
As
Integer
dmYResolution
As
Integer
dmTTOption
As
Integer
dmCollate
As
Integer
dmFormName
As
String
*
CCFORMNAME
dmUnusedPadding
As
Integer
dmBitsPerPel
As
Integer
dmPelsWidth
As
Long
dmPelsHeight
As
Long
dmDisplayFlags
As
Long
dmDisplayFrequency
As
Long
End
Type
Private
Sub
cmdChangeDesktopMode_Click()
Dim
DM
As
DEVMODE
With
DM
.dmPelsWidth
=
CInt(txtNewWidth.Text)
.dmPelsHeight
=
CInt(txtNewHeight.Text)
.dmBitsPerPel
=
CInt(txtNewColor.Text)
.dmDisplayFrequency
=
CInt(txtNewFreq.Text)
.dmFields
=
DM_PELSWIDTH
Or
DM_PELSHEIGHT
Or
DM_BITSPERPEL
Or
DM_DISPLAYFREQUENCY
.dmSize
=
LenB(DM)
End
With
If
ChangeDisplaySettings(DM,
CDS_FORCE)
Then
MsgBox
"错误!不支持此模式!"
End
If
End
Sub
Private
Sub
Form_Load()
txtOldWidth.Text
=
GetDeviceCaps(Me.hdc,
HORZRES)
txtOldHeight.Text
=
GetDeviceCaps(Me.hdc,
VERTRES)
txtOldColor.Text
=
GetDeviceCaps(Me.hdc,
BITSPIXEL)
txtOldFreq.Text
=
GetDeviceCaps(Me.hdc,
VREFRESH)
End
Sub
文章标题:vb.net优化代码,VBNET的编程机制
标题链接:http://azwzsj.com/article/hoejgo.html