Modify String object value passed as parameter

Asked

Viewed 934 times

3

I need to change the value of a variable dynamically by passing it as a parameter by an auxiliary method. I have always used the values of the parameters by moving from right to left. It is possible to change the variable from left to right:

Fields of 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 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 be 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 you get no error, the value of the fields are always empty. I am doing something wrong or demanding something that is not the language?

  • 1

    You are not passing the attributes themselves, just copying their value to the method cmdAdress.

  • Is it possible to pass the attribute? I’ve tried this cmdAdress(Object address, String type, string Addresses). It also doesn’t work.

  • 1

    Just calling the attribute straight in the method, as you did and commented that it was not useful, but using primitive types and String, is the only way.

  • Then I will have to create two auxiliary methods: one to set each attribute. Correct?

  • Why not directly change the class attribute? Why pass the attribute itself as a parameter to a class method?

  • The intention was to leave the dynamic method without having to duplicate it. The parameters are of the same type, so I thought I would avoid creating a method for each variable or avoid having to create a switch or if to determine which variable I would be dealing with.

  • 2

    If there is need to identify whether it is an ipaddress or dnsAddress, unfortunately you will have to make use of it. I would use constants to identify the type, and pass it as a parameter, so you can treat more simply which of the two information is being received.

Show 2 more comments

1 answer

4


I think it would be better to use function return to define the ipAddress and the dnsAddress. Leaves the passage 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;
}
  • You can use a enum to get organized

  • Yes, we could also use an Enum and a Stringbuilder in cmdAddress, would be better.

  • Got it. It was the solution that diegofm suggested. From what I saw I’m demanding even what there is in the language. About the adapterName, it comes from the constructor I omitted in the question and also there are already two getters returning ipaddress and dnsAddress. I am very pleased with the help. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.