以下是引用btsfdz在2008-10-9 18:08:00的发言:
Dim d As Date = Date.Today
dim b as integer= SubString(Convert("d",'System.String'),1,4)
dim a as Integer = "1975"
dim v as integer = "b"-"a"
output.show(v)
求教这段代码那里错了?
有3类错误:一个是Convert的使用环境有问题,一个是整数和字符的数据类型混淆,一个是SubString用法问题。
1、从帮助文件上看,Convert只能用于表达式环境,你看Convert("d",'System.String')中单引号,如果在命令窗口和代码中就当作注释了,显然有问题。
2、SubString,同样是“获取从字符串中的指定点开始,具有指定长度的子字符串”功能,但在表达式和代码中的语法是不同的,表达式式中语法是:SUBSTRING(expression, start, length) ,比如SUBSTRING([电话号码], 7, 8),涉及到表字段,代码中的语法是SubString(StartIndex)或SubString(StartIndex, Length),而且是作为字符串的属性引用。比如:
Dim s1 As String ="中华人民共和国"
Dim s2 As
String
Dim s3 As
String
s2 = s1.SubString(2)
s3 = s1.SubString(2,3)
3、用引号括起来的"1975"是字符型String,数字1975才是整数型Integer,两者不相等也无法相加减。"b"-"a"明显是错误的。
可以改成:
Dim d As Date = Date.Today
Dim b As Integer = d.year
dim a as Integer = 1975
dim v as integer = b-a
output.show(v)
如果要用SubString折腾一下,也可以这样:
Dim d As Date = Date.Today
dim b as string=d.ToString().SubString(0,4)
dim bb as Integer
Integer.TryParse(b, bb)
dim a as Integer = 1975
dim v as integer = bb-a
output.show(v)