热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

数组可以声明为常量吗?-Cananarraybedeclaredasaconstant?

Isitpossibletoeither:有没有可能:Declareanarrayasaconstant将数组声明为常量OR或Useaworkaroundt

Is it possible to either:

有没有可能:

  1. Declare an array as a constant

    将数组声明为常量

    OR

  2. Use a workaround to declare an array that is protected from adding, deleting or changing elements, and therefore functionally constant during the life of a macro?

    使用一个变通方法来声明一个数组,该数组可以避免添加、删除或更改元素,因此在宏的生命周期中,功能常量?

Of course I could do this:

我当然可以做到:

Const myConstant1 As Integer = 2
Const myConstant2 As Integer = 13
Const myConstant3 As Integer = 17
Const myConstant4 ...and so on

...but it loses the elegance of working with arrays. I could also load the constants into an array, and reload them each time I use them, but any failure to reload the array with those constant values before use could expose the code to a "constant" value that has changed.

…但是它失去了使用数组的优雅性。我还可以将这些常量加载到一个数组中,并在每次使用它们时重新加载它们,但是在使用之前使用这些常量重新加载数组的任何失败都可能使代码暴露在已经更改的“常量”值中。

Any workable answer is welcome but the ideal answer is one that can be setup once and not require any changes/maintenance when other code is modified.

任何可行的答案都是受欢迎的,但是理想的答案是可以设置一次,并且在修改其他代码时不需要任何更改/维护。

6 个解决方案

#1


15  

You could use a function to return the array and use the function as an array.

您可以使用一个函数返回数组并将该函数用作数组。

Function ContantArray()
    COntantArray= Array(2, 13, 17)
End Function

enter image description here

enter image description here

#2


5  

How about making it a function? Such as:

把它变成一个函数怎么样?如:

Public Function myConstant(ByVal idx As Integer) As Integer
    myCOnstant= Array(2, 13, 17, 23)(idx - 1)
End Function

Sub Test()
    Debug.Print myConstant(1)
    Debug.Print myConstant(2)
    Debug.Print myConstant(3)
    Debug.Print myConstant(4)
End Sub

Nobody can change it, resize it, or edit its content... Moreover, you can define your constants on just one line!

没有人可以改变它,调整它的大小,或者编辑它的内容……此外,您可以在一行中定义常量!

#3


4  

I declared a String constant of "1,2,3,4,5" and then used Split to create a new array, like so:

我声明了一个字符串常量为“1,2,3,4,5”,然后使用Split创建一个新的数组,如下所示:

Public Const myArray = "1,2,3,4,5"

Public Sub createArray()

        Dim i As Integer
        A = Split(myArray, ",")

        For i = LBound(A) To UBound(A)
                Debug.Print A(i)
        Next i

End Sub

When I tried to use ReDim or ReDim Preserve on A it did not let me. The downfall of this method is that you can still edit the values of the array, even if you can't change the size.

当我试图使用ReDim或ReDim保存时,它没有让我。这种方法的缺点是,即使不能更改数组的大小,也可以编辑数组的值。

#4


0  

Can an array be declared as a constant? No.

数组可以声明为常量吗?不。

Workarounds - Simplest one I can think of is to define a constant with delim and then use Split function to create an array.

变通方法——我能想到的最简单的方法是使用delim定义一个常量,然后使用Split function创建一个数组。

Const myCOnstant= "2,13,17"

Sub Test()
    i = Split(myConstant, ",")

    For j = LBound(i) To UBound(i)
        Debug.Print i(j)
    Next
End Sub

#5


0  

No - arrays can't be declared as constant but you can use a workaround.

没有-数组不能被声明为常量,但是您可以使用一个变通方法。

You can create a function that returns the array you want

你可以创建一个函数来返回你想要的数组。

http://www.vbaexpress.com/forum/showthread.php?1233-Solved-Declare-a-Constant-Array

http://www.vbaexpress.com/forum/showthread.php?1233-Solved-Declare-a-Constant-Array

#6


0  

If the specific VBA environment is Excel-VBA then a nice syntax is available from the Excel Application's Evaluate method which can be shortened to just square brackets.

如果特定的VBA环境是Excel-VBA,那么Excel应用程序的Evaluate方法可以提供很好的语法,该方法可以缩短为方括号。

Look at this

看看这个

Sub XlSerialization1()
    Dim v
    v = [{1,2;"foo",4.5}]

    Debug.Assert v(1, 1) = 1
    Debug.Assert v(1, 2) = 2
    Debug.Assert v(2, 1) = "foo"
    Debug.Assert v(2, 2) = 4.5

    '* write all cells in one line
    Sheet1.Cells(1, 1).Resize(2, 2).Value2 = v
End Sub

推荐阅读
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • Learning to Paint with Model-based Deep Reinforcement Learning
    本文介绍了一种基于模型的深度强化学习方法,通过结合神经渲染器,教机器像人类画家一样进行绘画。该方法能够生成笔画的坐标点、半径、透明度、颜色值等,以生成类似于给定目标图像的绘画。文章还讨论了该方法面临的挑战,包括绘制纹理丰富的图像等。通过对比实验的结果,作者证明了基于模型的深度强化学习方法相对于基于模型的DDPG和模型无关的DDPG方法的优势。该研究对于深度强化学习在绘画领域的应用具有重要意义。 ... [详细]
  • 本文介绍了一个Python函数same_set,用于判断两个相等长度的数组是否包含相同的元素。函数会忽略元素的顺序和重复次数,如果两个数组包含相同的元素,则返回1,否则返回0。文章还提供了函数的具体实现代码和样例输入输出。 ... [详细]
  • node.jsrequire和ES6导入导出的区别原 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • C++中的三角函数计算及其应用
    本文介绍了C++中的三角函数的计算方法和应用,包括计算余弦、正弦、正切值以及反三角函数求对应的弧度制角度的示例代码。代码中使用了C++的数学库和命名空间,通过赋值和输出语句实现了三角函数的计算和结果显示。通过学习本文,读者可以了解到C++中三角函数的基本用法和应用场景。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
author-avatar
mobiledu2502868933
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有