VB’s *My* for C# Developers


Juval Lowy showed me an amusing code sample at TechEd – “That”. What is “That” you say? “That” is “My” for C#. Confused yet? Good.


As Duncan Mackenzie explains in this MSDN Magazine article “My” is a new feature in Visual Basic 2005 that is a speed-dial into the .NET Framework. Using “My”, you can write code like this:


Dim contents As String
contents = My.Computer.FileSystem.ReadAllText(“c:\mytextfile.txt”
)

Instead of this:


Dim contents As String
contents = IO.File.ReadAllText(“c:\mytextfile.txt”)


The challenge with the latter sample is that if you’re brand new to the .NET Framework or never done file IO with .NET Framework 2.0, you wouldn’t know that the File class was in the IO namespace and that it has a ReadAllText method. “My” gives you the shortcut without having to know what namespace or class its under. A few C# developers have been asking for the same functionality in C#, so Juval created a “My” wrapper in .NET that is a static class named “That”. The naming convention being that when VB refers to “itself” it uses the keyword “Me” and when C# refers to itself it “this”, then logically when referring to something else, VB will use “My” and C# will use “That”. VB being “Me” and “My”, C# being “this” and “That”.


Here’s the IntelliSense screenshot of the “That” class


Drilling into Application information….


Drilling into Computer.Network 



Here’s some C# code you can use with “That”


bool test = That.Computer.Network.Ping(@www.microsoft.com);
That.Application.Log.WriteException(new Exception(“foo”));
long workingSet = That.Application.Info.WorkingSet;


So what’s missing?
To be clear, “That” is not everything that’s in “My”, notably the Settings and Webservices sections are gone. That being said (no pun intended), the code is freely available so someone can probably work their magic and do some work to dynamically code gen settings/webservices or scan the setting/webservices proxy classes for changes to make those available.


How’s it implemented?
Well you should download That for yourself from iDesign, but the “That” class is, as you might expect, a wrapper for the VB “My” classes.


What do you think? Good idea, bad idea, or just clever naming?


