C# - Reference a variable from another method

Asked

Viewed 1,681 times

0

I have two methods and I want to find a variable from one method to the other. It is the variable base64 who is in the function base64Decode. 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.

  • Simple. You can’t do that. If you have control over the class, you have to put in a field.

  • @Claudia, good morning. I didn’t quite understand your question, could you be more objective? from what I’ve seen, you have a method that encodes a string to base 64, and you want to use that variable in another method and that?

  • @Thomaserichpimentel yes that’s it

  • @Brunocosta can exemplify? Thank you

  • I don’t know where you got that piece of code from, but it doesn’t sound good. that part of class within class do not know where you want to get with this, you do not reference variable (Internal) method what can be done is the return, ie you can call your method and expect the return variable.

2 answers

2


From what I understand of your code, what you intend is, in a class(Wk_updatealvo) access a method(Base64Decode()) otherworldly(Allmethods).

The code is already calling this method on the line

AllMethods.Base64Decode(base64);

inside the block Try/catch

But the way the code looks, it doesn’t even compile.

The declaration of the class Allmethods cannot be done 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 are not relevant to the case.

  • Could give a slight example on the functionality of AllMethods?

  • @Thomaserichpimentel, this is just a class, I think he created with that name in order to have all his methods within it.

  • Ha perfect. thank you.

  • 1

    @Thomaserichpimentel Allmethods is a class helper. These types of classes have methods, usually static, which by their nature do not belong to any particular class.

1

Note that when creating a class, we can define some input variables, 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 correspond to my input variables, 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.

  • I mean, in my job I have to put the variable base64 as parameter? This way: public static string Base64Decode(string stringfile, byte[] base64) That is correct?

  • No, you have to put this parameter, in the class that will instantiate, as I understand it, the Base64decode class, encodes a string in Base64 and this string you want to use in another class, so in the other class you must parameterize the string that is sending. Obs: the Base64decode method is returning a string, so it should be parameterized as public void outraclasse(string nomedavariavel)

  • It worked. Thank you.

Browser other questions tagged

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