Skip to Content
-2

How to get all class elements?

Aug 15, 2017 at 08:11 AM

172

avatar image
Former Member

Hello,

How to get all elements from class.

I want to clear all global variables. But I don't know, how I get all global elements from my Abap class.

I hope, you could help me.

10 |10000 characters needed characters left characters exceeded

Your question is bit poor. Rather than ask how to implement your chosen solution to the programming problem, you should explain what you are trying to achieve in general. Then you may get other solutions that are better than your preferred one (dynamic clearing of static attributes, it seems). Furthermore, it means people don't have to guess at your intentions, thereby increasing the quality of the answers.

2
* Please Login or Register to Answer, Follow or Comment.

5 Answers

Uwe Fetzer Aug 15, 2017 at 12:31 PM
2

I'd just create a new reference, let the garbage collector do the rest for you...

(after clearing all public and private variables the whole class is initial, isn't it? Or am I missing something?)

Show 6 Share
10 |10000 characters needed characters left characters exceeded

OK for instance attributes. But I think the OP means static attributes by saying global variables.

0

Does that mean a public instance attribute is less global than a private static attribute?

Is there a globality ranking published somewhere I can use for future reference? ;-)

0

Independently from what the OP meant, you cannot clear all attributes by garbage collection but only instance attributes. Since he said "all elements from class", it is justified to assume that he meant all attributes including static attributes.

The terms for your reference are defined in the ABAP Glossary, e.g. global data, which, alas, are seldom use here .

1

It was a joke. You assumed he meant static, but if someone inaccurately calls attributes global, which they clearly aren't (as I said in my first response), then they would not necessarily have the same view.

If we try to define the globularity (globulosity?) of scope vs lifetime, one could argue that that which is visible to the outside looks 'global', making a public 'more global' than a private static, even though it has a shorter lifetime.

But, just in case someone draws any conclusions, all of this is nonsense of course :-)

0

Then I would use a structure instead of single variables.

Dynamic programming just to clear variables would be "mit Kanonen auf Spatzen schießen" IMO. But possible of course.

0

If it's your class ...

1
Suhas Saha
Aug 15, 2017 at 12:56 PM
1

@OP: What is your motivation for clearing the attributes of the instance?

Share
10 |10000 characters needed characters left characters exceeded
Horst Keller
Aug 15, 2017 at 08:27 AM
1

CL_ABAP_CLASSDESCR

Loop over the attribute table, dynamic assign to field symbol (attribute must be visible), CLEAR field symbol.

Share
10 |10000 characters needed characters left characters exceeded
Matthew Billingham
Aug 15, 2017 at 10:16 AM
0

If you mean how to clear all statics in a class - write a method to do it and call that.

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

I want to do this dynamic and don't want to clear every variable by hand.

0

Clearing all the static attributes of a class indicates that your design is poor. I can't see why the complexity of doing it dynamically could be preferable. I've never written a class where this was needed, and I've written thousands.

Consider using the singleton pattern instead. Then you can have a static method that creates and new instance, and returns that (clearing the old one).

1
Mike Pokraka Aug 15, 2017 at 08:27 AM
0

An ABAP class doesn't have any global variables. All attributes are local to the class.

RTTI services can provide you all components of a class, use CL_ABAP_CLASSDESCR.

Show 5 Share
10 |10000 characters needed characters left characters exceeded
Former Member

and how i could clear all these components or is it not possible?

0

Loop over the attribute table, dynamic assign to field symbol, CLEAR field symbol. Basic stuff.

0
Former Member
Horst Keller

How I get these attribute table. I don't manually used the CL_ABAP_CLASSDESCR before.

0

If you haven't done this before, then I would ask why you think you need to do it this way?

See answers from Uwe and Suhas.

0

Have you even looked at it?

0