call a locally designated variable

Asked

Viewed 32 times

0

I need to call a variable that is empty, but how to 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();

            }

Didn’t want to put the client.find out from Try/catch until pq I summarized well the logic, why can’t I do a check if it is null? Right would be me raising her like this?

cliente cliente = new cliente();

There would be no problem later in find()?

1 answer

2


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

cliente cliente = null;

Only it doesn’t seem to have much leverage on catch you try to access the customer, because if you went in there, it’s because find failed and you don’t have the right customer, I think you better print the id with which you tried to find the customer...

Browser other questions tagged

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