Implement Write-Once Read-Many Properties

Implement Write-Once Read-Many Properties

Creating a read-only property or a write-only property isn’t difficult, as you probably know: just omit the Property Let (or Set, if dealing with objects) or the Property Get procedure, respectively.

There are cases, however, when you want to implement a write-only-read-many property, that is, a property that can be assigned just once and be read any number of times. For example, the ID property of an object should never change after it has been assigned, so you should make it a WORM property. More in general, any property that is then used to retrieve additional information about an object from a database should be a WORM property.

Visual Basic doesn’t offer a built-in mechanism for implementing a write-once read-many property, but it’s easy to do it yourself, using a Static variable, as in the following code snippet:

Private m_ID As LongProperty Get ID() As Long    ID = m_IDEnd PropertyProperty Let ID(newValue As Long)    Static initialized As Boolean    If initialized Then        Err.Raise 1001, , "Can't assign this property more than once"    End If    m_ID = newValue    ' remember that the property has been set    initialized = TrueEnd Property

If the property has some special or invalid value, you don’t even need to declare an additional Static variable. For example, suppose that the ID property can’t take negative values:

Private m_ID As LongPrivate Sub Class_Initialize()    ' initialize ID with an invalid value    m_ID = -1End SubProperty Get ID() As Long    ID = m_IDEnd PropertyProperty Let ID(newValue As Long)    If m_ID <> -1 Then        Err.Raise 1001, , "Can't assign this property more than once"    ElseIf newValue < 0 Then        Err.Raise 1002, , "Invalid value for the ID property"     End If    m_ID = newValueEnd Property

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as