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 / User defined type and functions

Author
Message
mindsclay
14
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 6th Jul 2013 19:24 Edited at: 6th Jul 2013 19:26
This one has me baffled. The attached code produces an error \"Compilation Failed. Both user defined type values must use the same type at line 13.\" (on this line LocDir= DIRECTION())
I can get rid of the error by using

LocDir AS fromDirection = DIRECTION()

but then the function doesn\'t execute.

Basically, I want to assign values within a function then pass them on. I would use an array but there are strings and booleans, and I thought defining my own type would be easier than converting all to strings.


.

www.mindsclay.com
lucifermagus.mindsclay.com (not working with Firefox)
JackDawson
14
Years of Service
User Offline
Joined: 12th Jul 2011
Location:
Posted: 6th Jul 2013 20:20



These two lines seem to be a problem so I fixed it here.




https://soundcloud.com/toraktu
mindsclay
14
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 6th Jul 2013 21:01 Edited at: 6th Jul 2013 21:03
How does that fix anything? The error still comes up!

www.mindsclay.com
lucifermagus.mindsclay.com (not working with Firefox)
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Jul 2013 00:15
i dont think you can return a udt directly from the function

Shazam!
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 7th Jul 2013 00:35
Ortu is correct, you cannot return UDTs from a function, unfortunately. There is this "hack" I've used before where you define a global return variable, and use that to get the return value:



TheComet

mindsclay
14
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 7th Jul 2013 01:07 Edited at: 7th Jul 2013 01:22
Yes. I already went with the global method. But I am using an array called return$().

After I tried defining a type and the compiler told me the field defined in it didn't exist, I decided to give up on using UDTs at all.

I have wasted at least 5 hours dealing with UDTs and #CONSTANT. I can only get them to work if they are defined in the same file as they are being used. Using #CONSTANT keeps the compiler from running, which I didn't know about (thus the hours).

Maybe I'll try your way again later.

www.mindsclay.com
lucifermagus.mindsclay.com (not working with Firefox)
Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 7th Jul 2013 02:04
I love UDT's, and I think they're great. Although they're not perfect, they do stop a lot of silly mistakes happening in your code, most commonly the simple typo.

Take the below code as an example. Not using a UDT would leave you with working yet dysfunctional program that compiles without error, all due to a silly typo. Alternatively, if you were to use a UDT, on compile you'd be flagged an error for review, and you'd fix it in two seconds with your code & results would be as expected.



The easiest way you could gain an understanding of UDTs is to go over one of your old programs that doesn't use them, and re-write it to use them. Start with the simple things, like mouse position.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 7th Jul 2013 10:31 Edited at: 7th Jul 2013 10:34
Quote: " I can only get them to work if they are defined in the same file as they are being used."


do you mean that you can only get a variable created as a type when the variable is declared in the same file as the type was defined?

Or can you only access/work with a typed variable in the same file as the type?

without more info on how and where you are setting up the types and typed variables, it sounds like a scoping issue (global vs local) of the variables.

UDTs cannot be declared inside of a function, it will throw a 'declaration not valid' error if you try, but the UDT declaration can appear anywhere else in any included file (using #include or via the IDE). Code execution doesn't even have to pass over it, and it will still be available to set to a variable at any time.

That being said, most include files are often just a collection of functions, and so any variables declared to be of the UDT in any of those functions will be local to the function unless declared to be global, and so would only be available in that file, and more specifically, that function.

now the help files on #include are misleading:

Quote: "Be aware that included source code is
appended to the end of the program, which means such declarations as TYPE and FUNCTION names included are only valid for use within the included file. If you want to use your TYPE globally, ensure it is declared in your main program.
"


it lies.

create the file "test.dba" as so:



now create a new project as so:



you can see that the main file is perfectly capable of calling the function from the included file and is able to set variables as the type declared in the included file (but watch your scoping).

Shazam!

Login to post a reply

Server time is: 2026-07-07 00:32:19
Your offset time is: 2026-07-07 00:32:19