SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post print_r implementation for .NET

    Code:
    //Copyright 2005 Mike Borozdin
     
    class Utils
     {
      public static string printR(int[] intArray)
      {
       string result = "int[]\n{";
       for (int i = 0; i < intArray.Length; i++) {
    			 result += "\t[" + i + "] = " + intArray[i] + "\n";
       }
       result += "}";
       return result;  
      }
      public static string printR(string[] stringArray)
      {
       string result = "string[]\n{";
       for (int i = 0; i < stringArray.Length; i++) {
    			 result += "\t[" + i + "] = \"" + stringArray[i] + "\"\n";
       }
       result += "}";
       return result;  
      }
      public static string printR(object[] objArray)
      {
       string result = "object[]\n{";
       for (int i = 0; i < objArray.Length; i++) {
    			 result += "\t[" + i + "] = " + objArray[i] + "\n";
       }
       result += "}";
       return result;  
      }
      public static string printR(ICollection ic)
      {
       string result = "Collection\n{";
       int i = 0;
       foreach (object obj in ic)  {
    	result += "\t[" + i + "] = " + obj + "\n";
    	i++;
       }
       result += "}";
       return result;
      }
      public static string printR(IDictionary ic)
      {
       string result = "IDictionary\n{";
       IDictionaryEnumerator ide = ic.GetEnumerator();
       while (ide.MoveNext()) {
    	result += "\t[\"" + ide.Key + "\"] = " + ide.Value + "\n";
       }
       result += "}";
       return result;
      }
     }

  2. #2
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sweeet.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wht not start the debugger, set a breakpoint, and use quick watch?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm.... that's an idea, but I noticed people were missing this function and it's not so hard to implement.

  5. #5
    SitePoint Enthusiast siteartwork's Avatar
    Join Date
    Jan 2005
    Location
    Germany
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I'm curious: What's the magic behind this? (I'm not a .NET Developer...)

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by siteartwork
    I'm curious: What's the magic behind this? (I'm not a .NET Developer...)
    It spits out name=value pairs in an array or collection I believe.

  7. #7
    SitePoint Enthusiast siteartwork's Avatar
    Join Date
    Jan 2005
    Location
    Germany
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    It spits out name=value pairs in an array or collection I believe.
    ah...figured it out...guess the substitute would be
    PHP Code:
    var_dump() 
    in PHP

    Didn't know that .NET devs need to implement such a thing in userland, that's why I was irritated at first

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    One need not implement it if you have VS.NET setup since you can watch/trace things. I will admit I have occasionally missed trusty old print_r().

    Only thing I would add is that a neater implementation woudl be using recursion & reflection to drill down into complex types, but that could be a real bear.

  9. #9
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by freddydoesphp
    wht not start the debugger, set a breakpoint, and use quick watch?
    The reason would moste likely be for instances where you cannot use that, such as when you have unhandled exceptions emailed to you from a live application. In such an event, it might be a good idea to collect as much data as possible.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  10. #10
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteartwork
    ah...figured it out...guess the substitute would be
    PHP Code:
    var_dump() 
    in PHP
    Lol, no actually the substitute is print_r(), much like the title of the thread suggests

    Quaint Tech
    - Blog on web development and web technology.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •