我可以在 ColdFusion 中通过引用传递一个简单的值吗?

发布于 2024-07-27 18:43:15 字数 880 浏览 8 评论 0原文

默认情况下,ColdFusion 按值将简单类型(如数字、字符串和 GUID)传递给函数。 我想通过引用传递一个简单的类型。

我目前正在结构中包装一个简单的值(它们通过引用传递)。 这解决了我的问题,但它非常难看:

<!--- TheFunctionName---->
<cffunction name="TheFunctionName">
     <cfargument name="OutVariable" type="struct">
     <cfset OutVariable.ID = 5>
</cffunction>

<cfset OutVariable=StructNew()>
<cfset TheFunctionName(OutVariable)>

<!--- I want this to output 5--->
<cfoutput>#OutVariable.ID#</cfoutput>

我宁愿这样:

<!--- TheFunctionName---->
<cffunction name="TheFunctionName">
     <cfargument name="OutVariable" passbyref="true">
     <cfset OutVariable = 5>
</cffunction>

<cfset TheFunctionName(OutVariable)>

<!--- I want this to output 5--->
<cfoutput>#OutVariable#</cfoutput>

By default, ColdFusion passes simple types (like numeric, string, and GUID) by value to functions. I'd like to pass a simple type by reference.

I'm currently wrapping a simple value in a struct (they get passed by reference). This solves my problem but it is very ugly:

<!--- TheFunctionName---->
<cffunction name="TheFunctionName">
     <cfargument name="OutVariable" type="struct">
     <cfset OutVariable.ID = 5>
</cffunction>

<cfset OutVariable=StructNew()>
<cfset TheFunctionName(OutVariable)>

<!--- I want this to output 5--->
<cfoutput>#OutVariable.ID#</cfoutput>

I'd rather something like this:

<!--- TheFunctionName---->
<cffunction name="TheFunctionName">
     <cfargument name="OutVariable" passbyref="true">
     <cfset OutVariable = 5>
</cffunction>

<cfset TheFunctionName(OutVariable)>

<!--- I want this to output 5--->
<cfoutput>#OutVariable#</cfoutput>

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

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

发布评论

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

评论(3

谜兔 2024-08-03 18:43:15

AFAIK,在 ColdFusion 中无法通过引用传递简单值。 我能想到的唯一解决方法是您已经在使用的方法。

相反,我建议尝试重组您的程序以适应该语言的粒度。 如果只有一个简单的值需要“修改”,您可以让函数返回新值,并像这样调用它:

<cfset SomeVar = TheFunctionName(SomeVar)>

如果您要修改多个值,请退一步考虑是否可以将这些多个值捆绑到 CFC 中,并将您的变元函数变成 CFC 的方法。 无论如何,这可能是更清晰、更可维护的解决方案。

AFAIK, there's no way to pass simple values by reference in ColdFusion. The only workaround I can think of is the one you're already using.

Instead, I would suggest trying to restructure your program to work with the grain of the language. In cases where there's only one simple value to "modify", you could just make your function return the new value, and call it like:

<cfset SomeVar = TheFunctionName(SomeVar)>

In cases where you're modifying multiple values, take a step back and think about whether it's possible to bundle those multiple values up into a CFC with your mutator functions becoming methods of the CFC. This could be clearer and more maintainable solution anyway.

缺⑴份安定 2024-08-03 18:43:15

您可以将函数外部和内部使用的变量安排在两个代码区域都存在的范围内。 例如,如果您将变量放入“会话”或“请求”范围中,您将能够从函数内访问它。 所做的更改将持续存在。

请注意,当您执行此操作时,您实际上并未将变量“传递”给函数。 该函数只是假设变量存在或创​​建它,具体取决于您的编码方式。

<cffunction name="TheFunctionName">
     <cfset Request.StrVar = "inside function<br />" />
</cffunction>

<cfscript>
    Request.StrVar = "outside function<br />";
    WriteOutput(Request.StrVar);
    TheFunctionName();
    WriteOutput(Request.StrVar);
</cfscript>

关于 ColdFusion 范围

如果对调用页面声明需要时提前变量,您必须使用 标记或 IsDefined() 函数。

You can arrange for the variables used outside and inside the function to be in a scope that exists in both code areas. For example, if you put a variable in the "session" or the "request" scope you will be able to access it from within the function. The changes made will persist.

Note that when you are doing this you aren't actually "passing" the variables to the function. The function just assumes the variable exists or creates it, depending on how you code it.

<cffunction name="TheFunctionName">
     <cfset Request.StrVar = "inside function<br />" />
</cffunction>

<cfscript>
    Request.StrVar = "outside function<br />";
    WriteOutput(Request.StrVar);
    TheFunctionName();
    WriteOutput(Request.StrVar);
</cfscript>

About ColdFusion Scopes

If there is any doubt about the calling page declaring the variable in advance when it is required you'll have to do some legwork with the <cfparam> tag or IsDefined() function.

鹿童谣 2024-08-03 18:43:15

如果您:

  1. 在 CFC 内部声明该函数,
  2. 则使用调用该函数;

您将能够指定参数“returnvariable”,然后根据需要输出该变量。

<cfinvoke component="this" method="TheFunctionName" returnvariable="blah">
     <cfinvokeargument name="data" value="whatever" type="string">

     <cfreturn data>
</cfinvoke>

<cfdump var="#blah#">

如果您在 cfscript 中编写所有内容,那么我会按照 SurroundedByFish 所说的进行。

If you:

  1. declare the function inside of a CFC
  2. invoke the function using <cfinvoke>

You would be able to specify the <cfinvoke> parameter "returnvariable", and then output that variable however you like.

<cfinvoke component="this" method="TheFunctionName" returnvariable="blah">
     <cfinvokeargument name="data" value="whatever" type="string">

     <cfreturn data>
</cfinvoke>

<cfdump var="#blah#">

If you are writing everything in cfscript, then I would go with what SurroundedByFish said.

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