Link to home
Start Free TrialLog in
Avatar of APS NZ
APS NZFlag for New Zealand

asked on

String to numeric constant conversion

I have a list of musical notes which I have defined as constants
const
NOTE_A4 = 440
 NOTE_AS4 = 466
 NOTE_B4 = 494
 NOTE_C5 = 523
 NOTE_CS5 = 554
 NOTE_D5 = 587
etc etc

I have converted a ring tone tune to produce a comma separated list of notes in a TMemo.  I want to use Windows.Beep(Note,Duration) to play the notes by reading the numerical value from the constants.

I tried (eg) StrToInt('NOTE_C5') but received the error "invalid integer value".  How do I get the string 'NOTE_C5' to read the frequency value from the constants?
Avatar of Norie
Norie

Aren't the constants already numeric?

Also, if they were strings and you wanted to use StrToInt would you need the '?

Wouldn't it just be StrToInt(NOTE_C5)?
You have string list of notes as comma separated, and numeric constants - you need bunch of ifs...
var
  lst: StringList;
  i: Integer;
  s: String;
begin
  lst := StringList.Create;
  try
    lst.Delimiter := ',';
    lst.StrictDelimiter := True;
    lst.DelimitedText := sNoteList; //as A4,AS4,...
    
    for i:=1 to lst.Count do
    begin
       s := Trim(lst.Strings[i-1]);
       if s='A4' then Windows.Beep(NOTE_A4, 200)
       else if s='AS4' then Windows.Beep(NOTE_AS4, 200)
       else if s='B4' then Windows.Beep(NOTE_B4, 200)
....
    end;
  finally
    lst.Free;
  end;

Open in new window

Avatar of APS NZ

ASKER

Thanks for the replies
@Norie: StrToInt(NOTE_C5) gives the same error.

@Sinisa: That kinda defeats the purpose of the constants because I could just hard code the frequencies into the 'if' statements.

I just want a way to take the string (eg) NOTE_C5 from the TMemo and match it with its constant to read the frequency value.
Ah, right - you have a delimited text string with the notes for the ring tone and you want to match them against the constants.

Have you considered using a dictionary instead of constants?
The only answer here is: You Can't!
What you're asking is not possible this way. Any constant is translated at compile time to its value, so you can't use a string to get a constant value as it's already a translation to its declared value.

You have to change the way to get the values using functions or, as suggested, stringlists or arrays.

For example you could create a text file that contains all values and compile it as resource in your project, then load it into a stringlist from resource and get the values from it.

In attachment a little sample of what I mean.MyNotes.zip
ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of APS NZ

ASKER

@Ferruccio: I've done a lot of thinking about your idea, and it works, but I may have several ring tone tunes to convert at different times and it would mean a lot of conditional loading of resources at run time.

I think it's a bit easier to set up an array and copy and paste the values from the TMemo into the form. - eg

Melody: array[1..75] of Integer = (NOTE_D5, NOTE_DS5, NOTE_D5,........

then I just need a for loop to play the tune.
Avatar of APS NZ

ASKER

Thanks SteveBay - I think your approach is better because it has more flexibility if I save each tune as a text file and then load the one I want at runtime.
@I've done a lot of thinking about your idea, and it works, but I may have several ring tone tunes to convert at different times and it would mean a lot of conditional loading of resources at run time.

That was a missed input in your question, otherwise I'd have suggested another kind of approach. Anyway glad if you got your answer.