Solved

which line of code is better c# 4

Posted on 2012-03-29
4
258 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 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

18 Experts available now in Live!

Get 1:1 Help Now