Modify value of the String object passed as parameter

3

I need to change the value of a variable dynamically by passing it as a parameter by an auxiliary method. I always used the parameter values from right to left. You can change the variable from left to right:

Fields in my class:

private String ipAddress;
private String dnsAddress;

Set method to assign value to variable:

public void setDHCP(){        
    cmdAdress(ipAddress,"address", "dhcp");
    cmdAdress(dnsAddress, "dnsservers", "dhcp");        
}

Auxiliary method to make the set dynamic:

private void cmdAdress(String address, String type, String adresses){          
    //Isso não funciona
    address = "netsh interface ip set ";

    //Isso funciona mas não me é útil
    //ipAddress = "netsh interface ip set ";

    //restante do código
    address += type;        
    address += " name = \"" + adapterName + "\" ";      
    address += adresses + " "; 
}

The expected value would look something like this:

ipAddress: netsh interface ip set address name = \"OffBoard\" static 192.168.1.220 255.255.255.0 192.168.1.1
dnsAddress: netsh interface ip set dnsservers name=\"OffBoard\" static 192.168.1.1 primary no

Although there is no error, the value of the fields is always empty. Am I doing something wrong or demanding something that is not language?

    
asked by anonymous 20.11.2016 / 14:03

1 answer

4

I think it would be better to use the function return to set ipAddress and dnsAddress . It leaves the pass by parameter only to input the necessary data to the function. According to your example, it would look like this:

final private String TIPO_ADDRESS = "address";
final private String TIPO_DNSSERVERS = "dnsservers";
private String ipAddress;
private String dnsAddress;

public void setDHCP(){        
    ipAddress = cmdAdress(TIPO_ADDRESS, "dhcp");
    dnsAddress = cmdAdress(TIPO_DNSSERVERS, "dhcp");        
}

private String cmdAdress(String type, String adresses){
    String retorno = "netsh interface ip set ";
    retorno += type;
    retorno += " name = \"" + adapterName + "\" ";
    //Não sei de onde viria o adapterName - talvez de uma variável global, mas não é aconselhável - que tal passar como parâmetro também?
    //Se for uma constante, padronize o nome como maiúscula, igual ao TIPO_ADDRESS E TIPO_DNSSERVERS
    retorno += adresses + " ";
    return retorno;
}
    
21.11.2016 / 13:07