Author Topic: Using Global to Declare Variables  (Read 3167 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Using Global to Declare Variables
« on: October 25, 2007, 10:56:32 AM »

Hi

I am just curious;
myself and my boss have been working on a VBA project and to get particular variables (Double) to work properly throughout the entire project, we used Global.

While Global does work; it doesn't seem to work when lined up in a column of variable.
For example:

If I declare my Double Variables as such:
Code: [Select]
Global PRBASEPT(0 To 2), PRCENTPT(0 To 2), PRCONTPT(0 To 2), PRDRANPT(0 To 2) as Double

The Point value is not passing through

However, if I line them up in a row:
Code: [Select]
Global PRBASEPT(0 To 2) as Double
Global PRCENTPT(0 To 2) as Double
Global PRCONTPT(0 To 2) as Double
Global PRDRANPT(0 To 2) as Double

It works fine ??

Yes, I can go ahead and do it this way and I will however, I would like to know why this is happening and it is much cleaner to line them up vertically in a column.

Any ideas?

Thank you,

Mark


David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Using Global to Declare Variables
« Reply #1 on: October 25, 2007, 10:57:18 AM »
you have to declare each one as double
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Using Global to Declare Variables
« Reply #2 on: October 25, 2007, 10:57:45 AM »
Global PRBASEPT(0 To 2) as Double, PRCENTPT(0 To 2) as Double, PRCONTPT(0 To 2) as Double, PRDRANPT(0 To 2) as Double
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

ML

  • Guest
Re: Using Global to Declare Variables
« Reply #3 on: October 25, 2007, 11:01:34 AM »

Oh I see.
In this case, the word Double needs to spelled out with each variable
That is interesting.

This is not the case with other data types.

Is it because I am using Global? It must be because we have done it the other way before.

Only we used Dim or Public

Thanks  CM

Guest

  • Guest
Re: Using Global to Declare Variables
« Reply #4 on: October 25, 2007, 11:06:16 AM »
GLOBAL is old-school.  PUBLIC has replaced GLOBAL as of VB3 or 4??  I don't know that it really makes a difference though - I haven't found anything that says "You should NOT use Global.... EVER AGAIN! NEVER!!  ever..."

http://forums.devx.com/archive/index.php/t-58727.html

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Using Global to Declare Variables
« Reply #5 on: October 25, 2007, 11:09:41 AM »
Kinda off topic but hey, that's my style of late. Have to ask, do you have to use public / globals? In all my years of vb programming I've had to resort to globals maybe once.

More off topic stuff, if you're anal about the tidiness of your code you can do this --

Code: [Select]
Global _
    PRBASEPT(0 To 2) as Double, _
    PRCENTPT(0 To 2) as Double, _
    PRCONTPT(0 To 2) as Double, _
    PRDRANPT(0 To 2) as Double

This is not the case with other data types.

Not true, VB is unfortunately forgiving you for not specifying the data type and instead declaring them as variants on your behalf. Personally I always thought this behavior was evil.


Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ML

  • Guest
Re: Using Global to Declare Variables
« Reply #6 on: October 25, 2007, 11:18:01 AM »

Hi MP

To be honest, I have never heard of Global prior to this week and we have tried Public, I tried declaring all of the Double Variables in the declarations sections which shouls make them Public but neither method worked.

My boss  did handle the coding on this section and he said that Global was all that worked for him and he is a very good programmer so I totally respect his result.
Of course there may have been another way that we just did not catch but Public DID not work in this case.

As far as Variant; Variant is only assumed when no Data type is assigned. Please correct me if I am wrong.

Variant is also the most memory intensive data type I believe

Mark

havano

  • Guest
Re: Using Global to Declare Variables
« Reply #7 on: October 28, 2007, 08:03:20 PM »
Hi ML,

I made a similar mistake some time ago and was corrected on this forum. I had declared a bunch of variables like you did, separated by a comma, assuming finishing with "as variabletype" would mean declaring all preceding variables on that line as this variable type (Long or Byte or String or whatever). Instead, only the last variable was declared as the desired variable type, the rest was declared as Variant.

Using Variants for Autocad objects input variables which require a specific variable type will result in an error.

ML

  • Guest
Re: Using Global to Declare Variables
« Reply #8 on: October 29, 2007, 09:40:44 AM »

Hey Havano

That's interesting
So, are you telling me that is is the case for all data types?

In other words, I have successfully declared variables (of the same data types) in the past in a linear direction
ie. Dim CurrPath1, CurrPath2, CurrPath3, CurrPath4 as String
And there was no problem.

Prior to last week, I never tried that with The Double Data Type.

So, to test this theory, I would propose that you (we) try something like this

Dim Layer1, Layer2, Layer3, Layer4 as acadlayer

Obviously you will need to do a bit more coding but I do believe that all 4 will be recognized as an acad layer and not as a Variant type. If you are correct, then only the last one will be recognized as an acad layer and the first 3 will error out. Time permitting, let's see what happens

Mark

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Using Global to Declare Variables
« Reply #9 on: October 29, 2007, 10:11:21 AM »
So, are you telling me that is is the case for all data types?

Ummm, beggin' the colonel's pardon, but I already told ya that.

 :-P
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Using Global to Declare Variables
« Reply #10 on: October 29, 2007, 10:26:58 AM »
Tested, and l1-l3 are variants
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Using Global to Declare Variables
« Reply #11 on: October 29, 2007, 10:28:09 AM »
I used really simple test code
Code: [Select]
Public Sub Mark()
Dim l1, l2, l3, l4 As AcadLayer
For Each l4 In ThisDrawing.Layers
Debug.Print l4.Name
Next
End Sub

Hit f8 to step in, and take a screen shot of Locals window
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

havano

  • Guest
Re: Using Global to Declare Variables
« Reply #12 on: October 29, 2007, 10:31:07 AM »
Hi ML,

I don't do Autocad VBA programming lately, I just occasionally visit this forum "for old time's sake" and to prevent my VBA knowledge from vanishing completely.

Please check out this conversation: http://www.theswamp.org/index.php?topic=10222.msg130355#msg130355

In the "code section" I had originally declared: Dim FlippointA(0 To 2), FlippointB(0 To 2) As Double, which resulted in erroneous behavior of the subroutine. Bryco pointed out my mistake, after which I rectified the declaration in the code section.
« Last Edit: October 29, 2007, 10:38:56 AM by havano »

ML

  • Guest
Re: Using Global to Declare Variables
« Reply #13 on: October 29, 2007, 11:11:07 AM »

WOW!

That is interesting!
I learned that method here at my current position and just assumed it worked fine.
I never had an error result from it.

However, I am not sure I even tried it as I suggested.

Let me take CM's bit of code and take a look

Thanks guys!

ML

  • Guest
Re: Using Global to Declare Variables
« Reply #14 on: October 29, 2007, 11:26:40 AM »

WOW, how about that

Using CM's code example, I added a watch to each variable l1 - l4
All came back as type variant, except l4 which came back as an acadlayer
That is very interesting and good to know.

In all cases, I was able to loop through with each layer and still get a list on layers, however only l4 was an obj type acadlayer

That means that even if you get the result you are looking for, you are "still" using more memory then necessary as a type variant; not optimal.

So, in CM's code, it looks like it would have been proper to do
Dim l1 as acadlayer, l2 as acadlayer, l3 as acadlayer, l4 as acadlayer
If you are listing them linerally.

Thanks guys!

Mark