Int not an Object?

BlitzMax Forums/BlitzMax Programming/Int not an Object?

BLaBZ(Posted 2012) [#1]
I'm confused, I thought all variable types were objects?

Local X:Int = 4

If Object(x) Then  Notify "is Object" Else Notify "Not"



col(Posted 2012) [#2]
Nah,

Byte,Int,Float and Double aren't 'objects'.

How come you're checking it that way? We may be able to help use an alternative approach.


BLaBZ(Posted 2012) [#3]
Thanks for your help!

I have a variable, "i" that needs to change when the Change method is called. This variable type needs to be able to be a string as well.

Should be something like this but I can't quite get it to work...

Type TType

	Field x:Int Ptr
	
	Method Set(val:Int Ptr)
		x = val
	End Method
	
	'Needs to set the new value for i
	Method Change()
		Local newX:Int = 8
		x = Varptr newX
	End Method
	
End Type

Local i:Int = 2

Local obj:TType = New TType

Print i
obj.Set(Varptr i)
obj.Change()
Print i




BLaBZ(Posted 2012) [#4]
This works for everything but strings

Type TType

	Field x:Int Ptr
	
	Method Set(val:Int Var)
		x = Varptr val
	End Method
	
	'Needs to set the new value for i
	Method Change()
		x[0] = 4
	End Method
	
End Type

Local i:Int = 2

Local obj:TType = New TType

Print i
obj.Set(i)
obj.Change()
Print i




col(Posted 2012) [#5]
As you're setting the value of x using Ptr(s) to change it you would need to access it. So to get your example above working you would...

Type TType

	Field x:Int Ptr
	
	Method Set(val:Int Ptr)
		x = val
	End Method
	
	'Needs to set the new value for i
	Method Change()
		Local newX:Int = 8
		x[0] = newX
	End Method
	
End Type

Local i:Int = 2

Local obj:TType = New TType

Print i
obj.Set(Varptr i)
obj.Change()
Print i


I assume this to do with a debugger? If so, you could take a glance in the modules under brl.mod/appstub.mod/debugger.stdio.bmx for some hints ;-)


BLaBZ(Posted 2012) [#6]
Yeah, i have it working with ints,floats,bytes, etc. But strings are a whole other challenge...

Type TType

	Field x:Byte Ptr
	
	Method Set(val:String Var)
		x = val
	End Method
	
	'Needs to set the new value for i
	Method Change()
		Local newX:String = test
		x = Varptr newX
	End Method
	
End Type

Local i:String = "before"

Local obj:TType = New TType

Print i
obj.Set(i)
obj.Change()
Print i




col(Posted 2012) [#7]
function ( or method ) overloading would be perfect solution, but BMax doesn't support it unfortunately :(

Have you explored using reflection at all?
Nvm, I can already see reflection is not suitable.

You may find that you'll need different TType(s) for each Type that BMax can use. Or at least 2 - Int and Object. Then use the correct Type. You could make a 'Base' Type and Extend the others from it, or simply have a SetObject,SetByte,SetInt etc. It depends on the bigger picture of how/what you want in the final program.

Last edited 2012


BLaBZ(Posted 2012) [#8]
Yeah unfortunately that's not really suitable for my case -

I've decided to create separate functions with pointers to address this, it's not pretty but good enough -

Type TType

	Field AssignStringFunc(Val:String)
	
	Method AssignString(func(Val:String))
		AssignStringFunc = func
	End Method
	
	Method Change()
		Local newStr:String = "NEW STRING"
		AssignStringFunc(newStr)
	End Method
	
End Type

Function ChangeString(Val:String)
	i = Val
End Function 

Global i:String = "before"


Print i
Local obj:TType = New TType
obj.AssignString(ChangeString)
obj.Change()
Print i




col(Posted 2012) [#9]
I understand this is just example code, and may not actually bare any resemblance to real project, however you have i as a Global, changing the value of i will work anywhere in your code. It's just that the example looks like the biggest workaround to some simple typing:

Global i:String = "before"
ChangeI()
Print i

Function ChangeI()
	i = "New String"
EndFunction


:-)