Click here to Skip to main content
15,747,766 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
one dimension delegate is fine in my code ,however when i extend it to two dimension
i have got error

What I have tried:

delegate void SampleDelegate(object sender, MouseEventArgs e);

SampleDelegate[][] MyFun = {
                              { pb00MouseDown, pb01MouseDown },
                              { pb10MouseDown, pb11MouseDown }


//Eror in the above code

SampleDelegate pb00MouseDown = delegate (object sender, MouseEventArgs e)
{ MessageBox.Show("hellow 00");  };
SampleDelegate pb01MouseDown = delegate (object sender, MouseEventArgs e)
{ MessageBox.Show("hellow 01"); };
SampleDelegate pb10MouseDown = delegate (object sender, MouseEventArgs e)
{ MessageBox.Show("hellow 10");  };
SampleDelegate pb11MouseDown = delegate (object sender, MouseEventArgs e)
{ MessageBox.Show("hellow 11"); };
Updated 18-Aug-22 1:18am
OriginalGriff 12-Aug-22 11:00am    
"I have got error" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.
Engineer khalid 12-Aug-22 11:16am    
The error says
"array initializer can only be used in variable or field initializer. try using anew expression instead"

i have no problem when it is one dimension like this code
SampleDelegate[] MyFun = { pb1MouseDown, pb2MouseDown };
MyFun[0](sender, (MouseEventArgs)e);
Engineer khalid 12-Aug-22 11:18am    
if you need the whole (small) program i will write here
Engineer khalid 12-Aug-22 11:36am    
if one dimension
i have 2 picturebox when the cursor enter the 1st pictureBox and button is pressed it calls the
event mouseDown named pb1MouseDown using the delegate MyFun[0],same thing if the mousedown pressed when it in pictureBox2 it calls MyFun[1] or pb2MouseDown
i selected delegate rather than the unsafe pointer to function
i expect two dimension delegate can be easly done by changing to
SampleDelegate[][] MyFun = {
{ pb00MouseDown, pb01MouseDown },
{ pb10MouseDown, pb11MouseDown }
but it did not works

Are you the same person that posted Need to subscripe mousedown[^]? The two names look suspiciously similar. If you are, then please delete the duplicate userid.

But either way the original question suggested what you need to do.
Share this answer
Engineer khalid 12-Aug-22 11:47am    
yes i am he same person that posted Need to subscripe mousedown[^]? but the previous question was for one dimension and was solved but now for 2 dimension i got another error.

you wrote "delete the duplicate userid."
ok i am will to delete but would you please explain what you ment by userid do you mean this question ?
Richard MacCutchan 12-Aug-22 12:29pm    
No, you have two accounts "Engineer khalid" and Egr k - Professional Profile[^], so you need to close the second one.
Egr k 13-Aug-22 5:44am    
Some time i work in programming but i am a mechanical engineer when i do my engineer works i do not enter the codeproject that might explaine why i forget the name of my 1st user name and opened another account
kindly would you please transfer all of my questions that are in 1st account to the 2nd accounts ,i urgently need them

khalid sabtan
Your 2D array is called either a jagged array or alternatively an array of arrays. Here are some initialisation examples for string arrays. See my comments on the jaggedArray example to understand why it's initialisation cannot be as simple as the others.
// Inline init of a 1D array field is the most straightforward
private String[] linearArray = { "0", "1" };

// inline init of a 2D rectangular array field is similar
private String[,] rectangularArray = { { "0-0", "0-1" }, { "1-0", "1-1" } };

// inline init of 2D jagged array requires explicit declaration of the sub arrays
// as their lengths may differ.
private String[][] jaggedArray = { new String[] { "0-0", "0-1" }, new String[] { "1-0", "1-1", "1-2" } };

Share this answer
This is not another solution, it is a comment on Solution 2 but I am adding it in a 'Solution' section because I don't think that I can format code in a 'Comment' section.

The most compact (in source code, but not in runtime efficiency) way of creating a (pseudo) jagged array that I have found is ...
var numbers = new Dictionary<int, Dictionary<int, string>>
   [0] = { [0] = "zero-zero" },
   [1] = { [0] = "one-zero", [1] = "one-one" },
   [2] = { [0] = "two-zero", [1] = "two-one", [2] = "two-two" },
   [3] = { [0] = "three-zero", [1] = "three-one", [2] = "three-two", [3] = "three-three" }

Console.WriteLine(numbers[3][2]);   // three-two;
Share this answer
CHill60 18-Aug-22 7:21am    
If you want to format code in the comments use <pre> tags

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