Sea*_*ugh 121 vb.net hidden-features
我已经学习了很多浏览C#的隐藏功能,当我找不到类似于VB.NET的东西时,我感到很惊讶.
那么它的一些隐藏或鲜为人知的特征是什么?
tor*_*ial 128
该Exception When
条款基本上未知.
考虑一下:
Public Sub Login(host as string, user as String, password as string, _
Optional bRetry as Boolean = False)
Try
ssh.Connect(host, user, password)
Catch ex as TimeoutException When Not bRetry
''//Try again, but only once.
Login(host, user, password, True)
Catch ex as TimeoutException
''//Log exception
End Try
End Sub
Run Code Online (Sandbox Code Playgroud)
Kon*_*lph 82
Enum
小号VB的一个真正隐藏的功能是completionlist
XML文档标记,可用于创建Enum
具有扩展功能的类似自己的类型.但是,此功能在C#中不起作用.
我最近的一个代码中的一个例子:
'
''' <completionlist cref="RuleTemplates"/>
Public Class Rule
Private ReadOnly m_Expression As String
Private ReadOnly m_Options As RegexOptions
Public Sub New(ByVal expression As String)
Me.New(expression, RegexOptions.None)
End Sub
Public Sub New(ByVal expression As String, ByVal options As RegexOptions)
m_Expression = expression
m_options = options
End Sub
Public ReadOnly Property Expression() As String
Get
Return m_Expression
End Get
End Property
Public ReadOnly Property Options() As RegexOptions
Get
Return m_Options
End Get
End Property
End Class
Public NotInheritable Class RuleTemplates
Public Shared ReadOnly Whitespace As New Rule("\s+")
Public Shared ReadOnly Identifier As New Rule("\w+")
Public Shared ReadOnly [String] As New Rule("""([^""]|"""")*""")
End Class
Run Code Online (Sandbox Code Playgroud)
现在,在为声明为的变量赋值时Rule
,IDE会提供可能值的IntelliSense列表RuleTemplates
.
由于这是一个依赖于IDE的功能,因此在使用它时很难显示它的外观,但我只是使用屏幕截图:
行动完成清单http://page.mi.fu-berlin.de/krudolph/stuff/completionlist.png
事实上,IntelliSense与使用时的完全相同Enum
.
Par*_*rsa 49
你有没有注意到Like比较运算符?
Dim b As Boolean = "file.txt" Like "*.txt"
更多来自MSDN
Dim testCheck As Boolean
' The following statement returns True (does "F" satisfy "F"?)'
testCheck = "F" Like "F"
' The following statement returns False for Option Compare Binary'
' and True for Option Compare Text (does "F" satisfy "f"?)'
testCheck = "F" Like "f"
' The following statement returns False (does "F" satisfy "FFF"?)'
testCheck = "F" Like "FFF"
' The following statement returns True (does "aBBBa" have an "a" at the'
' beginning, an "a" at the end, and any number of characters in '
' between?)'
testCheck = "aBBBa" Like "a*a"
' The following statement returns True (does "F" occur in the set of'
' characters from "A" through "Z"?)'
testCheck = "F" Like "[A-Z]"
' The following statement returns False (does "F" NOT occur in the '
' set of characters from "A" through "Z"?)'
testCheck = "F" Like "[!A-Z]"
' The following statement returns True (does "a2a" begin and end with'
' an "a" and have any single-digit number in between?)'
testCheck = "a2a" Like "a#a"
' The following statement returns True (does "aM5b" begin with an "a",'
' followed by any character from the set "L" through "P", followed'
' by any single-digit number, and end with any character NOT in'
' the character set "c" through "e"?)'
testCheck = "aM5b" Like "a[L-P]#[!c-e]"
' The following statement returns True (does "BAT123khg" begin with a'
' "B", followed by any single character, followed by a "T", and end'
' with zero or more characters of any type?)'
testCheck = "BAT123khg" Like "B?T*"
' The following statement returns False (does "CAT123khg" begin with'
' a "B", followed by any single character, followed by a "T", and'
' end with zero or more characters of any type?)'
testCheck = "CAT123khg" Like "B?T*"
Run Code Online (Sandbox Code Playgroud)
Kon*_*lph 48
VB知道原始的那种typedef
通过Import
别名:
Imports S = System.String
Dim x As S = "Hello"
Run Code Online (Sandbox Code Playgroud)
当与泛型类型结合使用时,这更有用:
Imports StringPair = System.Collections.Generic.KeyValuePair(Of String, String)
Run Code Online (Sandbox Code Playgroud)
Nes*_*cio 45
哦! 并且不要忘记XML Literals.
Dim contact2 = _
<contact>
<name>Patrick Hines</name>
<%= From p In phoneNumbers2 _
Select <phone type=<%= p.Type %>><%= p.Number %></phone> _
%>
</contact>
Run Code Online (Sandbox Code Playgroud)
Nes*_*cio 39
对象初始化也在那里!
Dim x as New MyClass With {.Prop1 = foo, .Prop2 = bar}
Run Code Online (Sandbox Code Playgroud)
Kon*_*lph 38
DirectCast
DirectCast
是一个奇迹.从表面上看,它的工作方式类似于CType
操作符,因为它将对象从一种类型转换为另一种类型.但是,它的工作规则要严格得多.CType
因此,实际行为通常是不透明的,并且根本不明显执行哪种转换.
DirectCast
仅支持两种不同的操作:
任何其他强制转换都不起作用(例如,尝试将a取消映射Integer
到a Double
)并将导致编译时/运行时错误(取决于情况以及静态类型检查可以检测到的内容).因此DirectCast
,我尽可能地使用,因为这最好地捕获了我的意图:根据情况,我要么取消已知类型的值或执行向上转换.故事结局.
使用CType
,而另一方面,离开代码想知道什么是程序员真正意图,因为它解决了各种不同的操作,包括调用用户定义的代码的读者.
为什么这是一个隐藏的功能?VB团队已经发布了一个指南1,它不鼓励使用DirectCast
(即使它实际上更快!),以使代码更加统一.我认为这是一个不好的指导方针,应该予以逆转:只要有可能,DirectCast
对更一般的CType
运营商有利.它使代码更清晰.CType
另一方面,只有在确实需要时才应该调用,即应该调用缩小CType
运算符(参见运算符重载).
1)我无法找到指南的链接,但我发现了Paul Vick对它的看法(VB团队的首席开发人员):
在现实世界中,你几乎不会注意到这种差异,所以你也可以选择CType,CInt等更灵活的转换运算符.
(Zack编辑:在这里了解更多:我应该如何使用VB.NET?)
Kon*_*lph 37
If
条件和合并运算符我不知道你怎么称它为隐藏,但Iif([表达式],[值如果为真],[值如果为假])作为对象函数可以计数.
它不像被弃用那样隐藏!VB 9的If
运算符要好得多,并且与C#的条件和合并运算符完全一样(取决于你想要的):
Dim x = If(a = b, c, d)
Dim hello As String = Nothing
Dim y = If(hello, "World")
Run Code Online (Sandbox Code Playgroud)
编辑以显示另一个例子:
这可以使用If()
,但会引起异常IIf()
Dim x = If(b<>0,a/b,0)
Run Code Online (Sandbox Code Playgroud)
tor*_*ial 32
这是一个很好的.VB.Net中的Select Case语句非常强大.
当然有标准
Select Case Role
Case "Admin"
''//Do X
Case "Tester"
''//Do Y
Case "Developer"
''//Do Z
Case Else
''//Exception case
End Select
Run Code Online (Sandbox Code Playgroud)
但还有更多......
你可以做范围:
Select Case Amount
Case Is < 0
''//What!!
Case 0 To 15
Shipping = 2.0
Case 16 To 59
Shipping = 5.87
Case Is > 59
Shipping = 12.50
Case Else
Shipping = 9.99
End Select
Run Code Online (Sandbox Code Playgroud)
还有更多......
您可以(虽然可能不是一个好主意)对多个变量进行布尔检查:
Select Case True
Case a = b
''//Do X
Case a = c
''//Do Y
Case b = c
''//Do Z
Case Else
''//Exception case
End Select
Run Code Online (Sandbox Code Playgroud)
小智 31
我一直使用的主要节省时间是With关键字:
With ReallyLongClassName
.Property1 = Value1
.Property2 = Value2
...
End With
Run Code Online (Sandbox Code Playgroud)
我只是不喜欢打字超过我必须!
cjk*_*cjk 31
最好的CSV解析器:
Microsoft.VisualBasic.FileIO.TextFieldParser
Run Code Online (Sandbox Code Playgroud)
通过添加对Microsoft.VisualBasic的引用,可以在任何其他.Net语言中使用它,例如C#
Joe*_*orn 25
方法中的静态成员.
例如:
Function CleanString(byval input As String) As String
Static pattern As New RegEx("...")
return pattern.Replace(input, "")
End Function
Run Code Online (Sandbox Code Playgroud)
在上面的函数中,无论调用函数多少次,模式正则表达式都只会被创建一次.
另一个用途是保持"随机"的实例:
Function GetNextRandom() As Integer
Static r As New Random(getSeed())
Return r.Next()
End Function
Run Code Online (Sandbox Code Playgroud)
此外,这与简单地将其声明为该类的共享成员不同; 以这种方式声明的项目也保证是线程安全的.在这种情况下无关紧要,因为表达式永远不会改变,但还有其他可能的改变.
Shi*_*mmy 25
在vb中,这些运算符之间存在差异:
/
是Double
\
被Integer
忽略的余
Sub Main()
Dim x = 9 / 5
Dim y = 9 \ 5
Console.WriteLine("item x of '{0}' equals to {1}", x.GetType.FullName, x)
Console.WriteLine("item y of '{0}' equals to {1}", y.GetType.FullName, y)
'Results:
'item x of 'System.Double' equals to 1.8
'item y of 'System.Int32' equals to 1
End Sub
Run Code Online (Sandbox Code Playgroud)
Kon*_*lph 23
虽然很少有用,但事件处理可以大量定制:
Public Class ApplePie
Private ReadOnly m_BakedEvent As New List(Of EventHandler)()
Custom Event Baked As EventHandler
AddHandler(ByVal value As EventHandler)
Console.WriteLine("Adding a new subscriber: {0}", value.Method)
m_BakedEvent.Add(value)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
Console.WriteLine("Removing subscriber: {0}", value.Method)
m_BakedEvent.Remove(value)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("{0} is raising an event.", sender)
For Each ev In m_BakedEvent
ev.Invoke(sender, e)
Next
End RaiseEvent
End Event
Public Sub Bake()
''// 1. Add ingredients
''// 2. Stir
''// 3. Put into oven (heated, not pre-heated!)
''// 4. Bake
RaiseEvent Baked(Me, EventArgs.Empty)
''// 5. Digest
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
然后可以按以下方式测试:
Module Module1
Public Sub Foo(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine("Hmm, freshly baked apple pie.")
End Sub
Sub Main()
Dim pie As New ApplePie()
AddHandler pie.Baked, AddressOf Foo
pie.Bake()
RemoveHandler pie.Baked, AddressOf Foo
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
spl*_*tne 23
我非常喜欢Visual Basic 2005中引入的"我的"命名空间.我是几组信息和功能的快捷方式.它提供对以下类型信息的快速直观访问:
tor*_*ial 21
我刚发现一篇文章谈到"!" 运算符,也称为"字典查找运算符".以下是http://panopticoncentral.net/articles/902.aspx文章的摘录
技术名称为!operator是"字典查找运算符".字典是由键而不是数字索引的任何集合类型,就像英语字典中的条目被您想要定义的单词索引的方式一样.字典类型最常见的示例是System.Collections.Hashtable,它允许您将(键,值)对添加到哈希表中,然后使用键检索值.例如,以下代码将三个条目添加到哈希表中,并使用键"Pork"查找其中一个条目.
Dim Table As Hashtable = New Hashtable
Table("Orange") = "A fruit"
Table("Broccoli") = "A vegetable"
Table("Pork") = "A meat"
Console.WriteLine(Table("Pork"))
Run Code Online (Sandbox Code Playgroud)
的!运算符可用于从使用字符串索引其值的任何字典类型中查找值.之后的标识符!用作查找操作中的键.所以上面的代码可以写成:
Dim Table As Hashtable = New Hashtable
Table!Orange = "A fruit"
Table!Broccoli = "A vegetable"
Table!Pork = "A meat"
Console.WriteLine(Table!Pork)
Run Code Online (Sandbox Code Playgroud)
第二个例子完全等同于第一个例子,但看起来好多了,至少在我看来.我发现有很多地方在那里!可以使用,尤其是涉及XML和Web时,其中只有大量的字符串索引的集合.一个不幸的限制是事后的事情!仍然必须是一个有效的标识符,所以如果你想用作一个键的字符串中有一些无效的标识符字符,你就不能使用!运营商.(例如,你不能说"Table!AB $ CD = 5",因为$在标识符中不合法.)在VB6及之前,你可以使用括号来转义无效的标识符(即"表![AB $] CD]"),但是当我们开始使用括号来转义关键字时,我们失去了这样做的能力.在多数情况下,
为了获得真正的技术,如果x具有将String或Object作为参数的默认属性,则x!y有效.在这种情况下,x!y变为x.DefaultProperty("y").一个有趣的旁注是语言的词汇语法中有一个特殊的规则,使这一切都有效.的!character也用作语言中的类型字符,类型字符在运算符之前被占用.因此,如果没有特殊规则,x!y将被扫描为"x!y"而不是"x!y".幸运的是,由于在一行中两个标识符有效的语言中没有位置,我们只介绍了如果下一个字符在!之后的规则!是标识符的开头,我们考虑一下!成为一个运营商而不是一个类型角色.
tor*_*ial 19
这是内置的,并且比C#具有明显的优势.实现接口方法的能力,而不必使用相同的名称.
如:
Public Sub GetISCSIAdmInfo(ByRef xDoc As System.Xml.XmlDocument) Implements IUnix.GetISCSIInfo
End Sub
Run Code Online (Sandbox Code Playgroud)
Chr*_*aas 17
强迫ByVal
在VB中,如果将参数包装在一组额外的括号中,则可以覆盖方法的ByRef声明并将其转换为ByVal.例如,以下代码生成4,5,5而不是4,5,6
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim R = 4
Trace.WriteLine(R)
Test(R)
Trace.WriteLine(R)
Test((R))
Trace.WriteLine(R)
End Sub
Private Sub Test(ByRef i As Integer)
i += 1
End Sub
Run Code Online (Sandbox Code Playgroud)
小智 16
按名称传递参数,然后重新排序
Sub MyFunc(Optional msg as String= "", Optional displayOrder As integer = 0)
'Do stuff
End function
Run Code Online (Sandbox Code Playgroud)
用法:
Module Module1
Sub Main()
MyFunc() 'No params specified
End Sub
End Module
Run Code Online (Sandbox Code Playgroud)
也可以按任何顺序使用":="参数规范调用:
MyFunc(displayOrder:=10, msg:="mystring")
Run Code Online (Sandbox Code Playgroud)
tor*_*ial 15
从VB 8开始,Using语句是新的,C#从一开始就拥有它.它会自动为您调用dispose.
例如
Using lockThis as New MyLocker(objToLock)
End Using
Run Code Online (Sandbox Code Playgroud)
tor*_*ial 14
导入别名也很大程度上未知:
Import winf = System.Windows.Forms
''Later
Dim x as winf.Form
Run Code Online (Sandbox Code Playgroud)
tor*_*ial 14
如果需要变量名称以匹配关键字的名称,请用括号括起来.不是.虽然是最好的做法 - 但它可以明智地使用.
例如
Class CodeException
Public [Error] as String
''...
End Class
''later
Dim e as new CodeException
e.Error = "Invalid Syntax"
Run Code Online (Sandbox Code Playgroud)
例如评论中的例子(@Pondidum):
Class Timer
Public Sub Start()
''...
End Sub
Public Sub [Stop]()
''...
End Sub
Run Code Online (Sandbox Code Playgroud)
Tec*_*ble 14
请考虑以下事件声明
Public Event SomethingHappened As EventHandler
Run Code Online (Sandbox Code Playgroud)
在C#中,您可以使用以下语法检查事件订阅者:
if(SomethingHappened != null)
{
...
}
Run Code Online (Sandbox Code Playgroud)
但是,VB.NET编译器不支持此功能.它实际上创建了一个在IntelliSense中不可见的隐藏私有成员字段:
If Not SomethingHappenedEvent Is Nothing OrElse SomethingHappenedEvent.GetInvocationList.Length = 0 Then
...
End If
Run Code Online (Sandbox Code Playgroud)
更多信息:
http://jelle.druyts.net/2003/05/09/BehindTheScenesOfEventsInVBNET.aspx http://blogs.msdn.com/vbteam/archive/2009/09/25/testing-events-for-nothing-null-doug -rothaus.aspx
Rya*_*ndy 13
关于XML Literals有几个答案,但不是关于这个特定情况:
您可以使用XML Literals来包含原本需要转义的字符串文字.例如,包含双引号的字符串文字.
而不是这个:
Dim myString = _
"This string contains ""quotes"" and they're ugly."
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
Dim myString = _
<string>This string contains "quotes" and they're nice.</string>.Value
Run Code Online (Sandbox Code Playgroud)
如果您正在测试CSV解析的文字,这将特别有用:
Dim csvTestYuck = _
"""Smith"", ""Bob"", ""123 Anywhere St"", ""Los Angeles"", ""CA"""
Dim csvTestMuchBetter = _
<string>"Smith", "Bob", "123 Anywhere St", "Los Angeles", "CA"</string>.Value
Run Code Online (Sandbox Code Playgroud)
(当然,您不必使用<string>
标签;您可以使用任何您喜欢的标签.)
Par*_*rsa 12
您可以在一行中拥有2行代码.因此:
Dim x As New Something : x.CallAMethod
Run Code Online (Sandbox Code Playgroud)
dan*_*ash 12
可以使用#围绕日期来初始化DateTime
Dim independanceDay As DateTime = #7/4/1776#
Run Code Online (Sandbox Code Playgroud)
您还可以使用类型推断和此语法
Dim independanceDay = #7/4/1776#
Run Code Online (Sandbox Code Playgroud)
这比使用构造函数好很多
Dim independanceDay as DateTime = New DateTime(1776, 7, 4)
Run Code Online (Sandbox Code Playgroud)
dr.*_*vil 11
可选参数
可选项比创建新的重载更容易,例如:
Function CloseTheSystem(Optional ByVal msg AS String = "Shutting down the system...")
Console.Writeline(msg)
''//do stuff
End Function
Run Code Online (Sandbox Code Playgroud)
VB.Net中的标题案例可以通过旧的VB6 fxn来实现:
StrConv(stringToTitleCase, VbStrConv.ProperCase,0) ''0 is localeID
Run Code Online (Sandbox Code Playgroud)
带参数的属性
我一直在做一些C#编程,并发现了一个缺少VB.Net的功能,但这里没有提到.
可以在以下位置看到如何执行此操作的示例(以及c#限制):使用C#中的典型get set属性...带参数
我摘录了该答案的代码:
Private Shared m_Dictionary As IDictionary(Of String, Object) = _
New Dictionary(Of String, Object)
Public Shared Property DictionaryElement(ByVal Key As String) As Object
Get
If m_Dictionary.ContainsKey(Key) Then
Return m_Dictionary(Key)
Else
Return [String].Empty
End If
End Get
Set(ByVal value As Object)
If m_Dictionary.ContainsKey(Key) Then
m_Dictionary(Key) = value
Else
m_Dictionary.Add(Key, value)
End If
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
使用语句堆叠/组合多个:
Dim sql As String = "StoredProcedureName"
Using cn As SqlConnection = getOpenConnection(), _
cmd As New SqlCommand(sql, cn), _
rdr As SqlDataReader = cmd.ExecuteReader()
While rdr.Read()
''// Do Something
End While
End Using
Run Code Online (Sandbox Code Playgroud)
公平地说,你也可以用C#来做.但很多人都不知道这两种语言.
我发现其中一个非常有用且有助于解决许多错误的功能是将参数显式传递给函数,尤其是在使用可选项时.
这是一个例子:
Public Function DoSomething(byval x as integer, optional y as boolean=True, optional z as boolean=False)
' ......
End Function
Run Code Online (Sandbox Code Playgroud)
然后你可以这样称呼它:
DoSomething(x:=1, y:=false)
DoSomething(x:=2, z:=true)
or
DoSomething(x:=3,y:=false,z:=true)
Run Code Online (Sandbox Code Playgroud)
这样更干净,没有bug,然后调用这个函数
DoSomething(1,true)
Run Code Online (Sandbox Code Playgroud)
如果你从来不知道以下内容,你真的不相信它是真的,这真的是C#缺乏时间的东西:
(它被称为XML文字)
Imports <xmlns:xs="System">
Module Module1
Sub Main()
Dim xml =
<root>
<customer id="345">
<name>John</name>
<age>17</age>
</customer>
<customer id="365">
<name>Doe</name>
<age>99</age>
</customer>
</root>
Dim id = 1
Dim name = "Beth"
DoIt(
<param>
<customer>
<id><%= id %></id>
<name><%= name %></name>
</customer>
</param>
)
Dim names = xml...<name>
For Each n In names
Console.WriteLine(n.Value)
Next
For Each customer In xml.<customer>
Console.WriteLine("{0}: {1}", customer.@id, customer.<age>.Value)
Next
Console.Read()
End Sub
Private Sub CreateClass()
Dim CustomerSchema =
XDocument.Load(CurDir() & "\customer.xsd")
Dim fields =
From field In CustomerSchema...<xs:element>
Where field.@type IsNot Nothing
Select
Name = field.@name,
Type = field.@type
Dim customer =
<customer> Public Class Customer
<%= From field In fields Select <f>
Private m_<%= field.Name %> As <%= GetVBPropType(field.Type) %></f>.Value %>
<%= From field In fields Select <p>
Public Property <%= field.Name %> As <%= GetVBPropType(field.Type) %>
Get
Return m_<%= field.Name %>
End Get
Set(ByVal value As <%= GetVBPropType(field.Type) %>)
m_<%= field.Name %> = value
End Set
End Property</p>.Value %>
End Class</customer>
My.Computer.FileSystem.WriteAllText("Customer.vb",
customer.Value,
False,
System.Text.Encoding.ASCII)
End Sub
Private Function GetVBPropType(ByVal xmlType As String) As String
Select Case xmlType
Case "xs:string"
Return "String"
Case "xs:int"
Return "Integer"
Case "xs:decimal"
Return "Decimal"
Case "xs:boolean"
Return "Boolean"
Case "xs:dateTime", "xs:date"
Return "Date"
Case Else
Return "'TODO: Define Type"
End Select
End Function
Private Sub DoIt(ByVal param As XElement)
Dim customers =
From customer In param...<customer>
Select New Customer With
{
.ID = customer.<id>.Value,
.FirstName = customer.<name>.Value
}
For Each c In customers
Console.WriteLine(c.ToString())
Next
End Sub
Private Class Customer
Public ID As Integer
Public FirstName As String
Public Overrides Function ToString() As String
Return <string>
ID : <%= Me.ID %>
Name : <%= Me.FirstName %>
</string>.Value
End Function
End Class
End Module
'Results:
ID : 1
Name : Beth
John
Doe
345: 17
365: 99
Run Code Online (Sandbox Code Playgroud)
看看Beth Massi的XML Literals Tips/Tricks.
注意when
在行中的使用Catch ex As IO.FileLoadException When attempt < 3
Do
Dim attempt As Integer
Try
''// something that might cause an error.
Catch ex As IO.FileLoadException When attempt < 3
If MsgBox("do again?", MsgBoxStyle.YesNo) = MsgBoxResult.No Then
Exit Do
End If
Catch ex As Exception
''// if any other error type occurs or the attempts are too many
MsgBox(ex.Message)
Exit Do
End Try
''// increment the attempt counter.
attempt += 1
Loop
Run Code Online (Sandbox Code Playgroud)
最近在VbRad中查看
这是一个我没见过的有趣的; 我知道它在VS 2008中起作用,至少:
如果你不小心用分号结束你的VB行,因为你做了太多的C#,分号就会被自动删除.实际上不可能(至少在VS 2008中)不小心用分号结束VB行.试试吧!
(它并不完美;如果您在最终的班级名称中间输入分号,则不会自动填写班级名称.)
与break
VB中的C语言不同,您可以Exit
或Continue
您想要的块:
For i As Integer = 0 To 100
While True
Exit While
Select Case i
Case 1
Exit Select
Case 2
Exit For
Case 3
Exit While
Case Else
Exit Sub
End Select
Continue For
End While
Next
Run Code Online (Sandbox Code Playgroud)
在VB8和前面的版本中,如果没有为引入的变量指定任何类型,则自动检测对象类型.在VB9(2008)中,如果Option Infer设置为On(默认情况下),Dim
它将像C#的var
关键字一样
小智 5
选择Case代替多个If/ElseIf/Else语句.
在此示例中假设简单的几何对象:
Function GetToString(obj as SimpleGeomertyClass) as String
Select Case True
Case TypeOf obj is PointClass
Return String.Format("Point: Position = {0}", _
DirectCast(obj,Point).ToString)
Case TypeOf obj is LineClass
Dim Line = DirectCast(obj,LineClass)
Return String.Format("Line: StartPosition = {0}, EndPosition = {1}", _
Line.StartPoint.ToString,Line.EndPoint.ToString)
Case TypeOf obj is CircleClass
Dim Line = DirectCast(obj,CircleClass)
Return String.Format("Circle: CenterPosition = {0}, Radius = {1}", _
Circle.CenterPoint.ToString,Circle.Radius)
Case Else
Return String.Format("Unhandled Type {0}",TypeName(obj))
End Select
End Function
Run Code Online (Sandbox Code Playgroud)
IIf(False, MsgBox("msg1"), MsgBox("msg2"))
Run Code Online (Sandbox Code Playgroud)
结果是什么?两个消息框!!!! 这种情况发生,因为IIf函数在到达函数时会计算两个参数.
VB有一个新的If运算符(就像C#?:运算符):
If(False, MsgBox("msg1"), MsgBox("msg2"))
Run Code Online (Sandbox Code Playgroud)
将仅显示第二个msgbox.
一般情况下,我建议更换你的vb代码中的所有IIF,除非你想要它们两个项目:
Dim value = IIf(somthing, LoadAndGetValue1(), LoadAndGetValue2())
Run Code Online (Sandbox Code Playgroud)
你可以确定两个值都已加载.
如果用[和]括起名称,可以对属性和变量名使用保留关键字
Public Class Item
Private Value As Integer
Public Sub New(ByVal value As Integer)
Me.Value = value
End Sub
Public ReadOnly Property [String]() As String
Get
Return Value
End Get
End Property
Public ReadOnly Property [Integer]() As Integer
Get
Return Value
End Get
End Property
Public ReadOnly Property [Boolean]() As Boolean
Get
Return Value
End Get
End Property
End Class
'Real examples:
Public Class PropertyException : Inherits Exception
Public Sub New(ByVal [property] As String)
Me.Property = [property]
End Sub
Private m_Property As String
Public Property [Property]() As String
Get
Return m_Property
End Get
Set(ByVal value As String)
m_Property = value
End Set
End Property
End Class
Public Enum LoginLevel
[Public] = 0
Account = 1
Admin = 2
[Default] = Account
End Enum
Run Code Online (Sandbox Code Playgroud)
在vb.net中声明数组时,始终使用"0到xx"语法.
Dim b(0 to 9) as byte 'Declares an array of 10 bytes
Run Code Online (Sandbox Code Playgroud)
它清楚地表明了数组的跨度.将它与等效物进行比较
Dim b(9) as byte 'Declares another array of 10 bytes
Run Code Online (Sandbox Code Playgroud)
即使你知道第二个例子由10个元素组成,它也感觉不太明显.而且我不记得我从程序员那里看到代码的次数,而不是写的
Dim b(10) as byte 'Declares another array of 10 bytes
Run Code Online (Sandbox Code Playgroud)
这当然是完全错误的.因为b(10)创建一个11字节的数组.并且它很容易导致错误,因为它看起来对任何不知道要寻找什么的人都是正确的.
"0到xx"语法也适用于以下内容
Dim b As Byte() = New Byte(0 To 9) {} 'Another way to create a 10 byte array
ReDim b(0 to 9) 'Assigns a new 10 byte array to b
Run Code Online (Sandbox Code Playgroud)
通过使用完整语法,您还将向将来阅读您的代码的任何人展示您知道自己在做什么.
归档时间: |
|
查看次数: |
27099 次 |
最近记录: |