C # - Reference a variable from another method [closed]

0

I have two methods and I want to get a variable from one method to another. It is the base64 variable that is in the base64Decode function. The code is as follows:

 public class WK_UpdateAlvo : CodeActivity
{
    [Input("StringFile")]
    public InArgument<string> StringFile { get; set; }

    protected override void Execute(CodeActivityContext Execontext)
    {
     ITracingService _tracing;
     try
        {
           AllMethods.Base64Decode(base64);
            _tracing.Trace("base64: {0}", base64);
        }
     catch {
            trhow;
           }
          public class AllMethods
          {
            //decode a string 
            public static string Base64Decode(string stringfile)
            {
              var base64 = System.Convert.FromBase64String(stringfile);
              return System.Text.Encoding.UTF8.GetString(base64);
            }
          }
    }
 }

Thank you.

    
asked by anonymous 19.08.2016 / 11:49

2 answers

2

By what I understand from your code, what you want is in a class ( WK_UpdateAlvo) to access a method ( Base64Decode() ) of another class ( AllMethods ).

The code is already calling this method on the line

AllMethods.Base64Decode(base64);

within the try / catch block

But the way the code is, it does not even compile.

The declaration of the AllMethods class can not be made within a method. It should preferably be declared in a separate file, or at the same level as the other.

Change your code like this:

public class WK_UpdateAlvo : CodeActivity
{
    [Input("StringFile")]
    public InArgument<string> StringFile { get; set; }

    protected override void Execute(CodeActivityContext Execontext)
    {
     ITracingService _tracing;
     try
        {
           AllMethods.Base64Decode(base64);
            _tracing.Trace("base64: {0}", base64);
        }
     catch {
            trhow;
           }
    }
 }

public class AllMethods
{
    public static string Base64Decode(string stringfile)
    {
      var base64 = System.Convert.FromBase64String(stringfile);
      return System.Text.Encoding.UTF8.GetString(base64);
    }
}

The code presents other "problems" but is not relevant to the case.

    
19.08.2016 / 13:38
1

Note that when creating a class, we can define some input variables, for example:

class Cadastro_obsClie
{
    // Neste caso minhas variaveis de entrada são, id_cliente, data, titulo e descricao
    public int cadastro_obs_cliente(int id_cliente, string data, string titulo, string descricao)
    {
        // método qualquer.
    }
}

When we are going to instantiate a class, we must inform which variables corresponds to my input variables, for example:

// Instanciando a classe Cadastro_obsClie
Cadastro_obsClie cad_obsClie = new Cadastro_obsClie();

// Chamamos o metodo cadastro_obs_cliente
// repare que estou passando as variáveis de entrada para o metodo
cad_obsClie.cadastro_obs_cliente(id_cliente, DateTime.Now.ToLongDateString(), "Cadastro", "Cadastro inicial - Cliente inserido no banco de dados.");

Then, when constructing your class, you must parameterize the input variable, and when doing the instantiation you must enter the variable.

    
19.08.2016 / 12:53