Comments (61)

  1. The whole ‘My’ concept is nonsense, IMHO. People should better learn the REAL framework namespaces & classes.

  2. James Foster says:

    How about using the keyword "Our", to show that C# isn’t as possessive as VB.NET is? Every married person knows that Our.Computer is better than saying My.Computer.

  3. Senior Software Engineer says:

    That’s ridiculous. C# should support ‘My’, in exactly the same way as VB.

    It’s these gratuitous incompatabilities that give us uncertainty about Microsoft’s commitment to VB.

  4. Ricky Dhatt says:

    Sorry OT: I just realized, won’t "My" keyword be odd in Longhorn, where it has been reported that the "My" prefix has been eliminated?

  5. Tom says:

    We programmers are not idiots (most of the time, anyway). Therefore, we don’t need features that dumb down the programming environment.

    As was already pointed out, take some time to learn the library. Programming is not a 5-minute endeavor. It is an art and a science, and takes time to learn and get good at.

  6. Chris Nahr says:

    Ugh. Are you deliberately trying to drive programmers towards C++/CLI? 🙁

    About the only good reason I can think of to adopt this VB hellspawn is a future redesign of the .NET Framework that puts My/That/Our classes in other namespaces. But of course it would be better to avoid such a redesign in the first place.

    Programmers should learn their craft, and being able to search a standard library (especially given the comfortable MSDN Library software) is a very basic part of that craft. Kiddie wheels like "My" are exactly what gave VB its "for dummies" image. Keep "that" away from our beautiful curly-braced C#!

  7. KevinT says:

    I know the popular route is to take a swip at VB developers, but personally, I don’t have a problem with My/That, in fact I think it is a good thing. It is far closer to proper OO than using the normal namespace. Ie, the sample you give:

    My.Computer.FileSystem.ReadAllText("c:mytextfile.txt")

    vs

    IO.File.ReadAllText("c:mytextfile.txt")

    In the first example you are thinking in the problem space, in the second you are thinking in the machine space. OO is better served in the former…

  8. C#ではThat(VB新機能のMy に対するwrapper)

  9. Ryan Heath says:

    Hmm,

    wouldnt a (more-or-less)

    using That = MS.VB.My;

    be sufficient? 😉

    // Ryan

  10. zzz says:

    I’ll just quote the comments I agree with..

    > The whole ‘My’ concept is nonsense, IMHO. People should better learn the REAL framework namespaces & classes.

    True..

    > It’s these gratuitous incompatabilities that give us uncertainty about Microsoft’s commitment to VB.

    Doesn’t importing the VB assembly solve this..

    >wouldnt a (more-or-less) using That = MS.VB.My; be sufficient? 😉

    If not, I’d like to hear why.. What I am missing here?

    > I don’t have a problem with My/That, in fact I think it is a good thing. It is far closer to proper OO than using the normal namespace

    Well I guess then you’d kind of expect that the Computer can be any computer you’ve authorized with also.. Over network? And perform all kinds of remote operations through that instance? Dunno.

    > Kiddie wheels like "My" are exactly what gave VB its "for dummies" image. Keep "that" away from our beautiful curly-braced C#!

    However if this was something more thorough and integrate with stuff like WinFS and so on then it might be more interesting..

  11. Sergio Pereira says:

    I see some comments of people thinking that adding shortcuts to the language is dumbing down itself or calling the programmers idiots. I have to disagree with that. We have to realize that the people that use the language (and VB in special, given its history) are not always professional programmers.

    The more the language avoids making itself accessible, the less likely it will be chosen for the quick tasks, which are a hallmark of VB development (or have been).

  12. kbiel says:

    Here’s the problem with little helpers like this:

    1) You are now required to have yet another assembly referenced for this to work and it does not add to the program from the user prospective. Adding code that bloats the program and slows the process only to benefit the programmer’s laziness is huge no-no in my book.

    2) It insulates novice programmers from learning the API.

    3) Some day they will learn the API as they progress on to writing more complicated programs, but now that they know how to use the System.IO namespace (and others), they are stuck with lines upon lines of legacy code that has the helper in it.

    4) What happens tomorrow when MS adds another namespace under IO for the new WinFS (or some other similar scenario)? Do you keep expanding your little helper object? How big will you allow it to get? At what point is it just a renaming and reorganizing of MS’s entire System namespace?

    It would be different if it provided functionality for common tasks that surround a given namespace. For example, if you provided a function that automated the chain of stream resources that usually need to be created when attempting to encrypt a string so that now the developer only needs to call "string EncryptString(string rawString)". But just renaming the given namespace just for the sake of convenience, well, that is what the "using" keyword is for.

  13. Shital Shah says:

    This is sure funny. I guess VB implementation of My is pure mess. Look at all the modules and classes it generates. I would be reluctant to use it even in VB. I strongly believe designers at MS should have tried to come up with normal .Net library which can provides such shortcuts and be usable across languages. It would be then less fency but would have been usable to many more devs out there. Plus time that was burned in doing this fency thing that hardly anyone would care to use could have been better used by VB team to do refactoring functionality. But I’m sure you guys have your reasons…

  14. Leif Ashley says:

    I think this implementation of "that" is a good idea. 🙂

    While I usually don’t like 2 paths to get a job done, and learning the framework is a good idea as well, the MSDN documentation is horrible. Anything that points me in the right direction is a bonus I say.

    The beauty of this is that I can just start typing, and somewhat track down what I’m looking for, such as Ping and Log.

  15. Andrew Hallock says:

    kbiel said:

    Some day they will learn the API as they progress on to writing more complicated programs, but now that they know how to use the System.IO namespace (and others), they are stuck with lines upon lines of legacy code that has the helper in it.

    Yes, exactly! I’ve used the pre-generated Page_Load method in aspx code behinds for months, until I realized, what’s wrong with overriding OnLoad and calling the base OnLoad? Automation is definitely one good part of writing software, but not when it adds bloat.

  16. >wouldnt a (more-or-less)

    >

    >using That = MS.VB.My;

    >

    >be sufficient? 😉

    That’s what I was wondering…why the need for a wrapper? Can’t you just us the VB "My" assembly directly? It may not be quite as nice, but it’s gotta beat having a separate wrapper.

  17. What the?

    No.

    No no no no no!

    Why?

    I mean, honestly, why?  The “My” namespace is…

  18. Yann Schwartz says:

    Oh my Dog, something useful will taint our beautiful, manly, curly-braced language. "My" is violating our revered OO paradigm, but having one thousand catch-all static functions (oops – methods) on the poor System.IO.File class is pure OO bliss. Yeah, right.

    I, for one, would have liked some VB features creeping in C#, like its elegant way of dealing with late binding. But I forgot, this is pure evil too (but the OO zealots have recently discovered dynamically typed languages, all hope is not lost).

    "My" is one of the most elegant features of VB .Net 2.0

  19. Chris says:

    This is just a new namespace for WinFX concept and some link to existing classes. This article is an introduction to something new for me.

    I think the namespace’s name is badly choosen, but it’s my point of view. I would rather call it "System.Windows.WinFX".

    I simply hope "my" is not introducing something like variant to "simplify" coding… I gotta try it with my brand new Beta2 DVD.

    Considering the difference between C# and VB, I can not understand why there is so much differences, this just pre-MSIL!

  20. krhorrocks says:

    bloat

  21. Mike's Dump says:

    I was blogging the other day (Microsoft takes the ‘my’ out of computing) that I thought the whole…

  22. Gonzo says:

    this isn’t somehting new

    other people have either done this or at least started; see this guys stuff http://www.brokenkeyboards.com/brokenkeyboards/blogs.aspx?blogid=112

    don’t agree with all he says though

  23. Michael Chroman says:

    A software engineer will learn the language/s he/she chooses to work with. He will learn as much as he can about the language. He will use the language properly. He will NOT resort to some foolish shortcuts(ME, OH MY, etc..) just to appease the gods of another language. IF vb wants that, cool, Let vb have that. This is c# and should not be tainted with vb. I like to use the Me and My when I am programming in vb but when using c#, I like the language just like it is.

    Leave it alone.

  24. Stephen says:

    I don’t have any problem with developers encapsulating functionality in a way that makes sense to them. If I had to call directly into the windows API every time I wanted to create a button I would start to become very grumpy.

    When teaching programming we used to "scaffold" students like this, giving them nice classes which simplified the interfaces to common tasks and gave them a bit of a leg up, so to speak.

    However, we were always careful to bring them back down to earth later on. There is nothing more disheartening than to have a past student send you an email like:

    Hi, I have just started my new job but they don’t have the ::insert name of convenient scaffolding library here::. What do I do?

  25. Mike says:

    Awhile back, I posted to a discussion board (probably the one on asp.net) asking if you can use the "My" feature in C#. I think I was told by someone that you could just import the namespace, and use it just as you would from VB…but apparently that is not the case. This is unfortunate. I think it’s an excellent idea to be able to reach different resources on a computer in a more abstract and easy way. I have not had the need to access a collection of printers yet, but I don’t know how to do it off the top of my head. I’d probably have to start with looking into how to retrieve it using WMI (not exactly fun). In my opinion, this "My" concept should be a native feature of the .net API for all languages to use.

  26. Sergey Osypchuk says:

    I could see a lot of efforts in .NET 2.0 to "simplify" life for starter programmers. It just helps to build simple applications but not complex system. It make more complex to support code reuse. Could developer in any way customize how file is opened when using ‘My’?

    And what is worse, all such simple thing becomes industrial standards. [I think that worse example is Windows.Forms editor. Have you ever tried to change type of buttons in form?]

    And when you start to build complex system first thing you faced that you should prove to team members that practices which are called new could not be used because it not compatible with lot concepts of code reuse.

    Sometimes seems that Microsoft wants to make users from most of programmers and I do not like this.

    Regarding ‘My’, ‘That’ – I think it may be interesting for some of customers (hard to write about them ‘developers’) but I would like not to see such feature in Framework.

    http://dotnet.osypchuk.com

    Personal dotNet Experience blog

  27. Ninputer says:

    C# do have some VB’s My feature. Every project have a ‘Properties’ namespace and contain the things much like VB’s My.Settings and My.Resources.

  28. What the?

    No.

    No no no no no!

    Why?

    I mean, honestly, why?  The “My” namespace is silly enough,…

  29. Jan! says:

    Wouldn’t it have been more logical to use "This" and "Its", like "Me" and "My"? "That" is not possessive.

  30. Enrique says:

    I think "that" is confusing Microsoft must preserve the semantic My.

  31. steve potter says:

    All of you purists that have belittled the "my" feature are missing the point. The motivation for it is to make the job easier for a novice who doesn’t have the time or patience to understand and learn the framework. If you think it’s stupid then it’s not for you. Plus if you are such a purist you probably won’t even be programming in VB.NET, which is why they left it out of C#.

    It’s like the "auto-shift" feature on some cars. To me it’s a silly idea…either use a stick or enjoy the convenience of an automatic. But certain people like it.

    Point is, .NET wasn’t just created for you

  32. Shine says:

    Someone’s been going through Brian’s trash

  33. js@mailinator.com says:

    The amazing thing is this. If developers don’t like it don’t use it. Also if we are to "the REAL framework namespaces & classes" then we should get away from .Net and go back to assembly or even better machine language. What a novel concept. Let’s all be REAL programmers and program in machine language.

    Some people are under great time constraints and have to get things done under budget with a limited budget. But I guess there are many companies out there with tons of resources and money to throw at projects with no time constrains what so ever.

    Again let’s all go back to machine language. After all we don’t want to make thing any easier.

  34. Big Daddy says:

    THIS IS STUPID!!  IF all of the software engineers that develop in c#, c++, etc, need this "My" crap, go for it.  I believe a true engineer knows his craft.  Who the heck needs a dumbed down language?  VB is good for non engineers which is why it was designed.  However, to most of that don’t care about vb, this is just another way the vb people are trying to shove this crap down our throat.  If vb died tomorrow, I would not be sad!!!  I myself am tired of dealing with the vb vs c,c++,c#, java arguement.

  35. The pronouns we use when we address computers and imagine them addressing us hides some profound insight. I haven’t pinned down exactly what.

  36. MikeJ says:

    Anything that makes a developer’s life more productive and the code more readable is to be encouraged.

    VB.NET has an advantage over C# here particularly for those new to the .net framework. Anyway you can always not use the My or That namespace, thats your choice.

    But it has to be implemented in all .net languages otherwise you may have portability problems. Its a good high-level abstraction in OO terms.

    So please implement it in future releases/updates to C#

  37. Chui says:

    I think it was Steve McConnell who noted that blacksmiths don’t store their tools away organised by the type of tools, but by the job. I.e. hammers and tongs might be stored together since they work hand-in-hand.

    Similarly, the justification behind the My namespace is commonly used functions and tools should be in an easily accessible namespace. Witness the power of Python scripts, where functions like open(), print() are in the default namespace.

  38. George says:

    If the "My" namespace is "dumbing" down the programming environment, then what is intellisense doing?  By that horrific logic, we shouldn’t even have intellisense.

    The My namespace saves time, keeps you from adding an outrageous number of using statements and keeps you from having absurdly long calls to properties (if you don’t add using statements left and right).

    I use C# exclusively, but you VB "haters" need to get over yourselves and stop being blinded by your baseless hatred of vb.net.  Its all the same framework ppl…sorry VB.net has a nice way of accessing otherwise buried information.

  39. Nchantim says:

    But "My" actually exposes things that are different or otherwise not directly accessible through the framework (at least that I’ve found). Send a file to the recycle bin using IO namespace, anyone?

    If it’s easier to read, that is not "dumbing down", it’s actually smarter, because it makes my code more understandable to others on my dev team, and thus we’re all more productive.

  40. How Do we Add My class in my c# solution?

  41. if you use "My" in C# …

    Add references Microsoft.VisualBasic

    And…

    C# Code..

    Microsoft.VisualBasic.Devices.Computer My =new Microsoft.VisualBasic.Devices.Computer();

           private void Form1_Load(object sender, EventArgs e)

           {

               this.txtServerName.Text =My.Name;

           }

  42. SpeedyDev says:

    I like it.  I like a balance between power and rapid development.  Save me the trouble.  I’d much rather traverse the intellisense than dig through MSDN for a solution.

  43. itay says:

    Don’t you think it is much better just to make a lot of snippet codes, instead of my?

    that way the programmer also learn.

  44. SerCey says:

    Special Thanks for C# on That Using 😉

  45. Gishu says:

    You’re a lifesaver… was just trying to make sense of a Office programming HowTo video that uses VB. I need to use C#.

    My o Why?

  46. BrunoT says:

    In regards to the first comment. How is My any different then jQuery’s $() function? Its a quickdial into the most useful functionality. I would love to see this added to C#. I doubt you could get this one by Anders. I hope they do keep out XML literals though.

  47. Appyk says:

    What’s wrong in ‘My’?

    And what’s wrong in ‘Our’..?

    Why C sharp programmers always wanna keep Vb small..?

    After all both of them using the same CLR,FCL,JIT..?

    There are n numbers of features in Vb which C Sharp doesn’t support

    What wrong if a language tries to make developers life easier..?

    Can anyone tell me how to convert the following code snippet into C Sharp without errors..?

    Dim TextBox1 As New TextBox

    Dim obj = New Object

    TextBox1.Text = "Here"

    obj = TextBox1

    MsgBox(obj.text)

  48. JasonJShuler says:

    If you are going to claim that simplifying common tasks is bad because it shields programmers from the "right way," then why are you even using .NET?