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 / Returning User Defined Types

Author
Message
The One Ring
21
Years of Service
User Offline
Joined: 15th Sep 2002
Location: United States
Posted: 2nd Oct 2002 14:54
Question:

I can't seem to return a User Defined Type from a function. I get the error "#100047: Both user defined type values must use the same type at line xxx".

Example:



This code generates the error on the line where the function is called.

I've also tried dimensioning a one element array. That doesn't create compiler errors, but the values that I assign to the UDT variable are trashed to zero...

Any ideas?
Richard Davey
Retired Moderator
21
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 2nd Oct 2002 15:29
You can't pass a type to a function, which is essentially what's causing this error. Types are however global, so you can access them from within a function.

Also a 1 dimensioned array would work unless you create it within your function because every time you call that function it resets the array. Create the array outside of the function, make it global and then the function can manipulate it at will and it'll retain it's state.

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
The One Ring
21
Years of Service
User Offline
Joined: 15th Sep 2002
Location: United States
Posted: 3rd Oct 2002 01:08
BUMMER! Passing, returning, and using UDTs from functions would be a major plus! Any chance of seeing this ability in future releases?? Please???
Richard Davey
Retired Moderator
21
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 3rd Oct 2002 03:19
It's not my call really but I'll pass the comment on - however you can fully access your types from within any function, so pass in the value of the type to modify and let the function modify it. It wouldn't need to return anything because it would modify the type directly other than maybe a status code.

This is probably not a good explanation! But what I'm saying is that you CAN modify and update types from within functions easily, you can pass references to them into a function as well. The only thing you can't do is return a type out again.

Erk, still not sure I explained well

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
The One Ring
21
Years of Service
User Offline
Joined: 15th Sep 2002
Location: United States
Posted: 3rd Oct 2002 06:13
Oh, you explained it fine. I do totally understand. However, being able to take UDTs as parameters, and sending UDTs back as a result would be very useful.

Cheers!
The One Ring
Richard Davey
Retired Moderator
21
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 3rd Oct 2002 15:32
In what circumstances? Am just wondering how you would use it.

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
The One Ring
21
Years of Service
User Offline
Joined: 15th Sep 2002
Location: United States
Posted: 3rd Oct 2002 15:39
Well, here's an example. Another post on this forum asks about getting an image's width and height. You recommend using bitmap instead (which I agree with).

A function could be written here to automate the process of loading a bitmap and getting it as an image, returning the with and height back from the function. Example:



See what I mean?
Richard Davey
Retired Moderator
21
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 3rd Oct 2002 15:54
Hmmm, I see what you mean but I think that isn't a very correct way to use Types or think about using them. I don't see "size" as being in relation to anything - it's not really an entity within your program. Now if you had made an Image type and size was an extension of that, it would make sense to me - and that way you can pass in the reference to the image and store the results easily. As follows:



(Code un-tested, so probably won't compile but in theory it works just fine)

Can you see the different angle here? I'm not trying to preach and say "this is the right way!" but doesn't it make more sense to treat these things almost like objects? (DBPro isn't object orientated, but with types and extended types you can now simulate a lot of the OO way of handling things).

Anyway I still agree with you - being able to pass a type in and return one from a function is very useful - but I don't feel essential.

Cheers,

Rich

"Gentlemen, we are about to short-circuit the Universe!"
DB Team / Atari ST / DarkForge / Retro Gaming
The Wendigo
21
Years of Service
User Offline
Joined: 13th Sep 2002
Location: A hole near the base of a tree in the US
Posted: 3rd Oct 2002 22:18
I use UDT returning functions constantly in VB. In DBP, I use types to describe my entities.



See, that is a classic example of what I'd do with it! Imagine if I had a Plane type that was an array of 100 planes - each plane being different. And also plopping new planes in the game after certain planes crash or just after a certain amount of time? It would be so much easier using the CreatePlane function and passing an integer to tell what type of plane I want then to "reinvent the wheel" every time I need a plane!

The One Ring
21
Years of Service
User Offline
Joined: 15th Sep 2002
Location: United States
Posted: 4th Oct 2002 06:41
So true. Being able to use the UDTs like VB Types, C++ STRUCTS, and Delphi RECORDs would be nice.

Login to post a reply

Server time is: 2024-03-29 00:39:52
Your offset time is: 2024-03-29 00:39:52