【问题标题】:vb.net - Can module data be writeable within module, but public and read-only to other modules?vb.net - 模块数据可以在模块内写入,但对其他模块是公开的和只读的吗?
【发布时间】:2017-08-17 16:19:15
【问题描述】:

有没有办法声明模块内的数据结构可以由模块内的任何函数写入,但对不同模块中的函数是只读的?

也许这就像在 C++ 中让一个类返回另一个类一个只读 (const) 指针 1st class' 它的内部数据结构。

【问题讨论】:

标签: c# .net vb.net visual-studio


【解决方案1】:

您需要的是一个可以从程序中的任何地方访问的公共属性(只读)和一个仅在模块本身内部具有范围的私有字段。这是一个例子

Module myModule

   Private something As String 'This here is a Field

   'Below is the code for a read-only property
   Public Property SomethingWhichIsReadOnly As String
     'SomethingWhichIsReadOnly can be used from anywhere
     Get
        Return something         
     End Get
   End Property

   Public Function SomeFunction(ByVal value as Integer) As Boolean
      ...
      'You can use "something"(String Field declared above) in functions
      'Which can be accessed and modified only from the module itself
   End Function

End Module

如果你想创建一个可以读写的公共属性,那么使用

Public Property SomethingWhichIsReadOnly As String
  Get
      Return something 
  End Get
  Private Set(ByVal value As String)
      something = value 'Setting the value to the Private Field
  End Set
End Property

【讨论】:

  • 您可以通过在代码块前插入<!-- language: vb.net --> 将代码块的语法突出显示更改为 VB.NET(或任何其他语言)。您也可以写<!-- language-all: vb.net --> 来更改您帖子中所有代码块的语言。 -- 更多信息参见“代码的语法高亮”下的Markdown and Formatting help
  • 感谢您的编辑和信息。适当指出的一点。
【解决方案2】:

非常简单!
您可以使用公共属性私有字段

  • 使用外部模块(或类等)的公共属性
  • 内部使用的私有字段。

感谢没有人

【讨论】:

  • 技术上,1 个公共财产和 1 个私有字段
  • 您可以在命名中使用一个 Under Line char (_)
  • 我猜你错过了我所说的,1 个公共 Property 和 1 个私有 Field 。希望你知道区别
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 2021-11-09
相关资源
最近更新 更多