我可以从 LotusScript 函数返回列表吗?

发布于 2024-07-14 05:08:52 字数 306 浏览 10 评论 0原文

我想从 LotusScript 中的函数返回一个列表。

例如。

Function myfunc() List As Variant
    Dim mylist List As Variant
    mylist("one") = 1
    mylist("two") = "2"
    myfunc = mylist
End Function

Dim mylist List As Variant
mylist = myfunc()

这可能吗?

如果是这样,正确的语法是什么?

I want to return a List from a Function in LotusScript.

eg.

Function myfunc() List As Variant
    Dim mylist List As Variant
    mylist("one") = 1
    mylist("two") = "2"
    myfunc = mylist
End Function

Dim mylist List As Variant
mylist = myfunc()

Is this possible?

If so, what is the correct syntax?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝戈者 2024-07-21 05:08:52

看来您无法从函数返回列表。

您可以轻松地将其包装在一个类中并返回该类。

例如。

Class WrappedList
    Public list List As Variant
End Class

Function myfunc() As WrappedList
    Dim mylist As New WrappedList
    mylist.list("one") = 1
    mylist.list("two") = "2"
    Set myfunc = mylist
End Function

答案在这里找到: LotusScript 的列表错误再次袭来

It seems you can't return a List from a Function.

You can easily wrap it in a class though and return the class.

eg.

Class WrappedList
    Public list List As Variant
End Class

Function myfunc() As WrappedList
    Dim mylist As New WrappedList
    mylist.list("one") = 1
    mylist.list("two") = "2"
    Set myfunc = mylist
End Function

Answer was found here: LotusScript's List bug strikes again

溺ぐ爱和你が 2024-07-21 05:08:52

这对我来说效果很好。 我已将一个值设置为字符串,另一个值设置为整数,以便您可以看到这些变体的行为。

Sub Initialize
    Dim mylist List As Variant
    Call myfunc(mylist)
    Msgbox "un  = " + mylist("one")
    Msgbox "deux = " + cstr(mylist("two"))
End Sub

Sub myfunc(mylist List As Variant)
    mylist("one") = "1"
    mylist("two") = 2
End Sub

This works fine for me. I've set one value to string and the other to integer so you can see that the variants behave themselves.

Sub Initialize
    Dim mylist List As Variant
    Call myfunc(mylist)
    Msgbox "un  = " + mylist("one")
    Msgbox "deux = " + cstr(mylist("two"))
End Sub

Sub myfunc(mylist List As Variant)
    mylist("one") = "1"
    mylist("two") = 2
End Sub
人生戏 2024-07-21 05:08:52

简而言之,您必须有一个返回变体的函数。 我可以看到您喜欢以面向对象的方式完成它,但如果您只想在程序上“完成它”是最简单的。

尽管有多种方法可以做到这一点,但这是我的首选方法。 请注意,您可以创建任何原始数据类型的列表(即字符串、变体、整数、长整型等)。

Function myfunc as variant
    dim mylist list as variant
    mylist("somename") = "the value you want to store"
    mylist("someothername") = "another value"
    myfunc = mylist

End Function

要使用 myfunc ..

sub initialise
    dim anotherlist list as variant
    anotherlist = myfunc
end sub

如果需要,您可以通过简单地定义 myfunc 来向 myfunc 添加参数。您可以

function myfunc(val1 as variant, val2 as variant) as variant

使用与这样的参数相同的方式调用它。

anotherlist = myfunc("a value", "another value")

请注意,“variant”是您的通用数据类型。 重要的是 myfunc 作为变体是从函数返回列表和变体的唯一方法。

Simply put you gotta have a function that returns a variant. I can see that you like to do it in an object oriented fashion, but if you just want to "get it done" procedurally is the easiest.

Although there are a couple of ways to do this, this is my preferred way. Note that you can create a list of any primitive data type, (ie string, variant, integer, long etc).

Function myfunc as variant
    dim mylist list as variant
    mylist("somename") = "the value you want to store"
    mylist("someothername") = "another value"
    myfunc = mylist

End Function

To use myfunc ..

sub initialise
    dim anotherlist list as variant
    anotherlist = myfunc
end sub

You can add parameters to myfunc if you need to by simply defining myfunc this way

function myfunc(val1 as variant, val2 as variant) as variant

You call it the same ways with parameters like this

anotherlist = myfunc("a value", "another value")

Note that "variant" is your universal datatype. What's important is that myfunc as a variant is the only way you can return lists and variants from a function.

铁轨上的流浪者 2024-07-21 05:08:52

您可以获得一个返回列表的函数,只需去掉函数中的“List”位,这样

Function myfunc() List As Variant
   ...
End Function

而不是...do:

Function myfunc() As Variant

您就可以像以前一样调用您的函数,

Dim mylist List As Variant
mylist = myfunc()

。 列表很棒,我一直使用它们,但我发现列表有一个限制......

如果有一个类似的函数:

Public Function returnMyList() as Variant

   Dim myList List as Variant
   Dim s_test as String
   Dim i as Integer
   Dim doc as NotesDocuemnt
   Dim anotherList List as String

   '// ...set your variables however you like

   myList( "Some Text" ) = s_text
   myList( "Some Integer" ) = i
   myList( "Some Doc" ) = doc

   '// If you returned the list here, you're fine, but if you add
   '// another list...
   anotherList( "one" ) = ""
   anotherList( "two" ) = ""
   myList( "Another List" ) = anotherList

   '//  This will cause an error
   returnMyList = myList

   '// I bodge around this by writting directly to a List 
   '// that is set as global variable.

End Function

You can get a function toi return a list, just get rid of "List" bit in your function, so instead of

Function myfunc() List As Variant
   ...
End Function

...do:

Function myfunc() As Variant

then you can call your function as you already do.

Dim mylist List As Variant
mylist = myfunc()

Lists are great and I use them all the time, but I have found one limitation with lists...

if if have a function like:

Public Function returnMyList() as Variant

   Dim myList List as Variant
   Dim s_test as String
   Dim i as Integer
   Dim doc as NotesDocuemnt
   Dim anotherList List as String

   '// ...set your variables however you like

   myList( "Some Text" ) = s_text
   myList( "Some Integer" ) = i
   myList( "Some Doc" ) = doc

   '// If you returned the list here, you're fine, but if you add
   '// another list...
   anotherList( "one" ) = ""
   anotherList( "two" ) = ""
   myList( "Another List" ) = anotherList

   '//  This will cause an error
   returnMyList = myList

   '// I bodge around this by writting directly to a List 
   '// that is set as global variable.

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文