Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Instantiate a class only when valid parameters are available

Former Member
0 Likes
1,676

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

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
942

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

5 REPLIES 5
Read only

Former Member
0 Likes
943

Hi,

You can create a static method that will create and return an instance only if your parameters are correct

Regards

Read only

Former Member
0 Likes
942

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

Read only

Former Member
0 Likes
942

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.

Read only

Former Member
0 Likes
942

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.

Read only

Former Member
0 Likes
942

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.