Ok so I am making a sudoku solver for fun (yes I know it's already been made many times over) so to let people input there values before solving I used numericalupdown (81 one of them to be exact)
and i wanted to assign all of them to an array:
int[,] Sudoku = new int[9, 9];
and then on clicking "solve" the first thing it's supposed to do is put all the values in the array:
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 81; x++)
{
for (int y = 0; y < 9; y++)
{
if (x-1 == 0)
{
Sudoku[x - 1, y - 1] = Convert.ToInt32(numericUpDown[y].Value);
}
else
{
Sudoku[x - 1, y - 1] = Convert.ToInt32(numericUpDown[x][y].Value);
}
}
}
}
obviously you can't do: "numbericupdown[y]" but thats for you to see what I am trying to do....
sooooo thoughts?
THANKS,
Craiggles
If you put your numericUpDown controls into a 9x9 grid just like you have for the results, then copying the values will be straightforward.
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
Sudoku[x, y] = Convert.ToInt32(numericUpDown[x, y].Value);
}
}
}
If the controls are actually all dropped onto the form, then don't do that, remove them from the form. Use code to create them, place them programmatically, and put them into a 2d array. Since this is a learning program anyways, that's good practice for doing GUI's programmatically instead of just by drag-n-drop.
I would go about constructing/placing them dynamically and at the same time putting them into an array or list, but if they are already laid out onto a form you can just add them to a generic list to iterate over them.
List<NumericUpDown> nums = new List<NumericUpDown>();
nums.add(numericUpDwown1);
then you could calculate nums[1]*nums[9]... or whatever
I think I understand what you are doing... Two suggestions: 1) put all of your controls into an array of NumericaUpDown objects. Then you could do numericUpDown[y].Value. Otherwise, 2) name the controls ending with numbers (or some similar convention) then use reflection to find the right one and get its value.
Actually there is no reason at all why you cannot create controls and store them in various collection classes - not just arrays.
I would consider putting these into some sort of structured set of collections that reflect the structure of a sudoku grid.
You could dynamically create and place the controls on the form as well as holding them in the collection.
Related
I have a class (Biome) which contains a 2D array of custom objects (Level). I frequently want to loop through all (or some) of the levels and perform some actions, e.g. set their colour property.
Rather than writing the same nested for-loop over and over again, can I make my own method which will let me do something like: ForEachLevel(SetColour())?. Where SetColour() would be a method in my Biome class which just sets the Level's colour property to some random value or using some other logic based on factors within the Biome?
So instead of:
for (int r = 0; d < Rows; r++)
{
for (int c = 0; l < Cols; c++)
{
if (some logic here)
Levels[r, c].Colour = Color.Red;
else
Levels[r, c].Colour = Color.Green;
}
}
I could do something like:
ForEachLevel(SetColour(--what goes here?--));
void SetColour(Level level){
if (some logic here)
level.Colour = Color.Red;
else
level.Colour = Color.Green;
}
Or even better, I'd like to make something similar which only runs a function on, say, rows X through to Y.
As you can see from my example I don't even know how I'd get the context of each Level instance into the SetColour function.
I can keep copy/pasting my custom nested for-loops to achieve what I want, but I was hoping someone with more experience might be able to understand what I'm trying to do and point me to the right direction on how I can use better C# techniques.
Because I've been vague I understand if a specific answer cannot be given, but some key concepts for further research would be appreciated!! Thanks
EDIT2 actually that previous attempt doesnt work at all. I forgot I need to actually call ForEachLevel somewhere else. Still working on it.
private void SomeOtherMethod()
{
// where I want to actually use the ForEachLevel()
ForEachLevel(SetLevelColor(--Level??--, Color.red));
}
private void ForEachLevel(Action DoAThing)
{
for (int d = 0; d < Depths; d++)
{
for (int l = 0; l < Lanes; l++)
{
DoAThing();
}
}
}
private void SetLevelColor(Level level, Color color)
{
// trivial example
level.Color = color;
}
Im developing a small app/game in Unity3D.
The problem is:
I need to clone an array (call it tempArray) and make some modifications to it. Then i need to change values of the MAIN array to modified tempArray. However, whenever i make changes to cloned array, the same changes are made to the main one.
So i used the following code:
private Cell[,] allCells = new Cell[256, 256];
private Cell[,] cellClone = new Cell[256,256];
//meanwhile initiated to some values//
//Here i clone the array.
cellClone = (Cell[,])allCells.Clone();
//Here i output the values for an element from both arrays.
Debug.Log(cellClone[0, 0].region.name.ToString());
Debug.Log(allCells[0, 0].region.name.ToString());
//Here i want to change "Region" variable of cellClone ONLY.
cellClone[0, 0].setRegion(new Region("testregion123", Color.cyan, false));
//Finally, i output the same values again. Only cellClone should change.
Debug.Log(cellClone[0, 0].region.name.ToString());
Debug.Log(allCells[0, 0].region.name.ToString());
However, the output shows that allCells[0,0] element was also changed. This means that any operation I do to the cloned array, is executed to the main array.
EDIT:
After alot of playing around I implemented this as a solution. Im posting this in case anybody has a similar problem.
But Im not sure if this is how its supposed to be done so if anybody has any information - Ill be checking this post.
for (int i = 0; i < allCells.GetLength(0); i++)
{
for (int j = 0; j < allCells.GetLength(1); j++)
{
//cellClone[i, j] = allCells[i, j].Clone();
//cellClone[i, j] = new Cell((int)allCells[i, j].position.x, (int)allCells[i, j].position.y, allCells[i, j].getRegionName());
cellClone[i, j] = allCells[i, j].clone();
}
}
And the clone function:
public Cell clone()
{
Cell n = new Cell((int)position.x, (int)position.y, regionName);
return n;
}
However, the output shows that allCells[0,0] element was also changed. This means that any operation I do to the cloned array, is executed to the main array.
Unless Cell is a struct, your setRegion method (which sounds like it should really just be a Region property) isn't changing the contents of the array at all. It's changing the data stored in the object that both arrays contain a reference to.
You're performing a shallow clone of the array, which means the references are being copied - but each Cell object is not being cloned. (We don't even know whether that operation has been implemented within Cell.)
It sounds like you want to perform a deep clone, something like:
for (int i = 0; i < allCells.GetLength(0); i++)
{
for (int j = 0; j < allCells.GetLength(1); j++)
{
cellClone[i, j] = allCells[i, j].Clone();
}
}
... where you'd need to implement the Clone method yourself. (It may need to clone the region in turn, for example.)
Check this out:
Array.Copy
Array.Copy (Array, Array, Int32)
Easier and only 1 line of code;
when i creat unlimited textbox in gridview dynamically how can i access them?
for example:
int uste_uzaklik = 30;
int nesne = ListBox1.Items.Count;
Array.Resize(ref textboxarray, nesne * nesne);
for (int str = 0; str < nesne; str++)
{
for (int stn = 0; stn < nesne; stn++)
{
textboxarray[idm] = new TextBox();
textboxarray[idm].Font.Bold = true;
textboxarray[idm].Font.Name = "Verdana";
textboxarray[idm].ID = idm.ToString();
textboxarray[idm].ToolTip = textboxarray[idm].ID;
GridView2.Rows[str].Cells[stn + 1].Controls.Add(textboxarray[idm]);
if (str == stn) textboxarray[idm].Enabled = false;
uste_uzaklik += 30;
idm++;
}
}
i add texboxes in gridview...you can imagine a matris...
there is no problem...
but when i access them like this:
if (((TextBox)(GridView2.Rows[str].Cells[stn].FindControl(idm.ToString()))).Text != null)
{
matris[i, j] = Convert.ToInt32(GridView2.Rows[str].Cells[stn].Text);
}
occur an error
Object reference not set to an instance of an object.
how can i solve this problem?
References you have to controls don't cease to exist you add them to another control. You've already created an array of your TextBoxes, and you should use that to access them instead of trying to dig into the GridView in which you've added them every single time you want to change them.
Granted, you're going from a one-dimensional array of TextBoxes to a two-dimensional layout within the GridView, so you'll either have to find some way to establish how the indices match up between the two. Or, more easily, you could just turn textboxarray into a two-dimensional array and just have it exactly match the way it's laid out in the GridView. Either way, I think it'll be a lot less work than having to muck around in the GridView.
I have list called “images” which is contained of the series of bitmap images.
My question is how can I loop through every single element of my “images” list and do this operation for different elements of my list “images”?
You want to keep collections for each element of images, right?
You'll need to maintain a list of lists, or something similar:
List<List<int>> stuffOfImages = new List<List<int>>();
for (int x = 0; x < images[i].Width; x++) {
var stuffOfImage = new List<int>();
stuffOfImages.Add(stuffOfImage);
for (int y = 0; y < images[i].Height; y++) {
}
}
Note that you lose all reference to the associated image - it might be worthy creating a type so you can keep a reference to the image and the integer collection associated with it in one place and related.
I'm a little unclear as to what you mean, so please elaborate if necessary.
Making game of life I need to a have a grid that is 30x20 (X * Y). The problem is (I had another question regarding to that) that the c# arrays are rows, columns. So when I use CursorPosition() to drawing I need to swap it because it wants column at first.
Is there any way how I can reverse it so I can use like this?
int [,] Array = new int[30,20];
Console.SetCursorPosition(29,19) // now its vice versa, I would need to use 19,29.
I believe that this is purely conceptual (c# arrays are neither row/col or col/row that is up to the developer) and comes down to iterating your array in either a depth-first or breadth-first manner e.g.
//Breadth-first
for(int x = 0; x < col.Length; x++)
{
for(int y = 0; y < row.Length; y++)
{
}
}
//Depth-first
for(int y = 0; y < row.Length; y++)
{
for(int x = 0; x < col.Length; x++)
{
}
}
At first I was inclined to answer no as the parameters to Console.SetCursorPosition is Positional parameters. But when I remember that C# have Named parameters too so something like this works.
int a = 10;
int b = 20;
Console.SetCursorPosition(top: a, left: b);
This is the closest you can get, if you want to know why, search for the terms above
What you need is a data structure to store date in relation with an x,y coordinate.
You do not have to use a multi-dimensional array for this. You could very easily create a class that hides the specific implementation from the other classes.
In fact this will make your design more robust.
You can store the data in a database, bitarray, single dimension array, etc.