Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Converting a Multidimentional Array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AMP - 28 Mar 2008 17:34 GMT
Hello,
I am trying to convert MD array of ints to doubles;
I just want each x to be converted.
I am getting a Nullexception.
I was able to do this with a single dimention, but I am having
problems here.
OurData is passed correctly but GraphData looks like one dimention
when I get to Line commented with an error

public double[][] GraphData;
       public Form3(int[][] OurData)
       {
           GraphData = new double[OurData.Length][];
           for (int y=1;y<OurData.Length;y++)
           {
           foreach (int x in OurData[y])
           {
               GraphData[y][x] = System.Convert.ToDouble(x);    //
ERROR
           }

           InitializeComponent();
           }
       }

Thanks for your help
Mike
Family Tree Mike - 28 Mar 2008 17:57 GMT
Notice that your inner loop is using the value of the array as the index into
it.  I believe your inner loop should be a more traditional loop, as in for
(int idx = 0; idx < ourdata [y].length; ++idx).  Also, your outer loop
"probably" is supposed to start at zero, but that is your choice.

> Hello,
> I am trying to convert MD array of ints to doubles;
[quoted text clipped - 23 lines]
> Thanks for your help
> Mike
NvrBst - 28 Mar 2008 19:03 GMT
On Mar 28, 9:57 am, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.com> wrote:
> Notice that your inner loop is using the value of the array as the index into
> it.  I believe your inner loop should be a more traditional loop, as in for
[quoted text clipped - 30 lines]
>
> - Show quoted text -

FTM is correct, its usally, conceptually, easier to use traditional
loops, in this case.  The reasion your code gives a null error
reference is because you never allocate the inner "double[]" (will
need two "new" keywords in your code for a double[][]).  To conver a
int[][] to double[][] you can do something like the following.

-------------------
           int[][] OurData = { new int[] { 5, 3, 1 }, new int[]
{ 3 }, new int[] { 1, 2, 3, 4, 5 } };
           double[][] GraphData = new double[OurData.Length][];

           for(int i = 0; i < OurData.Length; i++) {
               GraphData[i] = new double[OurData[i].Length];
               for(int ii = 0; ii < OurData[i].Length; ii++)
GraphData[i][ii] = (double)OurData[i][ii];
           }
-------------------

Alternativly, if you like foreach loops, you can do it the same way
you would a while loop (with manual counters - UntestedCode);
-------------------
           int oCounter = 0, iCounter = 0;
           foreach(int[] A in GraphData) {
               GraphData[oCounter++] = new
double[OurData[oCounter]].Length];
               foreach(int B in A) GraphData[oCounter][iCounter++] =
(double)B;
               iCounter = 0;
           }
-------------------

Cheers, NB

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.