Boolean Variable array and Variable parsing.

Ok as far as variable arrays. I'm workign with Visual c++ and they have TextBox and Label as objects you can use for building forms.

This is how I can succesfully build an array full of Labels.

array<Label^> ^ ActiveBLabels = gcnew array<Label^>{label1,label2,label3};

However when I try this it doesn't work

array<bool^> ^ test = gcnew array<bool^>{ randombool1, randombool2 };

at best I can do this
bool arr[2] = {randombool1, randombool2};

However that doesn't put variables into the array. It puts values of those variables. I need to be able to access the variables themselves.

-------------------------

Another question I have is how do you parse a variable.

Meaning lets say I have variables named. Variable1 Variable2 Variable3 Variable4

For a specific reason I need to look up the number lets say I want Variable3 if the loop item I'm looking at is 3 and then assing another variable the content of Variable3.

I haven't figured out a way to do this. I could do this with other languages very easily. But C++ seems difficult about this (for no reason at all).

Thank you ahead of time.
array<Label^> is not C++. C#?

You can use pointers, when you want to point to something in C++.
array<Label^> works for me in C++

definitely not C#.

I'm using Microsoft Visuam C++ 2010

How would I use pointers to do this? I'm very new I don't really understand pointers to be honest. I have used them but I don't really know enough about them. They seem a bit alien to me. I'm used to scripting where everything is just letters.
nick050183 wrote:
array<Label^> works for me in C++
There is a huge difference between "C++" and "C++/CLI".

http://en.wikipedia.org/wiki/C%2B%2B/CLI
Last edited on
I figured there was enough similiarity to where people could help me with:

1) Making an array with Boolean variables

2) Parsing variables. So Variable1 Variable2 Variable3 finding that 1,2,3 and assigning or grabbing information from those variables.

I guess nobody knows. Haven't been able to google it either. Sad because much simpler languages can do this with absolute ease. Yet C++....... its a big mystery :(
http://www.cplusplus.com/doc/tutorial/pointers/

What you seem to want is an array of pointers.
nick050183 wrote:
I guess nobody knows. Haven't been able to google it either. Sad because much simpler languages can do this with absolute ease. Yet C++....... its a big mystery :(
You're using C++/CLI, not C++. Considering Microsoft made C++/CLI, it's no wonder it's a big mystery.
LB:

How would you do it in C++?

I've asked a few questions here before... with C++ in mind. And the answers have worked.

So it's not like it's a completely different thing. There are differences. But there is enough similarities to where I can ask questions here about it.
You want simple? bool * foo[2];
Unfortunately this does not work.

I have bool variables named
1
2
3
4
5
6
bool Bool1 = false;
bool Bool2 = false;

bool * foo[2] = { Bool1, Bool2 };

foo[0] = false;


This does not set Bool1 to false. Heck it doesn't even compile. I tried &Bool1 that doesnt work either.

This half way works
array<bool> bah = gcnew array<bool>

But all it does is grab the values from whatever I assign, it doesn't actually store the variables themslves. So for example if I assigned Bool1 and Bool2, it would only grab the true/false values from them but it wouldn't hold the variables themselves. So I have to go back and do this

1
2
Bool1 = bah[0];
Bool2 = bah[1];


Which is a workaround sure. But it's very ugly and god forbid I had a big array.

There has to be a better way of doing this. So far nobody knows :(
I tried &Bool1 that doesnt work either.

Really?
1
2
3
4
5
6
bool Bool1 = false;
bool Bool2 = false;
bool * const foo[] = { &Bool1, &Bool2 };
*(foo[1]) = true;
std::cout << Bool1 << '\n';
std::cout << Bool2 << '\n';

If you want to use pointers you need to learn how to use them:
1
2
3
4
5
6
bool Bool1 = false;
bool Bool2 = false;

bool * foo[2] = { &Bool1, &Bool2 };

*(foo[0]) = false;
http://www.cplusplus.com/doc/tutorial/pointers/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

void display( bool* );

int main()
{
    bool b1 = true;
    bool b2 = false;
    bool b3 = true;

    bool arrayOfBools[] = {b1,b2,b3};
    bool* a = arrayOfBools;

    cout << b1 << "-" << b2 << "-" << b3 << endl;

    for (int i = 0; i < 3 ; i++)
    {
        cout << arrayOfBools[i] << "-";
    }
    display ( a );
    display ( arrayOfBools );

    return 0;
}

void display( bool* xyz)
{
    for (int i = 0; i < 3 ; i++)
    {
        cout << xyz[i] << "-";
    }
    cout << endl;
}
Last edited on
@kemort I don't think that's what the OPer wants.
closed account (48T7M4Gy)
Neither do I LB. I just thought I'd follow the bool sidetrack for curiosity sake.

Unless the OP uses classes or structs with a name attribute then I doubt whether its possible to extract the variable name easily at runtime. It's obviously stored somewhere but where and in what form would be an interesting challenge. I suspect a futile one.
1
2
3
4
5
6
bool Bool1 = false;
bool Bool2 = false;
bool * const foo[] = { &Bool1, &Bool2 };
*(foo[1]) = true;
std::cout << Bool1 << '\n';
std::cout << Bool2 << '\n';


This works thank you.

Now I have a new problem.

Since this is C++/CLI and the Bool is declared outside of the function........ It throws up this disgusting error...

error C2440: 'initializing' : cannot convert from 'cli::interior_ptr<Type>' to 'bool *const '
1> with
1> [
1> Type=bool
1> ]
1> Cannot convert a managed type to an unmanaged type

If I use a bool that is declared within the function it's not a problem. But obviously for this to work I need to access the Bool I declared globally...... *SIGH*.

THIS IS SO OVERCOMPLICATED!

But I do thank you for answering the original question.
Topic archived. No new replies allowed.