Solved

which line of code is better c# 4

Posted on 2012-03-29
4
260 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now