当if语句的表达式中有括号时,它是什么意思? [英] What does it mean when there are brackets in the expression of an if statement?
问题描述
您好,我正在尝试将C#转换为Visual Basic,这是我的进步:
Imports Collections = System.Collections.Generic
Imports Text = System.Text
公开 NotInheritable 类解析器
私有索引为 整数
私有标记 As Collections.IList( Of 对象)
私有结果作为 Stmt
公共 Sub Parser( ByVal 令牌作为 Collections.IList( 对象))
我 .tokens = tokens
Me .index = 0
我 .result = 我 .ParserStmt()
如果( Me .index<> 我 .tokens.Count)然后
投掷 新 System.Exception( 预期的EOF )
结束 如果
结束 Sub
公共 函数 ReturnResult()作为 Stmt
返回结果
结束 功能
私人 功能 ParserStmt()作为 Stmt
Dim 结果 As Stmt
如果( Me .index = 我 .tokens.Count)然后
投掷 新 System.Exception( 预期声明,获得EOF)
结束 如果
结束 功能
结束 Class
您知道,Stmt是另一个只包含空格的文件中的公共部分类。
现在我需要将其转换为Visual Basic:
if ( this .tokens [ this .index] .Equals( Print))
{
this .index ++;
}
此if语句中的括号代表什么?
该代码中有四组不同的括号,它们都有不同的作业。
有大括号:
{
this .index ++;
}
外括号:
if ( this .tokens [ this .index] .Equals( 打印))
^ ^方括号:
if ( this .tokens [ this .index] .Equals( Print))
^ ^内括号:
<前lang =C#> 如果(< span class =code-keyword> this .tokens [ this .index] .Equals( 打印))
^ ^
大括号括起来将语句转换为语句块,如果if
条件的计算结果为true,则所有语句都将被执行,并且它们都不会计算为false。这很像如果......结束如果
构造:
如果 a = b 那么
' 做某事
结束 如果如果条件为True,将执行
End If
的代码。
外括号为C#语言规范的一部分,必须出现在if
之后,否则代码将无法编译。
广场括号表示标记
是一个数组,代码应该在方括号内的值给出的索引处获取数组的单个元素。与VB不同,C#明智地区分了数组访问和方法调用之间的代码:VB对两者使用(和)。
内括号表示Equals是一个方法,它应该传递字符串参数Print
Hello, I am trying to convert C# to Visual Basic and this is my progress:
Imports Collections = System.Collections.Generic
Imports Text = System.Text
Public NotInheritable Class Parser
Private index As Integer
Private tokens As Collections.IList(Of Object)
Private result As Stmt
Public Sub Parser(ByVal tokens As Collections.IList(Of Object))
Me.tokens = tokens
Me.index = 0
Me.result = Me.ParserStmt()
If (Me.index <> Me.tokens.Count) Then
Throw New System.Exception("Expected EOF")
End If
End Sub
Public Function ReturnResult() As Stmt
Return result
End Function
Private Function ParserStmt() As Stmt
Dim result As Stmt
If (Me.index = Me.tokens.Count) Then
Throw New System.Exception("Expected Statement, Got EOF")
End If
End Function
End Class
Just so you know, Stmt is a Public Partial Class in another file that only contains whitespace.
Now I need to convert this to Visual Basic:
if (this.tokens[this.index].Equals("Print"))
{
this.index++;
}
What do the brackets in this if statement represent?
There are four different sets of brackets in that code, and they all do different "jobs".
There are the curly brackets:
{ this.index++; }
The outer brackets:
if (this.tokens[this.index].Equals("Print")) ^ ^The square brackets:
if (this.tokens[this.index].Equals("Print")) ^ ^And the inner brackets:
if (this.tokens[this.index].Equals("Print")) ^ ^
The curly brackets enclose a set of statements into a statement block, all of which will be executed if theif
condition evaluates to true, and none of them is it evaluates to false. It's a lot like theIf...End If
construct:
If a=b Then ' Do something End IfThe code up to the
End If
will be executed if the condition is True.
The outer brackets are part of the C# language specification and must appear after theif
or the code will not compile.
The square brackets indicate thetokens
is an array, and that the code should fetch the single element of the array at the index given by the value inside the square brackets. Unlike VB, C# sensibly makes a distinction in code between an array access and a method call: VB uses "(" and ")" for both.
The inner brackets indicate that Equals is a method, and it should be passed the string parameter "Print"
这篇关于当if语句的表达式中有括号时,它是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!