noob needs some quick c# help

Soldato
Joined
15 Feb 2011
Posts
10,234
Location
Slough
i've got a double that i need to round (in any direction) to an integer and then convert to an int so that i can use it as the value of an array.
i.e. i want to be able to go myarray[mydouble][x]

i've tried using the convert to int32 function but this is giving me an out of bounds error when it really shouldnt, so i guess this just takes the binary double and crosses out double and writes int, resulting in a massive number that is way out of bounds.

if anyone could explain what i need to do that would be great, because its the last little thing i need to do before the big project gets handed in on friday

thanks :)

ps. we have never been taught C#, and we have only been taught the very basics of C++, so if you could explain things in morons terms then that would be great
 
Can you post a bit more info up about what you're trying to do?
The fact that you have a value as a double that you're trying to use as an Array index sounds wrong to me.
 
Can you post a bit more info up about what you're trying to do?
The fact that you have a value as a double that you're trying to use as an Array index sounds wrong to me.

i'm trying to record the Z axis of the magnetic sensor against distance driven by our robot. the distance driven code needs to be a double as its extra precisionis needed elsewhere in our program (its a group project).

its all hooked up to some code i've "borrowed" from the internet which can export a 2D array into excel, and make a pretty 3D graph of the magnet sensor values. basically i'll be driving a bunch of stripes across some ground where "magnetic rocks" are buried because of a recent "volcanic explosion" (i'll be driving the rover over a large piece of MDF with some very strong magnets in it)

thanks chuck, i'll try your idea in the lab tomorrow morning :)
 
Last edited:
i'm trying to record the Z axis of the magnetic sensor against distance driven by our robot. the distance driven code needs to be a double as its extra precisionis needed elsewhere in our program (its a group project).

its all hooked up to some code i've "borrowed" from the internet which can export a 2D array into excel, and make a pretty 3D graph of the magnet sensor values. basically i'll be driving a bunch of stripes across some ground where "magnetic rocks" are buried because of a recent "volcanic explosion" (i'll be driving the rover over a large piece of MDF with some very strong magnets in it)

thanks chuck, i'll try your idea in the lab tomorrow morning :)

OK, is there always going to be a valid array index for whatever the value of that double is?

Convert.ToInt32 works very similarly to Math.Round (http://msdn.microsoft.com/en-us/library/ffdk7eyz.aspx) so I suspect that won't fix the issue.

I presume you're writing to the array and setting a value based on the magnetic sensor?
Sounds to me like you might want a different data structure.

If you use an array then you'll need to ensure that it's appropriately sized to handle all possible values that can be put into it.
This may be a problem if you can have negative values as negative array indexes are not allowed in C#.
Also, will the data always cover all the values in the range? If not you may have to deal with issues to do with the array being sparsely populated.

Sounds like you'd be better off having a object than contains the values from your input and then creating a List of them.

This is an assumption based on knowing nothing about the rest of your code though, so could be wrong!
You'd also likely need to modify the code that exports this to Excel in that case.
 
the excel code is quite happy with being sparsely populated. its not a great way of doing it but i've just made the array massive, and initialised all the values to 0 so i shouldnt run out of space, and all that happens is that there are a hell of a lot of 0s on the excel file

the distance travelled shouldnt go negative, so it shouldnt be trying to write into a negative space of the array

this is what the array looks like:
double myarray [XDistance][lane] = magnetic sensor value;
(obviously this isnt the exact code i'm usng, but it should give you an idea of what my code is doing)

its very possible that theres a bug somewhere in my code, or that i'm not using the right variable from my friends code which sorts out the motors and distance sensors (the guy very helpfully named his variables a, b, c, d, e, f and g, and has almost no comments anywhere :mad:)
 
What is the value of the double you are trying to convert (when it throws the exception), and what do you expect the converted integer value to be?

[edit] Just to back up Haircut's point, an array isn't really a good data structure to use for this. I would suggest using a Dictionary like this

Code:
   // Presuming 'lane' (Z-value?) is a double as well...
   Dictionary<double, double> sensorValues = new Dictionary<double, double>();

And then whenever the Z-value is higher than 0, add an entry to the dictionary:

Code:
  if (zValue > 0D)
  {
      sensorValues.Add(distanceTravelled, zValue);
  }
 
Last edited:
the "lane" is an int, its just the Y co-ordinate. i'm manually changing that for each lane, so i made it an int to start with so that i dont need to convert it.

the magnet sensor values can be any number, positive or negative
 
Back
Top Bottom