1

Can anyone please tell me if this can be done, and if yes, how?

I have a List<float> FocalLengthList that I have populated with some values. Then THIS List is stored in a List<List<float>> MainFocalLenghtList. However, in my application I need to use the values fromMainFocalLenghtList to update an objects 3D position. So I need to cast fromMainFocalLenghtList [0] to int.

Can this be done and how?

Here is a small section of my code to explain. Adding values to FocalLengthList then adding that list to List<List<float>> MainFocalLenghtList

float newFocalLength = focalLength * pixelSize; 
FocalLengthList.Add(newFocalLength); 
MainFocallengthList.Add(FocalLengthList); 
FocalLengthList = new List<float>(); 

Then how I intend to use the values (not working)

int zComponent = MainFocallengthList[0];
Robaticus
  • 22,857
  • 5
  • 54
  • 63
heyred
  • 2,031
  • 8
  • 44
  • 89
  • Are you trying to cast a single float to a single int or the whole list to a list of ints? It's not clear from your question. – Matt Burland Apr 09 '12 at 20:34
  • I was about to ask the same thing.. – MilkyWayJoe Apr 09 '12 at 20:34
  • 2
    MainFocallenthList is a list of lists. What would you expect the result to be if you cast a list of floats to an int? – Robaticus Apr 09 '12 at 20:35
  • 1
    If you are looking for, for example, the first item from the first list in MainFocallengthList, you should be able to index it as: MainFocallengthList[0][0]. For the nth item in the mth list, you'd use MainFocallengthList[m][n]. – Matt Burland Apr 09 '12 at 20:35
  • What valid conversion is there (or could there be) between `List` and `int`? – Kendall Frey Apr 09 '12 at 20:35
  • 2
    May want to review the answers in this question: http://stackoverflow.com/questions/14/whats-the-difference-between-math-floor-and-math-truncate-in-net - there's a loss of precision going from float to int. – Tieson T. Apr 09 '12 at 20:41

5 Answers5

3

You can certainly cast a float to an int, as long as you do so explicitly (since it may involve a loss of precision).

The problem with the code you've posted is that you're indexing into a list of other lists. The value returned by MainFocallengthList[0] will itself be a List<float>. You must then index into that list to get a value you can actually cast to int.

Assuming both the target list and the target float in that list are at the first index of their respective containers:

int zComponent = (int)MainFocalLengthList[0][0];

That first index returns the FocalLengthList that you added to MainFocalLengthList. The second index returns the newFocalLength value that you added to FocalLengthList. Clear? :)

Dan J
  • 16,319
  • 7
  • 50
  • 82
1

I'd probably do it like this:

int zComponent = (int)Math.Ceiling(MainFocallengthList[m][n]);

Though you'll want to substitute actual values for the nth item in the mth FocalLengthList.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
1

Give this a shot:

var floatList = new List<float>();

var intList = floatList.Select(f => (int)Math.Ceiling(f)).ToList();
James Johnson
  • 45,496
  • 8
  • 73
  • 110
1

Since MainFocalLengthList is a List of List<float>

var intarr = Array.ConvertAll(MainFocalLengthList[0].ToArray(), f=>(int)f);
L.B
  • 114,136
  • 19
  • 178
  • 224
0

You can do it this way, but you need the indexes of both the inner and outer lists:

// The first [0] indicates the index of the nested list within MainFocallengthList
// The second [0] indicates the index of the item that you want in the nested list
int zComponent = (int)(MainFocallengthList[0][0])
Chris Shain
  • 50,833
  • 6
  • 93
  • 125