Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Can declare file scope variables anywhere?

Author
Message
mr b
18
Years of Service
User Offline
Joined: 28th Aug 2006
Location:
Posted: 22nd Feb 2007 03:00
Hi,
I'm new to DarkBasic but an experienced C programmer.

I have organized my project into "modules" where each module is a different dba source file. At the top of each file, I declare my module variables (equivalent to file scope variables in C). Here's the problem:

If I declare an empty array (e.g. dim myArray() as MY_UDT), when I try to use it at runtime I get an array out of bounds message after I insert and element and then try to access it. But if I simply cut the code and paste it into the main.dba source so that the declaration is at the very top of the entire source dump, it works fine.

Is it not allowed to declare variables in this manner? Or is this a bug?

Thanks,
mr.b
Aaron Miller
18
Years of Service
User Offline
Joined: 25th Feb 2006
Playing: osu!
Posted: 22nd Feb 2007 03:42 Edited at: 22nd Feb 2007 03:42
I believe files in DBP are appended to the end of the main source file. (Probably because functions usually go at the end of the file, so that the main loop is easier to access)

On the off topic, I know C++, and DBP, and I dont think DBP is very suitable for data handling, no matter how much it looks like it is, I much more prefer using C++ for that. (No offense to Lee Bamber, or DarkBASIC Professional)


Cheers,

-db


Enter my forums here.
mr b
18
Years of Service
User Offline
Joined: 28th Aug 2006
Location:
Posted: 22nd Feb 2007 03:52
Yes, the source files are appended to each other to make a big source file before compiling. But I though that variable declarations could go anywhere.

So in my case, I would have:

variables
functions
variables
functions
etc...

This should work because all my variables preceed functions, and they in fact do work except for my array.

Is this a bug? or expected behavior?
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 22nd Feb 2007 22:27 Edited at: 22nd Feb 2007 22:28
An included .DBA file can only contain functions and nothing else, (you are not supposed to have anything outside of the function header and endfunction lines apart from comments).

So, they are in effect 'function sets' and scope rules which apply to them are the same as with functions in your main source file.

As such, variables declared in a function in an include file are only accessible inside that function.

Declare arrays which you want to access in all functions in the main body of your program.

AFAIK, this was one of the things which changed in DBP from DBC, though I'm not a big DBP user so I might be mistaken with this.

TDK_Man

Zergei
19
Years of Service
User Offline
Joined: 9th Feb 2005
Location: Everywhere
Posted: 23rd Feb 2007 03:34
Quote: "An included .DBA file can only contain functions and nothing else"


Not only functions, but also labels. I've got a .dba called "globals&types" and i have a label as to call that section and set the globals and types (thus the name).

Further on my stuff at...

Login to post a reply

Server time is: 2024-11-26 00:29:29
Your offset time is: 2024-11-26 00:29:29