error calling a method passing a class as parameter c#

Asked

Viewed 337 times

0

Good!

I took part of a code that creates a user in AD with C#, the problem is when I call the method, I am beginner in programming, I will pass the code and the error.

  private static DirectoryResponse EnviarRequisicaoLdap(DirectoryRequest request)
    {
        LdapConnection ldapConexao = new LdapConnection(new LdapDirectoryIdentifier(LdapServidor, LdapPorta));
        try
        {
            // Tipo de autenticação com o LDAP
            ldapConexao.AuthType = AuthType.Basic;
            // Envia as credenciais com o dn completo
            NetworkCredential credenciais = new NetworkCredential(LdapUsuario, LdapSenha);
            // Estabele a conexão com o LDAP
            ldapConexao.Bind(credenciais);
            // Envia a requisição ao LDAP
            return ldapConexao.SendRequest(request);
        }
        finally
        {
            // Libera a conexão com o LDAP.
            if (ldapConexao != null)
                ldapConexao.Dispose();
        }
    }

    public static bool IncluirUsuario(Usuario usuario)
    {
        if (usuario != null)
        {
            string dn = string.Format("uid={0},{1}", usuario.Email, LdapUnidadeOrganizacional);

            AddRequest addRequest = new AddRequest(dn);

            addRequest.Attributes.AddRange(new DirectoryAttributeCollection() {
        new DirectoryAttribute("objectClass", LdapObjectClass),
        new DirectoryAttribute("uid", usuario.Email),
        new DirectoryAttribute("cn", usuario.Nome),
        new DirectoryAttribute("sn", usuario.Sobrenome),
        new DirectoryAttribute("userPassword", ConverterSenhaLdapMD5(usuario.Senha)),
    });

            // Acessando o LDAP
            EnviarRequisicaoLdap(addRequest);
            return true;
        }
        return false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        IncluirUsuario();
    }  
o parametro está em branco, pq qualquer coisa que eu passar, vai dar erro. 

There is no argument Given that Corresponds to the required formal Parameter "usuario"

Thanks in advance.


@G.Otanip did not understand very well, as I said I’m beginner, but I will post the class here and thank you for the response:

public sealed class Usuario
    {
        public string Nome = "TESTE";
        public string Sobrenome = "CJ";
        public string Email = "[email protected]";
        public string Senha = "******";
    }
  • 2

    The parameter you need to pass is an object of type Usuario. See the statement of this class Usuário and how you can instantiate and fill it before calling the function IncluirUsuario()

  • Dude I think I figured it out with the tip you gave me! I managed to instantiate.

  • Anderson, if you managed to resolve your question, I ask you to mark the question as solved, below the upvotes in the answers. Thanks.

3 answers

2

The error is because you function IncluirUsuario(Usuario usuario) requires a parameter. This parameter must be an object of the type Usuario, that is, you will have to instantiate this class Usuario and send this instance (object) as function parameter.

If the class Usuario is the one you showed in your reply, you have to instantiate her like you fez:

Usuario exemplo = new Usuario();

But, in addition, the function IncluirUsuario(Usuario usuario) seems to rescue and use the attributes (Nome, Sobrenome, etc) of the object Usuario that you passed as parameter, so it is good you fill them before sending it to the function:

// Em algum lugar do código, você vai alterar os atributos do exemplo (resgatando de um TextBox por exemplo)
exemplo.Nome = "Exemplo"
exemplo.Sobrenome = "De Código"
exemplo.Email = "[email protected]"
exemplo.Senha = "senhaforte"

So when the function IncluirUsuario(Usuario usuario) is called, these attributes must have already been filled.

One remark, I’m not sure, but I think you should put those two answers together with the question if they’re not the solution to your problem.

2

Anderson, yes the instance this way is right yes. However the values you will pass to the Incluusuario method will be those predetermined in the class. The ideal would be for the properties within the class to be empty and for whom to instantiate to fill in the values. For example.

Usuario user = new Userio();

and now you fill the object with the new user information to be created.

user.Nome = "Nome do usuário"; 
user.Sobrenome = "Sobrenome do usuário"; 
user.Email = "E-mail"; 
user.Senha = "Senha do usuário"

After the object is filled you call the Include function passing the goal you just created as parameter.

  IncluirUsuario(user);
  • Thanks! Thank you very much!

  • For nothing ;-) precise we are there.

0

@Anderson User777, you are connected to some database?

If you are connected to the database you can work with language mysql and everything gets a lot easier and in my opinion a lot more beautiful and presentable.

If you have a database try connecting to the database using :

> private string  StringDeConexao = @”Data Source=localhost,1433;Initial
> Catalog=master;User Id=sa;Password=minhasenha;”;

After you are connected to the database you can enter, delete and change the data. Do some more research on this method and see if you like it.

Browser other questions tagged

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