Results 1 to 15 of 15
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how do u define a variable to store decimal values (W97 - sr2)

    Hi all

    I want a variable to store decimal values eg
    2.73
    -0.04

    however when I define the variable as long or as integer & I set the variable
    the values are not stored.
    the variable result is either
    the next rounded number eg 2.73 to 3
    or just contains 1.

    can someone please let me know how I should define my variable?
    many thanks Diana

    can

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Brisbane, Australia
    Posts
    245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    Hi Diana

    Do they really need to be stored as long or integer values?

    What about a simple string? Are you using the values in calculations that absolutely require numbers?

    Leigh

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    HI Leigh

    the variable can be defined as anything.
    I'm not using the values for calculations.
    The values are used to format & align objects in word.

    Diana

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: how do u define a variable to store decimal values (W97 - sr2)

    You can't store decimals in a long or integer because by definition those datatypes hold whole numbers. If you want to store decimals, use a single or a double or even a currency type.
    Charlotte

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    Hi Diana,

    Charlotte's advice is on the money.
    Specifically, for things like indents and alignments in Word, you want to use the Single data type.

    Gary

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    I must admit I often use the Variant data type in such cases (that is, I simply use "Dim myIndent" without any type).
    Then I let Word worry about the type cast, if any is necessary.
    <pre>Dim myIndent
    myIndent = 8.5
    ...
    Selection.ParagraphFormat.LeftIndent = myIndent</pre>

    Some may call this sloppy programming, but I'm lazy...

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  7. #7
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    An example where the type of the parameter is hard to predict:

    Selection.ParagraphFormat.LeftIndent
    (in pt) is defined as "Single",
    Selection.ParagraphFormat.Borders.DistanceFromLeft
    (in pt) is defined as "Long".

    <img src=/S/confused.gif border=0 alt=confused width=15 height=20>Klaus

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: how do u define a variable to store decimal values (W97 - sr2)

    Not necessarily sloppy, but definitely inefficient. The variant datatype takes the most resources without returning anything but flexibility. Since you have a pretty good idea of the datatype you need for things like indents, it makes sense to use the appropriate datatype in your code.
    Charlotte

  9. #9
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: how do u define a variable to store decimal values (W97 - sr2)

    Most all of the numeric properties in Word, especially the numeric properties of the ParagraphFormat object are single, so you should define them as single, for example:
    <pre>Option Explicit
    Sub Macro1()
    Dim sngIndent As Single
    sngIndent = Selection.ParagraphFormat.RightIndent
    Selection.ParagraphFormat.LeftIndent = 1.5 * sngIndent
    End Sub</pre>

    When in doubt, the help file for a property usually tells you the data type. HTH --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    Not only inefficient, ir exposes you to the "evil of type coercion" problem as VB/VBA has a mind of its own and may convert types at inappropriate times.

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    I'd make a different point about efficiency, and that has to do with reuse and sharing of your code. It's much easier to reuse and share code that uses a variable naming convention, and using a variable naming convention goes hand in hand with declaring types. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Some of the code posted here would be much easier to following if the variable data type were more obvious from the name.

    As far as internal efficiency and Howard's comments about the dangers lurking behind type coercion, it seems we're not going to have much choice in the matter, once VB.Net makes its way over to Office. If I understand it right, variable typing there works like it does in VB Script - variable type declaration isn't supported and everything is a subtype of Variant, the subtyping presumably done by the kind of coercion Howard's referring to.

    Seems sort of the worst of both worlds since the variable isn't explicitly typed, but you'd still have to determine the variant subtype and code accordingly (as if it were typed), in many cases (I'd guess....)

    Maybe someone here who's worked with VB Script could share some views on the pros and cons of untyped variables?

    Gary

  12. #12
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    Hi Charlotte,

    <hr>Since you have a pretty good idea of the datatype you need for things like indents, it makes sense to use the appropriate datatype in your code.<hr>
    As my example showed, having a pretty good idea isn't enough, you have to look up the type of every parameter in the documentation.
    If you'd define .ParagraphFormat.Borders.DistanceFromLeft as Single, you might be in for a surprise when you use a non-integer value like 8.5 <img src=/S/beep.gif border=0 alt=beep width=15 height=15> <img src=/S/drop.gif border=0 alt=drop width=23 height=23>

    I knew I would be in for a bashing with this post, so I want to emphasize I do use type declarations for all objects in the Word Object Model, and for most other variables like strings, integers ...

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  13. #13
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: how do u define a variable to store decimal values (W97 - sr2)

    I haven't worked with .Net, but my understanding was that it required explicit typing and didn't allow variants at all. <img src=/S/confused.gif border=0 alt=confused width=15 height=20>
    Charlotte

  14. #14
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    VB.net does have all of the VB data types, except there is no longer a Variant data type.

    In VB.net, all data types are a special case of the Object data type, but you can still type variables as String, Long, etc.

    In addition, VB.net adds a new statement Option Strict.
    If Option Strict is On, then the code has to do all type conversions explicitly with, say, cast functions. If Option Strict is Off, then I believe the evil of type coercion can occur.

  15. #15
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: how do u define a variable to store decimal values (W97 - sr2)

    Howard,

    Thanks for the info.

    It's unusual that MS have done so much to discourage explicit typing in the past- and have suddenly forced it. Any example from Help from MS product have not typed variables. Here's a <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=vb&Number=2215&page=&view=& sb=&o=&vc=1>memory jog</A> for examples of wehat I mean.

    If MS had been really serious about typing, the help would have been giving examples many years ago. Instead, the help has encouraged people to be loose.

    I suspect with .Net, it's just too hard to allow variant data types- so they just said "it's sloppy programming" and pulled the plug on it.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •