if I have an 2 dimensional array eg
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
how do I add 8 to the first item of the third row eg 5 to make
[3, 2] { {1, 2}, {3, 4}, {13, 6} };
thnx
mrcw
7 1744
Your initializer creates the following:
numbers[0,0] = 1
numbers[0,1] = 2
numbers[1,0] = 3
numbers[1,1] = 4
numbers[2,0] = 5
numbers[2,1] = 6
So to add 8 to the first item of the third row:
numbers[2,0] = numbers[2,0] + 8;
You need to access the appropriate index for that item. In C#, array indexes start at zero, so according to your setup the third row would be index 2, and the first item of that row would be index 0. - numbers[2,0] += 8;
-
// or...
-
numbers[2,0] = numbers[2][0] + 8;
Here's the MSDN page on multidimensional arrays... http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx
*Edit: Ahhh look at me, getting caught up by syntax. Fixed!
can I use that code in a windows form application ? It works fine as a console application, but as a wpa it gives me lots of nonsense errors
mrcw
It should work fine. Please provide a copy of the code from your app along with the error message
int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; is fine -no errors
numbers[2, 0] = numbers[2, 0] + 8; red wriggly line under both 2, = and +
Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Error 2 Invalid token '=' in class, struct, or interface member declaration
Error 3 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
Error 4 Invalid token '+' in class, struct, or interface member declaration
As was earlier suggested, try it as numbers[2][0]
hi
I am trying the examples from http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
This gives me 11 errors that are copies of the ones I gave before
I still qustion "can I use arrays in wpa? It does appear not.
mrcw -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
-
-
namespace Arrays1
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
-
int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
-
numbers[2, 0] = numbers[2][0] + 8;
-
-
//int[,] myArray = new int[4,2];
-
// int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
-
-
int[,] myArray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
-
myArray[1,0] = myArray[1,3] + 5;
-
-
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
-
array2D[2 ,1 ] = 25;
-
-
-
//int[,] array5;
-
//array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
-
//array5[2, 1] = 25;
-
-
int[,] array6 = new int[10, 10];
-
-
}
-
}[
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Treetop |
last post by:
I cannot get this array to work. I want to have the game listed until the
day after the event, then come off the list.
function events() {
var today = new Date();
var dayarray=new...
|
by: Mike S. Nowostawsky |
last post by:
I tried using the "toUpperCase()" property to change the value of an array
entity to uppercase BUT it tells me that the property is invalid. It seems
that an array is not considered an object when...
|
by: Andrew Poulos |
last post by:
If I'm searching for an occurance of a value in a multi-dimensional
array how can I get it's index returned as an array, if found? For
example, if:
foo = new Array();
foo = , 5, , 9, 10];
...
|
by: VK |
last post by:
Or why I just did myArray = "Computers" but myArray.length is
showing 0. What a hey?
There is a new trend to treat arrays and hashes as they were some
variations of the same thing. But they...
|
by: bg_ie |
last post by:
Hi,
I have an array as follows -
var MultiArray = new Array(2);
MultiArray = new Array(num);
MultiArray = new Array(num);
I've tried randomizing this array using the following but its...
|
by: hpy_awad |
last post by:
I know that group is for C not for C++ but please try to help me ,
How can I declare size of an array as variable value came from screen?
#include "iostream.h"
#include "iomanip.h"
#include...
|
by: jar13861 |
last post by:
I'm confused on how to write a random array that will only generate 9
different numbers from 1-9. Here is what I have, but its only writing
one number....
holder = new Array ( 9 );
var flag =...
|
by: mr.resistor |
last post by:
hey
i am having a few problems calling a C DLL from C#. i am using a simple
function that takes an array of floats and an integer as an input, but
i cannot seem to get it to work. when i try to...
|
by: mdh_2972 |
last post by:
I have an array of over 1000 links in a .JS file. I do not want to put
the whole thing on my page because it would take to long to render the
page. So how can I randomly pick 1 element from the...
|
by: dennis.sam |
last post by:
Hi,
Is there away to define a multi-dimensional array with respect to the
number of dimensions the array has? For example, given a user spec of
"a b c d", I want to create a 4 dimensional array...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |