TheSwamp

Code Red => VB(A) => Topic started by: vladimirzm on December 20, 2005, 01:09:15 AM

Title: RND
Post by: vladimirzm on December 20, 2005, 01:09:15 AM
how do i store the the random number (generated through RND) in USERR1

ThisDrawing.SetVariable "USERI1", ((10 * Rnd) + 1) returns error
Title: Re: RND
Post by: Jürg Menzi on December 20, 2005, 02:14:07 AM
'rnd' returns a single (real in Lisp), therefore you've to use the USERRx variables as you mentioned in the text.
Your sample works with a USERIx variable and that is not possible...
Title: Re: RND
Post by: Arizona on December 20, 2005, 06:34:20 AM
In VBA, variables are defined based on the type of data you plan to store such as:

Dim Num as Integer

would be a variable that would only hold integers. So if I tried to store a string value such as "123" I would get an error.
However once I have defined my variable I can now assign a value to it:

Set Num = ((10 * RND)+1)

You can then use that variable that contains a value.
Note: I did not address whether your math function works or not.

What is the purpose of setting this value to UserI1? (is this really a variable? I'm on 2000i...) or are you simply trying to store a value somewhere?
Title: Re: RND
Post by: David Hall on December 20, 2005, 08:51:26 AM
This does what you want, but it cold be laid out better

Code: [Select]
Public Sub TESTRND()
Dim num As Double, e As Double
e = 0.25 ' MUST BE INITIALIZED TO SOMETHING, OR IT REPEATS THE SAME NUMBER
num = (10 * (Rnd(e)) + 1)
ThisDrawing.SetVariable "Userr1", num
End Sub
Title: Re: RND
Post by: vladimirzm on December 20, 2005, 10:01:51 AM
thanks

'rnd' returns a single (real in Lisp), therefore you've to use the USERRx variables as you mentioned in the text.
Your sample works with a USERIx variable and that is not possible...

USERRx or USERIx returns the same thing... error


In VBA, variables are defined based on the type of data you plan to store such as:

Dim Num as Integer

would be a variable that would only hold integers. So if I tried to store a string value such as "123" I would get an error.
However once I have defined my variable I can now assign a value to it:

Set Num = ((10 * RND)+1)

You can then use that variable that contains a value.
Note: I did not address whether your math function works or not.

What is the purpose of setting this value to UserI1? (is this really a variable? I'm on 2000i...) or are you simply trying to store a value somewhere?


Well, actually I'm trying to store from Vlisp. For example:

Code: [Select]
(defun msgbox (title button msg / useri1 r)   
  (setq useri1 (getvar "useri1")) 
  (vla-eval
    (vlax-get-acad-object)
    (strcat
      "ThisDrawing.SetVariable \"USERI1\","
      "MsgBox (\""
      msg
      "\","
      (itoa button)
      ",\""
      title
      "\")"
    )
  )
  (setq r (getvar "useri1"))
  (setvar "useri1" useri1) 
  r
)

This works very well and allow me to show basic interface without DCL... Msgbox returns a value that is stored in USERI1, then i can use this without trouble.

In the same way I supposed
Code: [Select]
ThisDrawing.SetVariable "USERI1", ((10 * Rnd) + 1)will work
Title: Re: RND
Post by: David Hall on December 20, 2005, 10:37:50 AM
only problem I see, useri1 stores an integer, and rnd (in vba at least) generates a real (double)
Title: Re: RND
Post by: Keith™ on December 20, 2005, 10:55:35 AM
If you place the value of your RND call in a variant you should be able to place it in useri1 without incident, or at very least move the data from a variant to an integer variable then into useri1
Title: Re: RND
Post by: Jürg Menzi on December 28, 2005, 07:56:56 AM
ThisDrawing.SetVariable "USERR1", (CDbl ((10 * Rnd) + 1))
should work...
Title: Re: RND
Post by: vladimirzm on January 05, 2006, 05:19:26 AM
Nice!

Another line to the tricks book!
Thank you!
Title: Re: RND
Post by: Kerry on January 05, 2006, 07:15:20 AM
I dont believe you have to cast the calc result, just make sure one of the numeric constants is a real

Code: [Select]
ThisDrawing.SetVariable "USERR1", ((10.0 * Rnd) + 1)or
Code: [Select]
ThisDrawing.SetVariable "USERR1", ((10 * Rnd) + 1.0)