Blub
Blub

Reputation: 581

How to change the value of a referenced variable stored in class?

So basically what I want to do is to store a reference to a boolean value in my class.

It looks something like this:

Public Class TestClass
    Private boolRef As Boolean

    Public Sub New(ByRef inBoolRef As Boolean)
        boolRef = inBoolRef 'Assign reference of "inBoolRef" to "boolRef"
    End Sub

    Public Sub changeBool()
        boolRef = true 'Change value of the referenced boolean variable (outside of this class)
    End Sub

End Class

The object of that class is running in a parallel thread, and that's why I want the object to control the variable in its own thread. And with my program doing this:

Module Program
    Dim myBool As Boolean = false

    Sub Main(args As String())
        Dim tC As New TestClass(myBool)
        'Opens a parallel thread in which the object does things
        'and should change "myBool" to true when the object is terminated
    End Sub
End Module

What I don't get is, that I declare a Boolean variable. Then I want another, in my Class saved variable to reference that variable (which was input in the ctor). In boolRef = inBoolRef I handle boolRef as if it was an actual reference. But in boolRef = true in changeBool() it seems as if it's not a reference anymore.

This question shows me, that it is possible in VB.Net and also works, at least with objects. But I can't assign different values to the myBool through means of the other instantiated object, which should store a reference to the variable. I mean, I could theoretically do something in the class like

Public Sub changeBool()
    boolRef.doSomethingLikeAssignAValue()
End Sub

but that won't work, because as far as I know, the Boolean is a primitive data type, and thus can not be changed by any Subs.

I come from the C(++) world and there I find it way more intuitive on how to handle references, pointers, etc.


TL;DR:

What I wan't to do is basically this (in C(++)):

Class BoolChanger
{
Private:
    bool *boolRef = nullptr;

Public:
    inline BoolChanger(bool *inBoolRef)
    {
        boolRef = inBoolRef;
    }

    inline void change()
    {
        *boolRef = true; // Change value of pointed-to boolean variable
    }
}

int main(int argc, char *argv[])
{
    bool myBool = false;

    // Open parallel thread in which the object runs an does its things
    BoolChanger bc(&myBool);
    // ... and when it's done, it should set myBool to "true"

    return 0;
}

but in VB.Net. Please can someone help me? I seem to be missing something which is important to know...

Upvotes: 0

Views: 242

Answers (1)

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

You can simply pass the parameter to the method directly instead of trying to assign it to a Boolean value first. Like this:

Public Class TestClass
    Private boolRef As Boolean

    Public Sub New()   
    End Sub

    Public Sub changeBool(ByRef inBoolRef As Boolean)
        inBoolRef = true
    End Sub

End Class

Then use I like this

Module Program
    Dim myBool As Boolean = false

    Sub Main(args As String())
        Dim tC As New TestClass
        tC.changeBool(myBool)
    End Sub
End Module

I don't know if there was a reason you didn't do it this way previously. Please let me know if your program does not support this way :)

Upvotes: 1

Related Questions