Solved

which line of code is better c# 4

Posted on 2012-03-29
4
262 Views
Last Modified: 2012-03-29
Just a quick question...

I'm wondering which if either of the following lines of code are better and if I should/shouldn't be doing one or the other....

Customer c;
c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));

Open in new window


OR

Customer c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));

Open in new window


Which is "industry standard"/correct way of doing it?
0
Comment
Question by:scm0sml
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37781076
My opinion:  either is fine. I do think, however, that it depends on what the code surrounding it does. What I mean is that if there is some condition being tested for which the logic you posted should only be run if the condition is true, and the logic for that condition has the potential to generate an exception, then you might use the former to prevent executing what you posted until you know for sure that the condition succeeded.

For example:

Customer c;

try
{
    if (someExceptionGeneratingCondition)
    {
        c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));
    }
}
catch (Exception ex)
{
    throw new ApplicaitonExcption("Exception-generating condition generated an exception", ex);
}

Open in new window


In the above, perhaps FindById just happens to be a long-running or expensive method to call. You may not want to initialize "c" until you are sure that you've passed the "someExceptionGeneratingCondition."
0
 

Author Comment

by:scm0sml
ID: 37781083
kaufmed if I went with your example there, should I be declaring c as Customer c = null; or not?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37781089
It's a good indication to the reader coming behind you that you (yourself) understood that you weren't initializing the variable in its declaration, but you don't have to. The only time you must do that (AFAIK) is if you were referring to "c" outside of the body of the if. In that case, the compiler wouldn't know if you understood that the variable wasn't initialized, and it would give you an error stating uninitialized variable.

e.g.

Customer c = null;  // Must do to prevent compiler error on last line

try
{
    if (someExceptionGeneratingCondition)
    {
        c = db.Customers.FindById(Convert.ToInt32(hdnCustomerID.Value));
    }
}
catch (Exception ex)
{
    throw new ApplicaitonExcption("Exception-generating condition generated an exception", ex);
}

c.SomeMethod();

Open in new window

0
 
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37781194
Hi scm0sml,

I know you have asked a specific question, but I could not resist.
how about we use int.tryParse and throw exception if it returns false?

Regards,
Chinmay.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Form Data Disappears Using Back Button 4 65
make control visible based off other control value 7 44
Return array 3 33
C# LINQ 5 40
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question