Delmon Young
Delmon Young

Reputation: 2053

Move conditional into multiple methods

I've got a really simple code snippet that I'm trying to move into two methods but for the life of me can't figure out what I'm doing wrong, here is the code currently:

if (chatLayer != null) {
  Page checkPage = resource.resourceResolver.getResource(url).adaptTo(Page);
  url = (checkPage) ? ".html" : "";
} else {
  PageManager manager = resource.getResourceResolver().adaptTo(PageManager.class);
  String currentPage = manager.getContainingPage(resource).getPath();
  url = "&refer="+currentPage;
}

When I try to turn it into methods:

url = chatLayer ? chatOverlay() : chatURL()

private static String chatOverlay(Page checkPage, String url) {
    Page checkPage = resource.resourceResolver.getResource(url).adaptTo(Page);
    url = (checkPage) ? ".html" : "";
}

private static String chatURL(Page checkPage, String url) {
    PageManager manager = resource.getResourceResolver().adaptTo(PageManager.class);
    String currentPage = manager.getContainingPage(resource).getPath();
    url = "&refer="+currentPage;
}

All I get as an error is a current scope already contains a variable of checkpage. Any help is greatly appreciated. I'm a newbie so modifications to my code or code snippets is greatly appreciated.

Upvotes: 0

Views: 60

Answers (2)

ktm5124
ktm5124

Reputation: 12123

private static String chatOverlay(Page checkPage, String url) {
    Page checkPage = resource.resourceResolver.getResource(url).adaptTo(Page);
    url = (checkPage) ? ".html" : "";
}

You already have a variable named checkPage as one of your method parameters. You can't have two variables named checkPage in your method -- how would the compiler know which variable you are referring to?

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

In your method chatOverlay you have a Page checkPage parameter and a Page checkPage local variable. Change the name of one of them. Additional: you're not returning anything from your methods (not sure if this is a typo).

private static String chatOverlay(Page checkPage, String url) {
    //changed the name of the local variable
    Page checkedPage = resource.resourceResolver.getResource(url).adaptTo(Page);
    url = (checkedPage) ? ".html" : "";
    return url;
}

Upvotes: 5

Related Questions