Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
void[] Stack01 = { Card01(),Card02() };  <<<<<<?????????

        public void Card01()
        {
            point = 1;
        }
        public void Card02()
        {
            point = 2;
        }


What I have tried:

i dont lie, i tried the code and didnt work. :)
Posted
Updated 7-Dec-16 7:43am
v6
Comments
[no name] 7-Dec-16 12:10pm    
You seriously need to learn how to ask a question.
_Q12_ 7-Dec-16 12:11pm    
:) english = my second lang. :)
But, feel free to -reformulate-.
[no name] 7-Dec-16 12:16pm    
So what?
[no name] 7-Dec-16 12:23pm    
Reformulate what? Reformulate "didn't work"? How can anyone but you do that? We can't read your mind. You would think after 9 years you would know this by now.

You can't create a array of void - it's a special keyword that means "returns no value" and it's not a data type at all. You can only create arrays of reference and value types, both of which are derived from object and void is neither of those.

What you are trying to do is set up an array of delegates that return no value:
C#
private delegate void pd();

Then you can use them in your code:
C#
pd[] stack01 = { Card01, Card02 };

C#
stack01[0]();
Console.WriteLine(point);
stack01[1]();
Console.WriteLine(point);
 
Share this answer
 
v2
Comments
_Q12_ 7-Dec-16 12:38pm    
wow, very cool !
but I get Red squiggly lines inside the array declaration.
"A field initializer cannot reference the non-static field, method, or property ..."
I am using VS2010 - not the latest version.
OriginalGriff 7-Dec-16 12:46pm    
That's because you put the declaration outside a method, and the initialise is trying to reference non-static (i.e. class instance specific) methods.
If you need to use non-static methods, then you can't use an initialiser outside a non-static method.
Try this:

private delegate void pd();
private pd[] stack01 = null;

and then put this in your class constructor:

stack01 = new pd[]{ Card01, Card02 };

_Q12_ 7-Dec-16 13:01pm    
uuuh - thank you so much!
I've made them public to be seen from outside the object!
And is working like a charm.
Truly WOW !
Amazing!
Thank you!
OriginalGriff 7-Dec-16 14:00pm    
You're welcome!
Instead of using void or a user defined delegate you can use the System.Action delegate (available since .NET 3.5):

C#
class Test
    {
        readonly Action[] Stack01;
        int point;

        public Test()
        {
            Stack01 = new Action[] { Card01, Card02 };
        }    
            
        // Invoke card with index
        public void Card(int index)
        {
            Stack01[index - 1]();
        }

        public void Card01()
        {
            point = 1;
        }
        public void Card02()
        {
            point = 2;
        }
    }
 
Share this answer
 
Comments
_Q12_ 7-Dec-16 13:50pm    
thanks!
I strongly suggest you download .NET Book Zero by Charles Petzold[^] and spend some time learning C# properly.
 
Share this answer
 
Comments
_Q12_ 7-Dec-16 13:47pm    
I had downloaded the book. I will read it(i hope).
Though, I must mention, I do programming by fun, and if I can learn something new along the way...eh... but, I will never be a (real) programmer. I have that feeling. :)
Thank you for your nice gift.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900