Find the nearest value of an array


,

To avoid reinventing the wheel I would like to know in Delphi if such
function exists :

Let's say I have an array of integer of N values, I have an integer X I
need something like

Function
GetTheIndexOfTheArrayThatHasTheNearestValue(ValueTocompare:integer):integer;

Posted On: Tuesday 16th of October 2012 03:54:49 AM Total Views:  229
View Complete with Replies

RELATED TOPICS OF Delphi Programming PROGRAMMING LANGUAGE




How to find the reasons for Form's unloading/closing

I have not been able to find any way for detecting the reasons for Form's unloading/closing. How can we find out the reason for which form is being unloaded/closed... OnClose event is not helpful here. I have managed to list the following reasons: - User clicks on the close button - Form is told to close from code - User is logging off Windows - Windows is shutting down - User closes End Task's the program from Task Manager TIA Yogi Yang
VIEWS ON THIS POST

211

Posted on:

Wednesday 19th September 2012
View Replies!

How do I find component on palette

Sorry if this is a silly question. But I have Delphi 2007 professional and I want to find the TADOCommand component on the tool palette. It was on the ADO tab in Delphi7 but for some reason I cant find it in D2007. The component shows ok on a project loaded into Delphi2007so it must be there somewhere. What ever happended to configure palette menu
VIEWS ON THIS POST

159

Posted on:

Saturday 22nd September 2012
View Replies!

Rounding to nearest 5 cents

I'm afraid i'm looking for help to round a number to the nearest 5 cents ie 1 & 2 cents back to 0 3 to 7 to 5 and 8 and 9 up to 1 I'm using d5 and xp. I know this has been done to death before but can't seem to get it to work properly. One thing I was looking at was just formating to 2 decimals - get the last char and using a case statement etc to add or change the decimals into the required 5 cents. I don't have to rely on accuracy as they are only using this system to avoid paying out in cents to the nearest 5 - I know!!! This is actually a government department setting the rules and doesn't matter as we no longer have 1 and 2 cent pieces in Australia but they are still used in cheques etc. Any pointers would be appreciated Kerry
VIEWS ON THIS POST

90

Posted on:

Saturday 22nd September 2012
View Replies!

Delphi Job - Where to find programmers

Hi Im quitting my current job for pastures new and need to help my current employer to recruit a delphi capable programmer quite quickly... job would be in the UK, in the Northwest/Yorkshire area, working from home. Any pointers/resources would be appreciated. ta. KevanB
VIEWS ON THIS POST

180

Posted on:

Saturday 22nd September 2012
View Replies!

How to declar String Array and give initial value?

I know that I can declar constant string array and give value with.... const str1='aaa'; str2='bbb'; str3='ccc'; const sStrArray:Array[0..2] of String= ( str1, str2, str3 ); How can I do the same thing with Var declaration var str1:String='aaa'; str2:String='bbb'; str3:String='ccc'; var sStrArray:Array[0..2] of String=( str1, str2, str3); //
VIEWS ON THIS POST

124

Posted on:

Tuesday 25th September 2012
View Replies!

How can I do to remove the Delphi compiler message "Return value of function 'MyFunction' might be undefined" ?

Before to modify my "old" delphi code, I would like to know if there is a different way to remove the Delphi compiler message "Return value of function 'MyFunction' might be undefined". For example, here is some code that generates this warning message : .... type TrvSampleType = (stTwo, stThree); ..... function GetSampleType(AValue: Integer):TrvSampleType; begin if (AValue Mod 2 = 0) then Result := stTwo else if (AValue Mod 3 = 0) then Result := stThree; end; What I should write is something like that : type TrvSampleType = (stNone, stTwo, stThree); ..... function GetSampleType(AValue: Integer):TrvSampleType; begin if (AValue Mod 2 = 0) then Result := stTwo else if (AValue Mod 3 = 0) then Result := stThree else Result := stNone; end; But in fact, I don't want to add a new sample type value like a stNone, so how can I assign a "null" value to the Result var to avoid the warning Delphi message Thank you for your help. Laurent
VIEWS ON THIS POST

288

Posted on:

Tuesday 25th September 2012
View Replies!

