以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  全局代码编译错误“语句不能出现在方法体外”的求助  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=114862)

--  作者:洮沙
--  发布时间:2018/2/24 11:55:00
--  全局代码编译错误“语句不能出现在方法体外”的求助

各位老师,春节快乐!

打算定义全局代码“Welcome”,准备在每个主窗口引用,但是保存全局代码时出现错误,麻烦老师看看,帮帮忙!

Dim ln As New Lunar(Date.Today)
Dim str As String = ""
Dim d As Date = cdate(ln.LunarDate)  \'.adddays(-11)
If d.Month = 1 Then
    If d.Day <= 10 Then
        str = cldate(d).Substring(5).trim("日")
        str = str.Insert(str.Length-1, "初")
        str = str.Remove(0,1)
        str = str.Insert(0, "正")
    Else
        str = cldate(d).Substring(5).trim("日")
        str = str.Remove(0,1)
        str = str.Insert(0, "正")
    End If
ElseIf d.Month = 2 OrElse d.Month = 3 OrElse d.Month = 4 OrElse d.Month = 5 OrElse d.Month = 6 OrElse d.Month = 7 OrElse d.Month = 8 OrElse d.Month = 9 OrElse d.Month = 10 OrElse d.Month = 11
    If d.Day <= 10 Then
        str = cldate(d).Substring(5).trim("日")
        str = str.Insert(str.Length-1, "初")
    Else
        str = cldate(d).Substring(5).trim("日")
    End If
ElseIf d.Month = 12 OrElse d.Month = 13
    If d.Day <= 10 Then
        str = cldate(d).Substring(5).trim("日")
        str = str.Insert(str.Length-1, "初")
        str = str.Remove(0,2)
        str = str.Insert(0, "腊")
    Else
        str = cldate(d).Substring(5).trim("日")
        str = str.Remove(0,2)
        str = str.Insert(0, "腊")
    End If
End If
Public Welcome As String =  "欢迎您:" & User.Name & " | 今天是:" & Format(Date.Today,"yyyy年MM月dd日 dddd" & " 农历:" & str   )

 

 


图片点击可在新窗口打开查看此主题相关图片如下:1.png
图片点击可在新窗口打开查看

此段代码,单独执行没有问题啊!自己不解。

[此贴子已经被作者于2018/2/24 12:06:49编辑过]

--  作者:aizaishuzui
--  发布时间:2018/2/24 12:11:00
--  
用内部函数呀
--  作者:有点蓝
--  发布时间:2018/2/24 14:34:00
--  
全局代码需要定义函数:http://www.foxtable.com/webhelp/scr/1938.htm

public function abc()
‘这里是1楼的代码,最后加上下面一句’
return Welcome 
end function 

外部调用

dim w as string = abc()
msgbox(w)

--  作者:洮沙
--  发布时间:2018/2/26 8:50:00
--  回复:(有点蓝)全局代码需要定义函数:http://www.f...

谢谢蓝老师!春节快乐!

 

不过按照您所教的,在设置全局代码时,出现以下错误:

 

 


图片点击可在新窗口打开查看此主题相关图片如下:2.png
图片点击可在新窗口打开查看

 

麻烦老师帮忙看看!谢谢!

[此贴子已经被作者于2018/2/26 8:54:42编辑过]

--  作者:有点甜
--  发布时间:2018/2/26 8:59:00
--  

做一个内部函数

 

Dim ln As New Lunar(Date.Today)
Dim str As String = ""
Dim d As Date = cdate(ln.LunarDate)  \'.adddays(-11)
If d.Month = 1 Then
    If d.Day <= 10 Then
        str = cldate(d).Substring(5).trim("日")
        str = str.Insert(str.Length-1, "初")
        str = str.Remove(0,1)
        str = str.Insert(0, "正")
    Else
        str = cldate(d).Substring(5).trim("日")
        str = str.Remove(0,1)
        str = str.Insert(0, "正")
    End If
ElseIf d.Month = 2 OrElse d.Month = 3 OrElse d.Month = 4 OrElse d.Month = 5 OrElse d.Month = 6 OrElse d.Month = 7 OrElse d.Month = 8 OrElse d.Month = 9 OrElse d.Month = 10 OrElse d.Month = 11
    If d.Day <= 10 Then
        str = cldate(d).Substring(5).trim("日")
        str = str.Insert(str.Length-1, "初")
    Else
        str = cldate(d).Substring(5).trim("日")
    End If
ElseIf d.Month = 12 OrElse d.Month = 13
    If d.Day <= 10 Then
        str = cldate(d).Substring(5).trim("日")
        str = str.Insert(str.Length-1, "初")
        str = str.Remove(0,2)
        str = str.Insert(0, "腊")
    Else
        str = cldate(d).Substring(5).trim("日")
        str = str.Remove(0,2)
        str = str.Insert(0, "腊")
    End If
End If
return "欢迎您:" & User.Name & " | 今天是:" & Format(Date.Today,"yyyy年MM月dd日 dddd" & " 农历:" & str   )

 

调用内部函数

 

Dim str As String = Functions.Execute("test")
msgbox(str)


--  作者:洮沙
--  发布时间:2018/2/26 9:14:00
--  回复:(有点甜)做一个内部函数 Dim ln As ...
不错,完美解决,谢谢老师!还是自己对内部函数理解的不够啊!图片点击可在新窗口打开查看