标签: scope

如何在VBA中声明全局变量?

我写了以下代码:

Function find_results_idle()

    Public iRaw As Integer
    Public iColumn As Integer
    iRaw = 1
    iColumn = 1
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

"Sub或Function中的无效属性"

你知道我做错了吗?

我尝试使用Global而不是Public,但遇到了同样的问题.

我试图将函数本身声明为`Public,但这也没有用.

创建全局变量需要做什么?

excel vba scope global-variables

128
推荐指数
5
解决办法
96万
查看次数

使代码内部但可用于其他项目的单元测试

我们将所有单元测试都放在他们自己的项目中.我们发现我们必须将某些类公开而不是内部仅用于单元测试.无论如何都要避免这样做.通过将类公开而不是密封来实现内存含义是什么?

c# unit-testing scope

123
推荐指数
3
解决办法
6万
查看次数

Javascript中的全局变量跨多个文件

我的一堆JavaScript代码位于名为helpers.js的外部文件中.在调用此JavaScript代码的HTML中,我发现自己需要知道是否已调用helpers.js中的某个函数.

我试图通过定义创建一个全局变量:

var myFunctionTag = true;
Run Code Online (Sandbox Code Playgroud)

在我的HTML代码和helpers.js中的全局范围.

下面是我的HTML代码:

<html>
...
<script type='text/javascript' src='js/helpers.js'></script>    
...
<script>
  var myFunctionTag = false;
  ...
  //I try to use myFunctionTag here but it is always false, even though it has been se t to 'true' in helpers.js
</script>
Run Code Online (Sandbox Code Playgroud)

我想做的甚至是可行的吗?

javascript scope global global-variables

121
推荐指数
3
解决办法
21万
查看次数

Spring Java Config:如何使用运行时参数创建原型范围的@Bean?

使用Spring的Java Config,我需要使用只能在运行时获得的构造函数参数来获取/实例化原型范围的bean.请考虑以下代码示例(为简洁起见而简化):

@Autowired
private ApplicationContext appCtx;

public void onRequest(Request request) {
    //request is already validated
    String name = request.getParameter("name");
    Thing thing = appCtx.getBean(Thing.class, name);

    //System.out.println(thing.getName()); //prints name
}
Run Code Online (Sandbox Code Playgroud)

Thing类的定义如下:

public class Thing {

    private final String name;

    @Autowired
    private SomeComponent someComponent;

    @Autowired
    private AnotherComponent anotherComponent;

    public Thing(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意事项namefinal:它只能通过构造函数来提供,并保证不变性.其他依赖项是Thing类的特定于实现的依赖项,并且不应该知道(紧密耦合到)请求处理程序实现.

此代码与Spring XML配置完美配合,例如:

<bean id="thing", class="com.whatever.Thing" scope="prototype">
    <!-- other post-instantiation properties omitted --> …
Run Code Online (Sandbox Code Playgroud)

java spring scope prototype spring-java-config

121
推荐指数
4
解决办法
8万
查看次数

在Java中,类静态中的枚举类型是什么?

我似乎无法从枚举中访问周围类的实例成员,因为我可以从内部类中访问.这是否意味着枚举是静态的?是否可以访问周围类的实例的范围,或者我是否必须将实例传递到我需要它的枚举方法中?

public class Universe {
    public final int theAnswer;

    public enum Planet {
        // ...
        EARTH(...);
        // ...

        // ... constructor etc.

        public int deepThought() {
            // -> "No enclosing instance of type 'Universe' is accessible in this scope"
            return Universe.this.theAnswer;
        }
    }

    public Universe(int locallyUniversalAnswer) {
        this.theAnswer = locallyUniversalAnswer;
    }
}
Run Code Online (Sandbox Code Playgroud)

java enums scope

118
推荐指数
2
解决办法
7万
查看次数

对于自己的花括号内的循环

我遇到过这种for循环布局:

#include <iostream>
int main()
{
    {
        for (int i = 0; i != 10; ++i)
        {
            std::cout << "delete i->second;" << std::endl;
        }
    }

    {
        for (size_t i = 0; i < 20; ++i)
        {
            std::cout << "delete m_indices[i];" << std::endl;
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想知道这个额外的支撑层是什么用的?这在我们的代码库中出现了几次.

c++ scope for-loop

117
推荐指数
2
解决办法
8944
查看次数

嵌套类的范围?

我试图理解Python中嵌套类的范围.这是我的示例代码:

class OuterClass:
    outer_var = 1
    class InnerClass:
        inner_var = outer_var
Run Code Online (Sandbox Code Playgroud)

类的创建没有完成,我收到错误:

<type 'exceptions.NameError'>: name 'outer_var' is not defined
Run Code Online (Sandbox Code Playgroud)

尝试inner_var = Outerclass.outer_var不起作用.我明白了:

<type 'exceptions.NameError'>: name 'OuterClass' is not defined
Run Code Online (Sandbox Code Playgroud)

我试图访问静态outer_varInnerClass.

有没有办法做到这一点?

python scope nested class inner-classes

113
推荐指数
3
解决办法
14万
查看次数

javascript:递归匿名函数?

假设我有一个基本的递归函数:

function recur(data) {
    data = data+1;
    var nothing = function() {
        recur(data);
    }
    nothing();
}
Run Code Online (Sandbox Code Playgroud)

如果我有匿名功能,我怎么能这样做...

(function(data){
    data = data+1;
    var nothing = function() {
        //Something here that calls the function?
    }
    nothing();
})();
Run Code Online (Sandbox Code Playgroud)

我想要一种方法来调用调用这个函数的函数...我已经看到某个地方的脚本(我记不清哪里)可以告诉你一个被调用的函数的名字,但我记不起任何一个那个信息现在.

javascript recursion scope anonymous-function

112
推荐指数
7
解决办法
6万
查看次数

Angular 2 - 在setTimeout中使用'this'

我班上有这样的功能

  showMessageSuccess(){

    var that = this;
    this.messageSuccess = true;

    setTimeout(function(){
      that.messageSuccess = false;
    },3000);

  }
Run Code Online (Sandbox Code Playgroud)

我怎样才能重写这个,所以我不必在'that'var中存储对'this'的引用?如果我在setTimeout中使用'this',则messageSuccess bool似乎不会更改/获取更新.

javascript scope angular

110
推荐指数
1
解决办法
17万
查看次数

为什么从函数返回向量是可以的?

请考虑此代码.我已经多次看过这种类型的代码了.words是一个本地向量.如何从函数中返回它?我们可以保证它不会死吗?

 std::vector<std::string> read_file(const std::string& path)
 {
    std::ifstream file("E:\\names.txt");

    if (!file.is_open())
    {
        std::cerr << "Unable to open file" << "\n";
        std::exit(-1);
    }

    std::vector<string> words;//this vector will be returned
    std::string token;

    while (std::getline(file, token, ','))
    {
        words.push_back(token);
    }

    return words;
}
Run Code Online (Sandbox Code Playgroud)

c++ scope stl vector standard-library

106
推荐指数
4
解决办法
13万
查看次数