Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. What is the most common error in a .Net application?

What is the most common error in a .Net application?

Scheduled Pinned Locked Moved The Lounge
questioncsharphelp
59 Posts 26 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Colin Angus Mackay

    Are you calling anything on value1, value2, value3 or value4? String.Format will check if a value passed to it is null before operating on it. So, it is not really the same situation.

    Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

    J Offline
    J Offline
    jith iii
    wrote on last edited by
    #47

    I think I'm bad in communication :) This was not related to null reference. I just quoted this code when you said about tostring() on integer. Here they could have written the code simply as,

    FinalString=String.Format("{0}\{1}\{2}\{3}",value1,value2,value3,value4)

    If you want null reference comedy ...here is a gem

    IF object1.ToString() <> Nothing Then

    End If

    C 1 Reply Last reply
    0
    • C Colin Angus Mackay

      jith - iii wrote:

      ToString() is quite popular here. It's coming repeatedly everywhere

      Well I wish it wasn't. The number of times I've been through code written by complete Muppets who call ToString() on everything (especially on strings themselves) is depressing. I even saw someone call ToString() on an integer, pass it to a Method and for the method to Parse it back to an integer again. That method was only ever called from one place! There was no need. The quality of some code is arse-paralysingly mind blowing!

      Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

      C Offline
      C Offline
      chaiguy1337
      wrote on last edited by
      #48

      Colin Angus Mackay wrote:

      I even saw someone call ToString() on an integer, pass it to a Method and for the method to Parse it back to an integer again. That method was only ever called from one place!

      lol

      Sad but true: 4/3 of Americans have difficulty with simple fractions. There are 10 types of people in this world: those who understand binary and those who don't. {o,o}.oO( Check out my blog! ) |)””’)          http://pihole.org/ -”-”-

      1 Reply Last reply
      0
      • J jith iii

        I think I'm bad in communication :) This was not related to null reference. I just quoted this code when you said about tostring() on integer. Here they could have written the code simply as,

        FinalString=String.Format("{0}\{1}\{2}\{3}",value1,value2,value3,value4)

        If you want null reference comedy ...here is a gem

        IF object1.ToString() <> Nothing Then

        End If

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #49

        jith - iii wrote:

        If you want null reference comedy

        You should read by blog. When I see weird stuff I blog it. Here's one I spotted recently: Formatting Dates the Hard Way[^]

        Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

        J 1 Reply Last reply
        0
        • C Colin Angus Mackay

          jith - iii wrote:

          If you want null reference comedy

          You should read by blog. When I see weird stuff I blog it. Here's one I spotted recently: Formatting Dates the Hard Way[^]

          Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

          J Offline
          J Offline
          jith iii
          wrote on last edited by
          #50

          Colin Angus Mackay wrote:

          Formatting Dates the Hard Way[^]

          That was cool :)...i've bookmarked your blog. One cool blog entry similarly i came across is this[^] from Tom kyte's blog. I am not sure if I came to the above link from codeproject itself,then you would have seen it. I was not aware that Tom Kyte is also blogging.

          modified on Tuesday, December 16, 2008 3:39 PM

          1 Reply Last reply
          0
          • C chaiguy1337

            I agree. I wish null exceptions were handled more friendly. They can be helpful while developing to identify potentially problematic or incorrect areas of code, but for end-user releases, they just end up getting in the way, since 90% of the time, just ignoring them is the solution anyway. Yet C# forces us to explicitly ignore them, rather than saving us the trouble. Look at WPF data binding as an example of how it can actually be helpful to default to ignoring: if you bind an object to a control in WPF and the object is null, WPF doesn't crash with a null reference exception, it just doesn't display anything.

            Sad but true: 4/3 of Americans have difficulty with simple fractions. There are 10 types of people in this world: those who understand binary and those who don't. {o,o}.oO( Check out my blog! ) |)””’)          http://pihole.org/ -”-”-

            M Offline
            M Offline
            Member 2956838
            wrote on last edited by
            #51

            Why not using the famos Nz() function (and if you dont have it build one) G

            C 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Are you calling anything on value1, value2, value3 or value4? String.Format will check if a value passed to it is null before operating on it. So, it is not really the same situation.

              Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

              F Offline
              F Offline
              franky1987
              wrote on last edited by
              #52

              Hi all, Thanks to VS2008 I put in all my applications an assembly full of Extension Methods that are so useful when I'm in production. One of these extension method is called 'IsNullOrTrimmedEmpty()'. Actually it just check if a string refence is null and than, if it isn't, if the trimmed value of the string equals to "", and return me a bool value. Not too much advanced, but very helpful!!

              1 Reply Last reply
              0
              • J jith iii

                Last week we had a code delivery. We have been taught from the graduation days that ToString() without a null check is a bomb. Still we got it in a critical part . "Object reference not set" My vote goes for it.

                modified on Monday, December 15, 2008 2:26 PM

                D Offline
                D Offline
                deostroll
                wrote on last edited by
                #53

                I agree. 2nd to tht tho I used to get those page validation errors, you know when you try to use a rich text editor for your website, and then those DBNull things.

                1 Reply Last reply
                0
                • M Member 2956838

                  Why not using the famos Nz() function (and if you dont have it build one) G

                  C Offline
                  C Offline
                  chaiguy1337
                  wrote on last edited by
                  #54

                  Can you elaborate on that?

                  Sad but true: 4/3 of Americans have difficulty with simple fractions. There are 10 types of people in this world: those who understand binary and those who don't. {o,o}.oO( Check out my blog! ) |)””’)          http://pihole.org/ -”-”-

                  L 1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    jith - iii wrote:

                    . It will instantly give error if you try to set null to an integer,right? why double standard to string where you can set it to nothing but not otherwise?

                    There is no double standard. All reference types behave the same way. It is perfectly valid in many situations for a reference to be null. The compiler cannot catch logic errors, only syntax errors. System.Int32 is a value type. It is not valid ever for a value type to be null therefore the compiler can and will catch it for you.

                    Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

                    K Offline
                    K Offline
                    Kevin McFarlane
                    wrote on last edited by
                    #55

                    Colin Angus Mackay wrote:

                    It is perfectly valid in many situations for a reference to be null.

                    That's true. But one of the things I find most difficult in maintenance programming is figuring out when null is expected and when it isn't - in the absence of developer documentation.

                    Kevin

                    1 Reply Last reply
                    0
                    • J jith iii

                      :) ...but I don't know, ToString() is quite popular here. It's coming repeatedly everywhere. May be,people Trim() only after they polish the null refernece with ToString()

                      O Offline
                      O Offline
                      ormonds
                      wrote on last edited by
                      #56

                      The most common error is that .NET software is written by people who get sidetracked onto software issues and never actually answer the question :laugh:

                      1 Reply Last reply
                      0
                      • C Colin Angus Mackay

                        jith - iii wrote:

                        . It will instantly give error if you try to set null to an integer,right? why double standard to string where you can set it to nothing but not otherwise?

                        There is no double standard. All reference types behave the same way. It is perfectly valid in many situations for a reference to be null. The compiler cannot catch logic errors, only syntax errors. System.Int32 is a value type. It is not valid ever for a value type to be null therefore the compiler can and will catch it for you.

                        Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

                        A Offline
                        A Offline
                        Adar Wesley
                        wrote on last edited by
                        #57

                        Colin Angus Mackay wrote:

                        There is no double standard. All reference types behave the same way. It is perfectly valid in many situations for a reference to be null. The compiler cannot catch logic errors, only syntax errors. System.Int32 is a value type. It is not valid ever for a value type to be null therefore the compiler can and will catch it for you.

                        Try this one:

                        int? value = null;
                        if (value == null) {
                            Console.WriteLine("Value is null");
                            Console.WriteLine("type int? is a: {0}", typeof(int?).IsValueType? "ValueType" : "RefType");
                        }
                        

                        int? is a value type and is equal to null... And anyway, there sure is a double standard. The double standard is that value types are teated differently then ref types. How about boxing? How about deriving from value types? Don't get me wrong. I am not saying it should be different. Just that there is a difference. --- Adar Wesley

                        C 1 Reply Last reply
                        0
                        • A Adar Wesley

                          Colin Angus Mackay wrote:

                          There is no double standard. All reference types behave the same way. It is perfectly valid in many situations for a reference to be null. The compiler cannot catch logic errors, only syntax errors. System.Int32 is a value type. It is not valid ever for a value type to be null therefore the compiler can and will catch it for you.

                          Try this one:

                          int? value = null;
                          if (value == null) {
                              Console.WriteLine("Value is null");
                              Console.WriteLine("type int? is a: {0}", typeof(int?).IsValueType? "ValueType" : "RefType");
                          }
                          

                          int? is a value type and is equal to null... And anyway, there sure is a double standard. The double standard is that value types are teated differently then ref types. How about boxing? How about deriving from value types? Don't get me wrong. I am not saying it should be different. Just that there is a difference. --- Adar Wesley

                          C Offline
                          C Offline
                          Colin Angus Mackay
                          wrote on last edited by
                          #58

                          Adar Wesley wrote:

                          int? is a value type and is equal to null...

                          True that int? is a value type True that int? equals null False that int? IS null. Value types still cannot BE null, even if they can EQUAL null. The two concepts are difference. int? is actually syntactic sugar for Nullable<int> which is also a value type. It has internally two fields, hasValue and value. It also overrides the Equals method which checks whether it hasValue or not, if not it compares with null. If you disassemble code that uses nullable types you'll see that it also does inline checks. For example, here is the C# program:

                          class Program
                          {
                              static void Main(string\[\] args)
                              {
                                  int? a = null;
                                  int? b = 2;
                          
                                  Console.WriteLine("A equals B? {0}", a==b);
                                  Console.ReadLine();
                              }
                          }
                          

                          And here is what Red Gate's Reflector made of it once the syntactic sugar was stripped away:

                          private static void Main(string[] args)
                          {
                          int? a = new int?();
                          int? b = new int?(2);
                          int? CS$0$0000 = a;
                          int? CS$0$0001 = b;
                          Console.WriteLine("A equals B? {0}",
                          (CS$0$0000.GetValueOrDefault() == CS$0$0001.GetValueOrDefault()) &&
                          (CS$0$0000.HasValue == CS$0$0001.HasValue));
                          Console.ReadLine();
                          }

                          If value types could truely really support being null then none of the above would be necessary.

                          Adar Wesley wrote:

                          And anyway, there sure is a double standard. The double standard is that value types are teated differently then ref types. How about boxing? How about deriving from value types?

                          That's like saying there is a double standard because the CEO of a company that gets a big salary, company car and better benefits than the stock room worker who is on minimum wage. They are doing different jobs.

                          Developer Day Scotland 2 - Free community conference Recent blog posts: *Throwing Exceptions *Training Developers * Method hiding or overriding - or the difference between new and virtual

                          1 Reply Last reply
                          0
                          • C chaiguy1337

                            Can you elaborate on that?

                            Sad but true: 4/3 of Americans have difficulty with simple fractions. There are 10 types of people in this world: those who understand binary and those who don't. {o,o}.oO( Check out my blog! ) |)””’)          http://pihole.org/ -”-”-

                            L Offline
                            L Offline
                            Lashdex
                            wrote on last edited by
                            #59

                            The function NZ he is referring to is from VB where it replaces a null value. This example returns the value of the variable unless is null then it returns the second parameter: Nz(someVar, "It is null!") It is a coalesce function just like COALESCE(par1, par2,[...]) in SQL. In C# you could use ?? like this: Console.WriteLine(foo.ToString() ?? "Aww, it is null");

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups