AndroidDev
AndroidDev

Reputation: 691

Method with restrict parameter

When you try to give another value to the 3rd argument in Toast.makeText other than Toast.LENGTH_LONG or Toast.LENGTH_SHORT it won't accept.

I need to create a method and use the same type of restriction: it can only be accepted three values which I'll define previously using constants.

How can this be accomplished?

Upvotes: 1

Views: 415

Answers (2)

Yasir Tahir
Yasir Tahir

Reputation: 800

You can restrict the user by creating your own enum. Sample code:

public enum Notification {
    LONG,
    SHORT
}

Method which I want to restrict:

private void methodName(String value1, Notification value2){
// Write whatever you want to perform
}

Now, this method can only be called like:

methodName("", Notification.LONG); // Second Parameter is allowed only from your custom enum

Upvotes: 3

Raman Shrivastava
Raman Shrivastava

Reputation: 2953

Throw IllegalArgumentException from your method if argument passed is not something you want.

Upvotes: 0

Related Questions