delphi 5 and findfirst function

I am still happy with Delphi 5 and Windows XP +SP2 Home edition. But I cannot decode the file attribute in my searchrecord. I am using the FindFirst function with srec as the searchrecord. Now when I inspect the found files I find srec.attr to hold an integer with value 8224. The attributeconstants faReadOnly,faHidden,faSysFile etc does not give me such a great number even if I 'or' them all in an integer. Can you explain how to find out how to decode this attr-field
VIEWS ON THIS POST

321

Posted on:

Wednesday 26th September 2012
View Replies!

Subrange Set property accepts enumerated values outside of subrange

I'm having problems with a field that is a set type. The set type is declared as a set of a subrange of an enumerated type. In my code if I try to add another value from the enumeration to the set, it compiles. My feeling is that because the value is outside of the subrange (and hence the allowable types in the set) it should not compile. Any views or explanations are welcome. D type TCTReason = (srStarted, srTimedOut, srTerminated, srSuspended, srResumed); TCTStatusFlag = srStarted .. srTerminated; TCTStatusFlags = set of TCTStatusFlag; .... TCTObject = class private FStatusFlags: TCTStatusFlags; public procedure Update; end; ..... procedure TCTObject.Update; begin // the following line compiles !! It shouldn't because srResumed is outside of the subrange TCTStatusFlag FStatusFlags := FStatusFlags + [srResumed]; end; David Moorhouse Moorhouse Works ltd www.moorhouse.co.nz
VIEWS ON THIS POST

151

Posted on:

Thursday 4th October 2012
View Replies!

could not find program

I am getting a bizarre error in Delphi 5 and Delphi 7 when compiling and running a program from the IDE: Could not find program, (path to program) I have many Delphi projects and this error is only affecting one (I hope!). If I remove some source code the program may create the exe correctly, for example I removed the line: Listbox1.items.add('Button Clicked'); and the program exe was created okay as soon as I put back the above code the exe was not created. There are no compilation errors and I have tried with the FastMM4 but this does not help. I do not know where to begin to look to find a resolution to this problem, any help will be greatly appreciated! I am running under Windows XP Home. TIA Barry
VIEWS ON THIS POST

185

Posted on:

Friday 5th October 2012
View Replies!

Find the nearest value of an array

, To avoid reinventing the wheel I would like to know in Delphi if such function exists : Let's say I have an array of integer of N values, I have an integer X I need something like Function GetTheIndexOfTheArrayThatHasTheNearestValue(ValueTocompare:integer):integer;
VIEWS ON THIS POST

300

Posted on:

Friday 5th October 2012
View Replies!

assign string value to a property of a set type

, - I have a string value for example 'ST1' and I have an object property of type set (or enumeration I am not sure) called Sys (type TSys) has those values [pST1, pST2, pST3] is there a way to assign the string value to the set other than using IF..ElseIF - Please also advice me in what cases I can use this typcast syntax : ({string type} as TAnotherClass)
VIEWS ON THIS POST

93

Posted on:

Friday 5th October 2012
View Replies!

Reverse engineering a stream - finding the object definition(s)

I need to reconstruct the object(s) from a stream in a file. I'm told it was written from Delphi and it looks like it too (after a short test). I'm trying to reconstruct the stream file using other data, as input to an application. It appears the original source is lost or otherwise unavailable. I can enter data and see how the data is put in the stream. However, deconstructing the whole file looks like a bigger job than I'd like. Is there a shortcut here, eg something that will ****yse the stream and come out with a data structure Am I missing something Andrew -- Andrew Gabb email: agabb@tpgi.com.au Adelaide, South Australia phone: +61 8 8342-1021 -----
VIEWS ON THIS POST

125

Posted on:

Friday 5th October 2012
View Replies!

AV I can't find.

After leaving ReadDATFile but before returning to mnuUpDateCxClick execution goes off into FastMM4, and bombs with the message 'Access violation at addr 005F84AB in MyApp.exe. Read of addr 00000000.' Madexcept doesn't fire. The AV occurs running in and out of the IDE. Pausing and doing a Find Error on 005F84AB points at the line indicated.
VIEWS ON THIS POST

