I'm currently working on some UI controls in Silverlight. Now I need to figure out some mathematical equation in order to figure out at what position an item should be inserted. Basically, the item to be inserted is inside a Grid at some odd row (1, 3, 5... you known, odd numbers). Once I click on the item, it should be taken out from the Grid and inserted into an ObservableCollection<T>. The relationship between the collection and the grid is that for every item in the collection, two rows in the Grid are created, so basically, the item at index 0 of the collection is also at row 0 of the Grid, but item 1 of the collection is at row 2 of the Grid and so on... When the user hovers over the odd row, a semi-transparent item appears and when the user clicks on the item, it should be inserted into the collection at the relative position.

I've been trying to find a pattern that I can use, but I just couldn't. It gets harder as more items are added, but I'm sure there has to be some equation to figure it out...
i don't know if i got what you mean.
so you have items in rows 1, 3 , 5 , 7 .... of the grid
and rows 0/1 , 2/3, 4/5, .... corresponds to index 0, 1, 2, ... of the collection
so when you click on 1 if should go to 0 , on 3 to 1, 5 to 2, ??
pseudo-code:

int row_i // row of item to be inserted at grid
int row_collection_i1 // 1st row of collection item
int row_collection_i2 // 2nd row of collection item

if (row_i%2)
row_collection_i1 = row_i - 1;
row_collection_i2 = row_i;
end
GN90 wrotei don't know if i got what you mean.
so you have items in rows 1, 3 , 5 , 7 .... of the grid
and rows 0/1 , 2/3, 4/5, .... corresponds to index 0, 1, 2, ... of the collection
so when you click on 1 if should go to 0 , on 3 to 1, 5 to 2, ??
Not exactly. Both are 0 based (i.e: start at 0). When you click on row 1, it should go to index 1 (because it's between rows 0 and 2 which already have items). When you click on 3 it should go to index 2...

5 ===> 3
7 ===> 4
9 ===> 5

... and so on.
collectionIndex = rowIndex/2 + 1
results will be :
rowIndex ===> collectionIndex
1 ===> 1
3 ===> 2
5 ===> 3
7 ===> 4
9 ===> 5
11 ===> 6
13 ===> 7
15 ===> 8
......
Kassem wrote
GN90 wrotei don't know if i got what you mean.
so you have items in rows 1, 3 , 5 , 7 .... of the grid
and rows 0/1 , 2/3, 4/5, .... corresponds to index 0, 1, 2, ... of the collection
so when you click on 1 if should go to 0 , on 3 to 1, 5 to 2, ??
Not exactly. Both are 0 based (i.e: start at 0). When you click on row 1, it should go to index 1 (because it's between rows 0 and 2 which already have items). When you click on 3 it should go to index 2...

5 ===> 3
7 ===> 4
9 ===> 5

... and so on.
Ohhh, ok, then GN90's solution is the correct one.
Man, i want to see you working on some encryption / decryption algorithms. That would be fun.

I seriously wonder how this combination is working for you (A very VERY (Yes VERY) skilled programmer and learner, yet a person who failed the math 101 test)...

Kassem, do you remember the 1000 vs 500 math problem (few months ago) :P
GN90 wrote
collectionIndex = rowIndex/2 + 1
results will be :
rowIndex ===> collectionIndex
1 ===> 1
3 ===> 2
5 ===> 3
7 ===> 4
9 ===> 5
11 ===> 6
13 ===> 7
15 ===> 8
......
mesa177 wroteOhhh, ok, then GN90's solution is the correct one.
Actually, I think it is:
collectionIndex = (rowIndex - 1) / 2 + 1
But I could still be wrong... I'll have to fix a few bugs first and then test it thoroughly to make sure whether it's the correct equation or not.
Georges wroteMan, i want to see you working on some encryption / decryption algorithms. That would be fun.
More fun than you think. I've been through this in my Data Security course. That was the ONLY IT course in which I needed help - I was completely lost! The other two courses in which I needed help during my 3 years of uni were Math 2 and Statistics 2 :)
Georges wroteI seriously wonder how this combination is working for you (A very VERY (Yes VERY) skilled programmer and learner, yet a person who failed the math 101 test)...
Actually, I think that's because I rarely do any UI programming. But currently, I'm working on some drawing + animation framework for Silverlight (outside my work-hours though). I expect my brain to freeze frequently over the next few weeks! :P

But the thing is, when you're working on enterprise applications where it's more about problem solving (technical and business problems), you rarely need to use any math. I just love designing solutions that involve some fancy architecture (sometimes I fall in the pitfall of over-architecture though). I cannot claim to be a software architect yet but I'm doing my best to be skilled enough to become one. I also found out that I prefer framework/library development over application development. Or at least, I prefer working on laying out the infrastructure of an application so everything would be easily built on top of it.
Georges wroteKassem, do you remember the 1000 vs 500 math problem (few months ago) :P
Have I mentioned that I have a terrible memory? :P But I think it has something to do with some question I've asked you on MSN?
Kassem wroteActually, I think it is:
collectionIndex = (rowIndex - 1) / 2 + 1
But I could still be wrong... I'll have to fix a few bugs first and then test it thoroughly to make sure whether it's the correct equation or not.
If both rowIndex and collectionIndex are of type int, then the decimal part from the division is lost. So GN90's equation is true. On the other hand, your equation is also true. So go with either equations.
mesa177 wroteIf both rowIndex and collectionIndex are of type int, then the decimal part from the division is lost. So GN90's equation is true. On the other hand, your equation is also true. So go with either equations.
Thanks for the explanation, makes sense :)