Solved

How do I cast or convert an Integer to a Word ?

Posted on 2000-03-22
13
212 Views
Last Modified: 2012-05-04
Consider the following procedure:

procedure TSampleCalendar.PrevYear;
var
tyear, tmonth, tday : word;
begin
  tyear := word(Year);
  tmonth := word(Month);
  tday := word(Day);
  DecodeDate(IncMonth(CalendarDate, -12), tyear,tmonth,tday);
end;

Is there some way I can perform the cast INSIDE the DecodeDate function call?

It would look something like this:

procedure TSampleCalendar.PrevYear;
begin
  DecodeDate(IncMonth(CalendarDate, -12), word(Year), wor(Month), word(Day));
end;

Now, the attempt to cast while inside the DecodeDate function call [in my "modified" procedure] doesn't compile.  I get some error about "Constant object cannot be passed as var parameter".  Can I perform the cast as I am attempting in some other fashion?  I hate to use up three variables just as place holders like I am doing in the first procedure.

Thanks,

Tom
0
Comment
Question by:knowlton
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 10 total points
ID: 2646531
You will need to pass in words as the arguments are var parameters.

You can do casts in Delphi like this:

wordVar := word(intVar);

Cheers,

Raymond.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2646538
Cute. I read the Q, answer it, and then when the Q refreshes with my answer the Q has changed (a lot!)

Anyway, the answer still holds...

Cheers,

Raymond.
0
 
LVL 4

Expert Comment

by:DMN
ID: 2646564
INTEGER variable is 32bit word while WORD is 16bit. So the only way to do what you want is:

  year:=0;month:=0;day:=0;
  DecodeDate(IncMonth(CalendarDate, -1), word((@year)^), word((@month)^), word((@day)^));

Set all variables to 0 just to be sure what it's high words is 0.

Alexander.
Please excuse my english.

0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2646631
Raymond, you are right about the change. There should be a 'Edited text of question' message somewhere.
0
 
LVL 5

Author Comment

by:knowlton
ID: 2649161
Raymond:
I figured out how to do the cast, that is why I changed the question.  When I changed the question, nobody had posted anything (atleast it appeared that way)so I thought I was safe.  I guess we were both in the middle of refreshing.  It was only like 10 minutes between when I posted the question and went back and changed it to its present form.  Wow, you guys move fast!

DMN:  
What the H_LL are you doing?  Your solution looks really bizarre to me.  Somebody explain to this dummy what DMN is doing!!!

Raymond:  
I've worked with you before and I trust your answers.  What do you think of DMN's reply?
0
 
LVL 4

Expert Comment

by:DMN
ID: 2649523
:) But it works, yes?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:DMN
ID: 2649564
word(integer) can be only be passed BY VALUE. DecodeDate funtion wants parameters to be VAR. VAR parameter passed to function by reference (by its address). Address of Integer variable is equal to address of it's Low Word. So:

   @integer - address of integer
   (@integer)^ - dereferenced address of integer, something like untyped variable. You can cast it to any other type...

    word((@integer)^) - low word of your integer. And it can be passed as VAR parameter...

Please excuse my english :)
DMN.
0
 

Expert Comment

by:SuperSy
ID: 2661292
To Raymond;

With all due respect, your answer does not hold anymore if we need three variables to do this, according to the present wording of the question, right?

I am curious what the old question was, though.  


To knowlton;

I don't intend to answer the question (unless rwilson does not want to), but I just want to know which variable is giving you the "Constant object ..." error message: Year, Month, or Day.  (I hope it is not the IncMonth()).  
0
 
LVL 5

Author Comment

by:knowlton
ID: 2661618
In all fairness, the points belong to rwilson.  He answered the question as originally posted, not as I changed it a few minutes later.

DMN:

I you want I can transfer 10 points to your account as well, since you answered the question as I modified it.

SuperSy:  I think it was all three variables giving the message.

Tom

0
 
LVL 5

Author Comment

by:knowlton
ID: 2661627
SuperSy:

The original question was simply "how do I cast an integer to a word?"

I figured out how to do this a few minutes after I posted, so I modified the question to "how do I cast [in-place] during a function call that is expecting words variables when I am passing in integers?"

I had no idea that rwilson had already read and attempted to answer my original question so fast.  When I went back and modifed my question the second time, it appeared that NOONE had posted anything yet.
0
 

Expert Comment

by:SuperSy
ID: 2661878
I don't think you did anything "bad" here --- as you said, it was just bad timing, and since Raymond got the points (which to me seems rightfully his), I think everything turned out alright.  

By the way, I have a feeling that the function/procedure that contains the DecodeDate is receiving the Year, Month and Day as const parameters.  If so, you can remove the "const"s from the declaration to avoid that error message; otherwise, I do not see why these three variables are seen as constant variables.  

Ignore me if I am wrong.  Well, ignore me even if I am right.  Just a comment from a guy bored at work.  ^_^
0
 
LVL 5

Author Comment

by:knowlton
ID: 2661901
Okay.

This entire question of mine arose because I was attempting to write my first Delphi component.  No big deal.

Tom
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2661919
SuperSpy: DecodeDate has the parameters year, month, day as var paramters - thats where it puts the returned year, month and day values.

Cheers,

Raymond.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi OLE Error 8 93
Magic Software info 18 124
Working with hours 3 46
How to define IfThen functions in one common unit? 4 44
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now