Any Way To Read Field Name And use it in same line

BlitzMax Forums/BlitzMax Programming/Any Way To Read Field Name And use it in same line

Hardcoal(Posted 2015) [#1]
Hi.

Maybe its silly but Im trying to do this..

Field ObjectXPos$ = GetNameOfField$()

I want this function Called GetNameOfField() to read the Field Name which is excuting her. and in this case function should return "ObjectXPos"

Is it possible?

Thanks in advance


degac(Posted 2015) [#2]
Hi

I don't really understand your request, but the closer thing it's REFLECTION.
It allows to 'know' what are the fields of an object and read/write their content.


col(Posted 2015) [#3]
I'm not too sure either, but the way that you're proposing there looks like it would end up in an infinite loop?

The variable value is the result of the function/method call that is used to get the value of the variable whose value is the result of the function etc....

[edit]
Too early in the morning for me :D
As suggested it looks as though you need 'reflection'.
[/edit]


Hardcoal(Posted 2015) [#4]
I know how to use reflections more or less.
So ill try to figure this up and post if succeed.


col(Posted 2015) [#5]

I want this function Called GetNameOfField() to read the Field Name which is excuting her.


But even then there's no easy way for the function/method to know who callled it unless you pass in a variable to let it know.


col(Posted 2015) [#6]
If you can use a variable as a parameter then it becomes quite trivial.
Not sure if this would fit in with your requirements though, and of course you'd probably want to use the name in a different way than is shown...

Type myObject
	Field ObjectXPos$ = GetNameOfField(0)
	Field ObjectYPos$ = GetNameOfField(1)
	Field ObjectZPos$ = GetNameOfField(2)
	
	Method GetNameOfField$(FieldPosition:Int)
		Local Tid:TTypeId = TTypeId.ForObject(Self)
		Local Fields:TField[] = TField[](Tid.Fields().ToArray())
		
		Return Fields[FieldPosition].Name()
	EndMethod
EndType

' create an object
Local Obj:myObject = New myObject
Print Obj.ObjectXPos



Hardcoal(Posted 2015) [#7]
Tnx Dave. Thats great..
Though I'm trying to do that without giving an index number. And trust me I'll find it and post it.

(actually already found a solution)

Now for Another question..
How can you get the index of an object on a list without counting the whole list for comparisons?


Brucey(Posted 2015) [#8]
How can you get the index of an object on a list without counting the whole list for comparisons?

Make your own subclass of list and implement the functionality yourself.

actually already found a solution

What was it then? (for those in the future who'd like to know the same)


col(Posted 2015) [#9]

(for those in the future who'd like to know the same)



And the present :p


Hardcoal(Posted 2015) [#10]
Hi Brucey .. of course Ill put my solution.
I Just came home.. now. its not like Im always in front of the computer :)
Got Stuck with the Car.. Long Story.. :(

The Solution is in my head.. and now I need to test it..

Brb in a sec :p.. (Hope Im right..)


Hardcoal(Posted 2015) [#11]
Im trying to post replay since yesterday and I get a server error.
Something in my Text of my code makes that error...

here is the error message

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@... to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/2.4.10 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Server at www.blitzbasic.com Port 80


Hardcoal(Posted 2015) [#12]
I removed some text from my post and now it works..

This what is causing a server error: '- - -'
Type this but without spaces and see if it happens to you too.

Strict

Global Gen:General = New General

Global CurrentObject:Object
Global FieldCounter

Type General

	Method GetFieldName:String(Obj:Object)
		Local IntFieldCounter, TargetFld:TField
		
		'Zero Process
		If Obj <> CurrentObject Then
			CurrentObject = Obj
			FieldCounter = 0
		End If
		
		'Process
		For TargetFld = EachIn TTypeId.ForObject(Obj).EnumFields()
		
			IntFieldCounter = IntFieldCounter + 1
	
			If IntFieldCounter > FieldCounter Then
			
				FieldCounter = IntFieldCounter
				Print TargetFld.Name()
			 	Return TargetFld.Name()
				
			End If
		Next
		
	End Method

End Type

Type MyObject

	Field ObjectXPos:String = Gen.GetFieldName(Self)
	Field ObjectYPos:String = Gen.GetFieldName(Self)
	Field ObjectZPos:String = Gen.GetFieldName(Self)
	
EndType

Type AnotherType

	Field ThisIsTwo:String = Gen.GetFieldName(Self)
	Field ThisIs45:String = Gen.GetFieldName(Self)
	
End Type

'Test Area'

New MyObject
New AnotherType




Any way.. this is only good if all your GetFieldName() are used one after the other..
or if you add on the function a field type filter and create your own special Type to be recognized by the process.

im sure most people dont need this process any way. it was only for my specific need.

I think the only way to really do it correctly is if you can while on compiling the code read the current compiled field somehow..
other wise its a compromised method.

Any way.. while working on that.. I realized new ways to reach what I need without it,
and now i dont think I really need this process.

Thats the result of Programming Evolution


col(Posted 2015) [#13]
Heh, Now it looks clearer what you're trying to achieve :)

You say you probably wont do it this way anyway, which is cool like you say, programming evolution, but you got me thinking of how of ways to do what you originally asked...

You could utilize metadata...

Strict

Type MyObject
	Field ObjectXPos:String { AssignString }
	Field ObjectYPos:String
	Field ObjectZPos:String	{ AssignString }
	
	Method New()
		Local Tid:TTypeId = TTypeId.ForObject(Self)
		For Local Fld:TField = EachIn Tid.Fields()
			If Fld.MetaData() = "AssignString=1" Fld.Set(Self,Fld.Name())
		Next
	EndMethod
EndType

Local Obj:MyObject = New MyObject
Print Obj.ObjectXPos
Print Obj.ObjectYPos
Print Obj.ObjectZPos



Hardcoal(Posted 2015) [#14]
I'm glad I turned your interest col.
I might use it what I've done..
Still didnt decided not too.

I'm trying to simply matters and so far it worked for me well.

Now I'm trying to bring it to a new level of simplification and I will post it here
With my conclusions..

I'll check metadata but I don't know what it means exactly..


col(Posted 2015) [#15]
If my memory serves me well I recall that JoshK once mentioned he uses the metadata tag in the Leadwerks editor to assign loaded data values to the appropriate fields of an object. This allowed him to greatly simplify and automate his object saving and loading code.


I'll check metadata but I don't know what it means exactly..



Metadata is simply a data used to describe already existing data. In laymens you can assign metadata and interpret it any way you wish. As in the example code above. I'm not sure why in Max the '=1' is tagged on to it though, maybe someone else know's the reason behind that one.

[edit]
I think it would more accurate to say that JoshK used reflection extensively, as opposed to just using metadata.
[/edit]