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.

DarkBASIC Professional Discussion / Set variable to "null"

Author
Message
Hayer
20
Years of Service
User Offline
Joined: 4th Nov 2005
Location: Norway
Posted: 14th Jun 2010 23:27
Hi!

In C++(and many others language i've been using) i can do

selectedBuilding = null

and then later i can do

if selectedBuilding <> null
` Some code here
endif


How can i do this in DBPro?
Tried to do this first:

global dummyBuilding as Building

and then just set

selectedBuilding = dummyBuilding

if selectedBuilding <> dummyBuilding
`Some code here
endif

Put that gives me some error about pType==NULL ?

Keep it simple.
Questions? Mail me
Rudolpho
20
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 15th Jun 2010 00:02
Is Building a defined datatype?
Also I'm not quite sure that this is allowed:

although I think it should be.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Jun 2010 00:49
@Hayer,
You assume that DBPro has pointers like C++ has - it doesn't, and therefore there is no such thing as 'null' in DBPro (with one very small exception that won't help you).

If you want to represent 'not set' in some way, you'll need to represent it another way, such as adding an in-use flag within your type.

luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 15th Jun 2010 04:45
My workaround is to have string variable container of attributes for my object.Because my container is a string i can put "0 10 -10 null 2000", basicly anything.When i need to do something related to my object attribute i grab the string parse it to words put each in array, then check the array elements if it`s <> null i assign the value, i use this method for items in rpg i`m making now.

This is the original thread where you can see some example, look at my first code snippet: http://forum.thegamecreators.com/?m=forum_view&t=171131&b=1

Where there is a will, there is a way.
I often edit my posts, that`s who i am
jfroco
16
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 16th Jun 2010 08:08 Edited at: 16th Jun 2010 08:09
Hi Hayer,

I just tested "if myVar" and it worked OK in my test: if myVar was declared but no value was asigned it returned false, if I assign a value to one of myVar's attributes it returned true.

My test:



Hope this helps.

Best regards

JF
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 16th Jun 2010 08:40 Edited at: 16th Jun 2010 08:44
jfroco If you don`t assign value to your variable it`s true that it returns false, but in the same time if you check the variable it says it`s 0.If it`s 0 then it`s not NULL, it must have no value to be NULL, but there can`t be declared real variable with no value in dbpro.You can`t use that way because there could be variable which must be 0 and not Null.If you do this:



In your code it still shows that variable is Null which is not true because you give it value of 0.



Check this!
Cheers!

Where there is a will, there is a way.
I often edit my posts, that`s who i am
jfroco
16
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 16th Jun 2010 08:55
Hello luskos,

Please remember this is a workaround, but anyway:

You cannot do this in ANY language: "if var is null then print var.x" it will raise a null value exception so your code is wrong: You cannot access the attributes if the object is null.

Also as far as I know, primitive data types generally cannot have null values (except for strings), so it is probably wrong to think you can assign a null value to a real/float variable in any OOP language.

Best regards

JF
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 16th Jun 2010 11:20
NULL value assigned to variable is to do not bother to access whatever by checking if it`s NULL or not and skipping folowing operations with this variable.That`s exactly what i use it for.You can check the code i provide and the link from my thread.I use NULL in a string which i parse and check every value if it`s NULL i skip operation, if it`s anything else i assign the value to coresponding variable and do the calculation.

However it can be irrelevant in most cases if you use 0 and not NULL.

About this is possible or not your are wrong!

Obviously this can be done in some languages, that`s why Hayer is asking how it can be achieved in dbpro!Read IanM`s post and you can see how this is done in C++.

Anyway you are half right about strings.They also aren`t NULL but just empty.That`s what i think the strings are, but i may also be half right about this.

And again i make some tests using your piece of code and for some reason changing "y" did not changed the myVar state only "x".I swap them and then "y" changed the state and "x" not.I think that Hayer can use this because he can use the first variable defined within the UDT as flag.Just the way IanM suggested.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
jfroco
16
Years of Service
User Offline
Joined: 1st Dec 2009
Location: Chile
Posted: 16th Jun 2010 11:37
Hi,

I dont want to argue with you because I don't see the point of critizing a workaround I proposed, but you are actually doing the opposite your are describing:

You say: "NULL value assigned to variable is to do not bother to access whatever by checking if it`s NULL or not and skipping folowing operations with this variable.That`s exactly what i use it for"

In your code you do the opposite:



Again the opposite:


And then you say I'm wrong: I don't understand why you that. I just said: 1) if the language supports null you will raise an exception when trying to access the attribute of an object that is null 2) vars declared as primite data types cannot be null in most languages

Of course I'm talking about a language that supports null, it is not the case of DBPro. For DBPro I provided a simple and elegant workaround, and I dont see your point of critizing it violating rules 1 and 2.

I hope Hayer will find this workaround useful.

On the other hand discussing with you is not of my interest so I'm disabling mailback.

Best regards

JF

Login to post a reply

Server time is: 2026-07-25 16:21:17
Your offset time is: 2026-07-25 16:21:17