Variant Data Type variables -Can't print individually 
Author Message
 Variant Data Type variables -Can't print individually

Hi, I've got this big problem and I really need help.
I'm writing a program that attempts to print the a variant variable value
individually. Variable num must be of type Variant and always holds a 9
digit number.

For example  num = 123456789 as variant
I need to disply it like this :
       1
       2
       3
       4
       5...


Option Explicit
Dim num As Variant

Private Sub frmPrint_Click()
Dim obj As Variant
Dim lenNum As Integer
Dim i As Integer
Dim strNumArr(1 To 9) As Variant
num = 123456789

    For i = 1 To 9    ' An attempt to place the first number in variable num
as the first element of array strNumarr
      strNumArr(i) = num(1) ' this doesn't work
    Next i

   For Each obj In strNumArr
     Picture1.Print obj
   Next

End Sub

Thanks for your help.

md.



Thu, 04 Dec 2003 20:11:02 GMT  
 Variant Data Type variables -Can't print individually

Quote:

> Hi, I've got this big problem and I really need help.
> I'm writing a program that attempts to print the a variant variable value
> individually. Variable num must be of type Variant and always holds a 9
> digit number.

> For example  num = 123456789 as variant
> I need to disply it like this :
>        1
>        2
>        3
>        4
>        5...


> Option Explicit
> Dim num As Variant

> Private Sub frmPrint_Click()
> Dim obj As Variant
> Dim lenNum As Integer
> Dim i As Integer
> Dim strNumArr(1 To 9) As Variant
> num = 123456789

>     For i = 1 To 9    ' An attempt to place the first number in variable num
> as the first element of array strNumarr
>       strNumArr(i) = num(1) ' this doesn't work
>     Next i

>    For Each obj In strNumArr
>      Picture1.Print obj
>    Next

> End Sub

> Thanks for your help.

> md.

Hi.

1. Trivial method: convert your number to a string (CSTR) then print
each character at a time (MID).

2. If you meant that no conversion to a string must be done, then
extract your digits using integer and modulo division of your number by
the powers of 10. Since 9 digits are required, use Long type to hold
your intermediate calculations. Now, each digit is the weight of 10 to
the power of the digit's position (10^dp) - starting from position zero
for LSD up to eight for MSD. Start counting from 8 to 0, for each step
divide the "current number" by 10^counter and take the integer of this
division (the quotient). This is the required digit at place "count".
Next, have the remainder (using MOD) of the division, decrease your
counter and repeat this process:

lngCurrentNum = num
For Counter = 8 to 0 step -1
   lngPower = 10 ^ Counter
   strNumArr(Counter+1) = Int(lngCurrentNum / lngPower) ' Quotient
   lngRemainder = num Mod lngPower ' Remainder
   lngCurrentNum = lngRemainder ' Update "current number"
Next Counter

Now you should have your digits in strNumArr (BTW, str prefix is used
for strings, better change to something else).

HTH,
Yaniv.



Thu, 04 Dec 2003 20:33:12 GMT  
 Variant Data Type variables -Can't print individually
I'm not sure why you want to use a Variant when there are other more
suitable data types but, as they say, "Ours is not to reason why. Ours is
but to do or die". (I can't remember who said that now!). If you simply want
to print out the individual digits of the number then you could try:

Dim num As Variant
Dim s1 As String, n As Long
num = 123456789
s1 = CStr(num)
For n = 1 To Len(s1)
Picture1.Print Mid(s1, n, 1)
Next n

Mike


Quote:
> Hi, I've got this big problem and I really need help.
> I'm writing a program that attempts to print the a variant variable value
> individually. Variable num must be of type Variant and always holds a 9
> digit number.

> For example  num = 123456789 as variant
> I need to disply it like this :
>        1
>        2
>        3
>        4
>        5...


> Option Explicit
> Dim num As Variant

> Private Sub frmPrint_Click()
> Dim obj As Variant
> Dim lenNum As Integer
> Dim i As Integer
> Dim strNumArr(1 To 9) As Variant
> num = 123456789

>     For i = 1 To 9    ' An attempt to place the first number in variable
num
> as the first element of array strNumarr
>       strNumArr(i) = num(1) ' this doesn't work
>     Next i

>    For Each obj In strNumArr
>      Picture1.Print obj
>    Next

> End Sub

> Thanks for your help.

> md.



Thu, 04 Dec 2003 21:01:40 GMT  
 Variant Data Type variables -Can't print individually
Hi Mike,

I though my memory was bad!!

Quote:

> I'm not sure why you want to use a Variant when there are other more
> suitable data types but, as they say, "Ours is not to reason why. Ours is
> but to do or die". (I can't remember who said that now!).

Alfred, Lord Tennyson, "The Charge of the Light Brigade", Verse 2:

     "Forward, the Light Brigade!"
     Was there a man dismayed?
      Not though the soldier knew
      Someone had blundered.
      Theirs not to make reply,
      Theirs not to reason why,
      Theirs but to do and die.
      Into the valley of Death
      Rode the six hundred.

Best regards,

John...........



Thu, 04 Dec 2003 22:38:28 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Q: variant data type with user defined data type

2. 'Canned' data in VB program

3. How to read Excel cells data types without Variant types

4. Variables typed as Variant/whatever

5. vbScript variant type variables

6. vbScript variant type variables

7. problem of converting C's union type to VB's a data type

8. problem of converting C's union type to VB's a data type

9. GetRows() Method and Variant data type

10. Variant data type

11. RDO and Variant data types

12. Cannot assign a variable declared as a string to a variable declared as data type

 

 
Powered by phpBB® Forum Software