call a locally designated variable

0

I need to call a variable that is empty, but how do I do it? I created the following code

public ActionResult Index()
{
     cliente cliente;
            try
            {
                 cliente = db.cliente.find(id);
                //etc
            }
            catch (Exception ex)
            {
                var GravaLogErro = new ErroDotNet
                {

                    IDC = cliente != null ? cliente.ClienteId : 0
[Ele fala: User of unasigned local variable 'cliente']
                };
                GravaLogErro.GravarLog();

            }

I did not want to put the client.find out of try / catch until I summarized the logic well, why can not I do a check if it is null? Okay, would I create her like this?

cliente cliente = new cliente();

Would not give problem in find ()?

    
asked by anonymous 30.03.2016 / 15:51

1 answer

2

You can allocate null as well. The advantage is that the code gets more predictable.

cliente cliente = null;

But you do not seem to have much of an advantage in catch you try to access the client, because if you entered it, it's because the find failed and you do not have the correct client, with which you tried to find the client ...

    
30.03.2016 / 16:03