236

Posted on:

Friday 5th October 2012
View Replies!

How to show debugging values with mouseover?

What setting to I change to show variable values when I mouse over the code during debugging I had this working before, but changed something in the settings and can't remember what.
VIEWS ON THIS POST

153

Posted on:

Saturday 6th October 2012
View Replies!

cxDBLookupComboBox how to get the ListFieldNames value

Squirrel30 wrote: > > I have cxDBLookupComboBox and I want to get the value of the > ListFieldNames how can I do that Please ask in the Developer Express forums; you'll find more experts there. -- Dave Nottage [TeamB]
VIEWS ON THIS POST

112

Posted on:

Sunday 7th October 2012
View Replies!

Rounding up/down to nearest X? John Herbster?

I need to be able to round a number to a specific unit, specifying to round either up or down. Examples: Round to nearest .05, rounding up. 1.24 => 1.25 Round to nearest .05, rounding down. 1.24 => 1.20 Round to nearest .01, rounding up. 1.242 => 1.25 Round to nearest .01, rounding down. 1.242 => 1.24 I've looked at John Herbster's excellent DecimalRounding_JH1.pas unit, but frankly, I'm dumb with this kind of thing and don't understand how to get this kind of functionality although it seems it should be fairly easy with this unit.
VIEWS ON THIS POST

50

Posted on:

Sunday 7th October 2012
View Replies!

faulting application! how to find a reason?

I have created server using Delphi, Indy idHTTP, Threads and EurekaLog. I run it as application under Admin account on Windows2003 One or two times in day it crashes without any error, eureka log do not gives error also and windows event viewer show error below. How to find where is the problem in such situation I have no clue what is going on. Faulting application Server.exe, version 0.0.0.0, faulting module kernel32.dll, version 5.2.3790.4062, fault address 0x0000bee7.
VIEWS ON THIS POST

121

Posted on:

Tuesday 9th October 2012
View Replies!

Read value out of form's DFM at runtime?

Is there a supported way to read a value out of a form's DFM at runtime The property i'm after is put a published property, but only exists because it was added as a "fake" property using: procedure TCustomForm.DefineProperties(Filer: TFiler); begin ... Filer.DefineProperty('TextHeight', ReadTextHeight, WriteTextHeight, not IsControl); ... end; Now i can't use TStream.ReadComponent TStream.ReadComponentRes or the ReadComponentResEx procedure because these return a component, and the property is not available as a property. Which is why i think there must be a way get at the value of a component's "fake" properties by talking to the component. Something like: TextHeight := Form1.GetDFMProperty('TextHeight'); or Caption := Form1.GetDFMProperty('Caption'); In reality i know there is a zero percent chance of a method like that existing. So then i could get the form's DFM text resource out of my executable directly and either parse it myself, or create a TReader and copy all the guts that loads a form component - but it's not a *correct* way to do things. There would be a case where there's a form that does something i don't expect. (like being the form dfm resource being in binary rather than text). So, is there a *nice* way to read a value out of a form's DFM at runtime
VIEWS ON THIS POST

175

Posted on:

Tuesday 16th October 2012
View Replies!

Subrange Set property accepts enumerated values outside of subrange

I'm having problems with a field that is a set type. The set type is declared as a set of a subrange of an enumerated type. In my code if I try to add another value from the enumeration to the set, it compiles. My feeling is that because the value is outside of the subrange (and hence the allowable types in the set) it should not compile. Any views or explanations are welcome. D type TCTReason = (srStarted, srTimedOut, srTerminated, srSuspended, srResumed); TCTStatusFlag = srStarted .. srTerminated; TCTStatusFlags = set of TCTStatusFlag; .... TCTObject = class private FStatusFlags: TCTStatusFlags; public procedure Update; end; ..... procedure TCTObject.Update; begin // the following line compiles !! It shouldn't because srResumed is outside of the subrange TCTStatusFlag FStatusFlags := FStatusFlags + [srResumed]; end; David Moorhouse Moorhouse Works ltd www.moorhouse.co.nz
VIEWS ON THIS POST

178

Posted on:

Tuesday 16th October 2012
View Replies!