locked
Puzzled Over Code Snippet RRS feed

  • Question

  • I've been looking at this code snippet trying to make sense of its purpose... Any ideas?

    	Public Overloads Overrides Function ToString() As String
    		Return String.Format("Min: {1}; Max: {2}; Flags: {3}", lngMin, lngMax, dpfFlags)
    	End Function

    Tuesday, March 20, 2012 11:28 PM

Answers

  • Every class inherits a method from System.Object named "ToString".  This method is used to turn the class into a string representation, which is used whenever you need to display an instance of your class as a string.

    In this case, the developer specifically said "When you look at my class as a string, I want it to show the Min + Max + Flags".  Without this, you'll just get the full name of the class.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, March 20, 2012 11:58 PM

All replies

  • It changes what happens when you call Object.ToString() on your class.  For example, if you use the object directly in a call to String.Format, you'll see the output string as printed.

    This also has the side effect of changing the display of the object in the debugger and other places, which is often why developers use this.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, March 20, 2012 11:34 PM
  • Not sure I understand what you're saying... Sorry.   This code snippet is part of a class that returns Memory, IRQ, DMA, or Port Range values.  Each class contains a similar function.  I'm just trying to figure out how it is useful.
    Tuesday, March 20, 2012 11:39 PM
  • Every class inherits a method from System.Object named "ToString".  This method is used to turn the class into a string representation, which is used whenever you need to display an instance of your class as a string.

    In this case, the developer specifically said "When you look at my class as a string, I want it to show the Min + Max + Flags".  Without this, you'll just get the full name of the class.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, March 20, 2012 11:58 PM
  • Ah, I got it now.  It says that he wants to see these values specifically instead of what would normally be shown, right?
    Wednesday, March 21, 2012 12:02 AM
  • Doing alot of Win32 API calls in my project and slowly losing my mind and brain cells.
    Wednesday, March 21, 2012 12:03 AM
  • Ah, I got it now.  It says that he wants to see these values specifically instead of what would normally be shown, right?

    Exactly - without this, you'd get something like:  "YourProject.YourNamespace.YourClass"  With this, you get "Min: 42; Max:86; Flags:Foo;"


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, March 21, 2012 12:33 AM