Hi, How can I store the value of a variable into another variable that wont be changed unless it meets certain criteria - Thanks

Dani AI

Generated

: the short answer depends on the type you're working with and where the value must survive.

Value vs reference types

  • For primitives and structures (Integer, Double, structures) a plain assignment already takes a snapshot; later changes to the original variable do not affect the copy. For objects (class instances, arrays), assignment copies a reference — mutating the instance through one variable will be visible via the other. See for details.

Encapsulation pattern (keep a stored value until a validator allows change)

  • Wrap the stored value in a private field and expose a ReadOnly property; provide a single update method that applies your criteria. This keeps all validation in one place and prevents accidental overwrites from other code.
Private _saved As Integer = Integer.MinValue

Public ReadOnly Property SavedValue As Integer
    Get
        Return _saved
    End Get
End Property

Public Sub TryUpdateCandidate(candidate As Integer, validator As Func(Of Integer, Boolean))
    _saved = If(validator(candidate), candidate, _saved)
End Sub

Reference types — clone when you need a snapshot

  • If the thing you want to “freeze” is an object, create a copy (shallow or deep) instead of copying the reference. Implement a copy constructor or a Clone method so saved = current.Clone() produces an independent object.

Other notes

  • If you want a method to alter the caller's variable, VB supports ByRef parameters (see Passing arguments by value and by reference).
  • For ASP.NET page lifetimes, consider where the value must live (local variable, control, ViewState, Session) and guard updates via the same validation method to keep behavior consistent.
  • If multiple threads can update the value, make the update atomic (SyncLock or Interlocked) to avoid race conditions.

This builds on 's note about assignment and 's suggestion to gate changes with validation, while clarifying value vs reference behavior and practical patterns to keep a stored value unchanged until criteria are met.

Recommended Answers

All 2 Replies

Hi, How can I store the value of a variable into another variable that wont be changed unless it meets certain criteria - Thanks

consider a and b are two variables
the value of a is 5
now let me assign the value for b as
b=a
In the forthcoming statement if the value of 'a' changes,the value of b won't change because on the time of initailization to b the value of a is 5.
If you want to change the value of b when the value of a changes
you can use the reference concept

Hi, How can I store the value of a variable into another variable that wont be changed unless it meets certain criteria - Thanks

Have you not been introduced to If-Then-Else blocks?

If (Condition) Then
   Do Something
Else 'Optional
   Do SomethingElse
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.