2008 Jun 13 7:46 AM
Hi,
I have a class which has user id as import parameter for the constructor. My class is instantiated based on the USER ID and populates the attributes PERNR, NAME and POSITON.
Now when the constructor method is executed my class gets instantiated in all the scenarios. When i dont pass an user id all my parameters are blank but my class still creates an instance. But i want my instance to be created only when a valid user id is passed.
I tried to exit the constructor when an invalid user id is passed but still my class gets instantiated (with all attributes blank). One way could be i could check for the validity of the user id before instantiating my class but i want my constructor to take care of this.
Is this possible? Can someone let me know how to achieve this?
Thanks,
Prasath N
2008 Jun 13 1:40 PM
Hi,
You can create a static method that will create and return an instance only if your parameters are correct
Regards
Hi,
I have a class which has user id as import parameter for the constructor. My class is instantiated based on the USER ID and populates the attributes PERNR, NAME and POSITON.
Now when the constructor method is executed my class gets instantiated in all the scenarios. When i dont pass an user id all my parameters are blank but my class still creates an instance. But i want my instance to be created only when a valid user id is passed.
I tried to exit the constructor when an invalid user id is passed but still my class gets instantiated (with all attributes blank). One way could be i could check for the validity of the user id before instantiating my class but i want my constructor to take care of this.
Is this possible? Can someone let me know how to achieve this?
Thanks,
Prasath N
2008 Jun 13 1:40 PM
Hi,
You can create a static method that will create and return an instance only if your parameters are correct
Regards
2008 Jun 13 1:47 PM
hi,
try to do validation.....and then create the instance.
parameter : id(20), name(30) , pernr(14).
at selection-screen on id.
if not id is initial.
create object.
else.
message 'enter the id'.
This is just psedu code.
Try to use the logic.
Regards
Sandeep Reddy
2008 Jun 13 3:11 PM
You cannot control the instantiation of the object from constructor itself as in ABAP we donot have any concept of explicit destructor like in C++. But may be by using the given code you can achieve the same indirectly.
class z_test definition.
public section.
Methods: constructor
importing p type i.
private section.
data: val type i.
endclass.
class z_test implementation.
method constructor.
val = p.
endmethod.
endclass.
class z_wrapper definition.
public section.
Methods: constructor
importing p type i,
check_intantiation
returning value(stat) type char1.
private section.
data: dref type ref to z_test.
endclass.
class z_wrapper implementation.
method constructor.
if p < 10.
create object dref exporting p = 12.
Write: /'Object DREF created'.
else.
free dref.
Write: /'Object DREF not created'.
endif.
endmethod.
method check_intantiation.
if dref is not bound.
clear stat.
else.
stat = 'X'.
endif.
endmethod.
endclass.
start-of-selection.
data: dref type ref to z_wrapper.
data: status type char1.
create object dref exporting p = 12.
if dref->check_intantiation( ) = space.
free dref.
Write: /'Main object is deleted'.
endif.
2008 Jun 13 3:35 PM
One option is don't create any public constructor instaed create one static method say GET_INSTANCE which will take input parameter as user id and returning you the instance.
Do validations on user id in this method and on success return instance..
something like
data : obj type ref to zcl_test.
obj = zcl_test=>get_instance( user id = ..).G@urav.
2008 Jun 13 3:41 PM
Hi,
I'm not sure if this will work but worth giving it a try. Create exception class or raise any standard exception and see what happens.
regards,
Advait.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |