A little problem with oop...

General FreeBASIC programming questions.
h4tt3n
Posts: 694
Joined: Oct 22, 2005 21:12
Location: Denmark

A little problem with oop...

Postby h4tt3n » Jun 06, 2008 15:12

Code: Select all

Type SubType1
   As Single X
   Declare Sub PrintXfromSub2()
End Type

Type SubType2
   As Single X
End Type

Type MainType
   As SubType1 Sub1
   As SubType2 Sub2
End Type

Dim As MainType Main

Main.Sub2.X = 4

Main.Sub1.PrintXfromSub2()

Sleep

End

Sub SubType1.PrintXfromSub2()
   ''   how the heck do I get X from subtype2 ???
   ''   (below line doesn't work)
   Print Main.Sub2.X
End Sub


The trouble is I can't seem to adress data in one nested subtype with a sub in another one. What's the right syntax?

And another thing... If I try to define a type like this:

Code: Select all

Type SomeType
   Declare Sub SomeSub()
End Type


I get this error:

error 208: An ENUM, TYPE or UNION cannot be empty, found 'End' in 'End Type'


Isn't that a FB bug, since obviously the type is not empty?

cheers, h4tt3n
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Jun 06, 2008 15:24

The trouble is I can't seem to adress data in one nested subtype with a sub in another one. What's the right syntax?
FreeBASIC doesn't have "nested" types. Your types aren't truly nested. If they were, you wouldn't need to specify the instance, I'm sure. SubType1 knows NOTHING about Main.

FreeBASIC doesn't have static type members at the moment, either - Only static procedures. Accessing MyType.MySubType.x should be done as myInstance.myInstance_Member.x

Isn't that a FB bug, since obviously the type is not empty?

With the second block of code, procedures aren't considered a type member in FreeBASIC yet afaik. So your type is empty. Every instance has to have a memory address, so if there's nothing to fill up the space, FreeBASIC can't have an instance of that type.
Hexadecimal Dude!
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:

Re: A little problem with oop...

Postby Hexadecimal Dude! » Jun 06, 2008 15:53

h4tt3n wrote:

Code: Select all

Type SubType1
   As Single X
   Declare Sub PrintXfromSub2()
End Type

Type SubType2
   As Single X
End Type

Type MainType
   As SubType1 Sub1
   As SubType2 Sub2
End Type

Dim As MainType Main

Main.Sub2.X = 4

Main.Sub1.PrintXfromSub2()

Sleep

End


If you could do that, then you could type:

Code: Select all

Type SubType1
   As Single X
   Declare Sub PrintXfromSub2()
End Type

Type SubType2
   As Single X
End Type

Type MainType
   As SubType1 Sub1
   As SubType2 Sub2
End Type

Dim problem as SubType1

problem.PrintXfromSub2()


what should happen in that situation?
h4tt3n
Posts: 694
Joined: Oct 22, 2005 21:12
Location: Denmark

Postby h4tt3n » Jun 06, 2008 16:21

Ok, so I suppose the right way to do this is:

Code: Select all

Type SubType1
   As Single X
End Type

Type SubType2
   As Single X
End Type

Type MainType
   As SubType1 Sub1
   As SubType2 Sub2
   Declare Sub PrintXfromSub2()
End Type

Dim As MainType Main

Main.Sub2.X = 4

Main.PrintXfromSub2()

Sleep

End

Sub MainType.PrintXfromSub2()
   Print Sub2.X
End Sub
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Jun 06, 2008 16:24

@Hexdude and others.

Java: http://copy-pasta.com/pasta157493

C++: http://copy-pasta.com/pasta157494

@h4tten:

If it all works out for you, then yes.
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Postby VirusScanner » Jun 07, 2008 23:25

Your original code will work if you use DIM SHARED.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Jun 08, 2008 0:38

VirusScanner wrote:Your original code will work if you use DIM SHARED.
... And what would be the point of having a setup *anything* like his if he was going to use dim shared?
HD_
Posts: 215
Joined: Jun 10, 2006 12:15
Contact:

Postby HD_ » Jun 08, 2008 1:50

He declared main for a reason...

Another way you could do it is to pass the pointer of the parent type to the subtypes.

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests