I have list of doubles and a list of objects of a certain type. I`m trying to find if the values in the list of doubles match the objects in and around a precision value, if they do, I want the Index of the object.

Now if the value is not present I want to return a null value. With the code I have now, I always return a value - that`s where the issue is.

//TimeVals is the List of Doubles. Sorted///AObjList is the List of Objects of a certain class//FmZero = 0.0001 This is the precision value for (i = 0; i < TimeVals.Count - 1; i++) { Ind = GetInd(TimeVals[i], AObjList, ref Ind);Console.WriteLine(Ind); } private int? GetInd(double Time, List<Obj> LObj, ref int? Ind) { int Head = 0; int Tail = LObj.Count - 1; if (Time < LObj[(int)Ind].Time) { while ((Ind != Head) && Time < (LObj[(int)Ind].Time - GP.FmZero)) { Ind = Ind - 1; } } else { while ((Ind != Tail) && Time >= (LTrj[(int)Ind].Time - GP.FmZero)) { Ind = Ind + 1; } } return Ind; }

You've denoted Ind to be a reference parameter by your use of the ref keyword. ref expects that its corresponding parameter be initialized prior to passing it to the function. How are you initializing the Ind variable prior to line 7?

Yes, but where would I use that in the code. The way GetInd function is written it always returns a value.
For example where would I set the bool Found? I`m guessing I`ll have to rewrite the function is a different way.

private int? GetTrj(double Time, List<Trajectory> LTrj, int? Ind) { int Head = 0; int Tail = LTrj.Count - 1; bool Found = false; if (Time < LTrj[(int)Ind].Time) { while ((Ind != Head) && Time < (LTrj[(int)Ind].Time - GP.FmZero)) { Ind = Ind - 1; } } else { while ((Ind != Tail) && Time >= (LTrj[(int)Ind].Time - GP.FmZero)) { Ind = Ind + 1; } } return (Found == false ? null : Ind); }

Nullable are useful, specially with databases. If they suit you, go for it.

But a regular int is easier to work with and takes up less ressources, so when it is possible, there is an alternative: return a set value that could not be returned if something was found, and treat this value as a null.

With integers, I often use int.MinValue as "my" null. I usulally do not expect -2147483648 as a value for one of my integers, so it is easy for me to interpret that value as being null.

Structurally, line 22 is fine. Your problem, however, is that you never update the value of Found anywhere in that function. It will always be false. You need to change your logic to update the value of Found.

I have it working now. What do you guys think about this.

int i = 0; int? Ind = 0; int? SInd = 0; //Begin search at this index double APos = 0; int EInd = 0; //Exist Index. Increment only if the the given Time value was found. for (i = 0; i < TimeCnt - 1; i++) { Ind = GetTrj(TimeVals[i], ATrj, SInd); if (Ind != null) { APos = ATrj[(int)Ind].Pos; TableGV[a, i].Value = APos; SInd = Ind; EInd++; } else { Ind = SInd; TableGV[a, i].Value = null; } } private int? GetTrj(double Time, List<Trajectory> LTrj, int? SInd) { int Head = 0; int Tail = LTrj.Count - 1; bool Found = false; int Ind = 0; if (Time < LTrj[(int)SInd].Time) { for (Ind = (int)SInd; Ind > Head; Ind--) { if ((Time <= (LTrj[Ind].Time + GP.FmZero)) && (Time >= (LTrj[Ind].Time - GP.FmZero))) { Found = true; break; } } } else { for (Ind = (int)SInd; Ind < Tail; Ind++) { if ((Time <= (LTrj[Ind].Time + GP.FmZero)) && (Time >= (LTrj[Ind].Time - GP.FmZero))) { Found = true; break; } } } return (Found == false ? null : (int?)Ind); }

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…

Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…

In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.
https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt
https://filedb.experts-exchange.com/incoming/201…