C# foibles from an (ex) VB.NET programmer

  • Thread starter Thread starter Stano
  • Start date Start date
S

Stano

I've finally taken the plunge and started coding all my apps in C#
from VB.NET (I couldn't wait any more for XML documentation in VB -
thank you NDoc for getting me my life back). All in all I've found it
a pretty smooth ride, but one thing is bugging me. In VB.NET I can use
the following declaration:

Includes My.Namespace.SomeClass

But in C# if I use the corresponding

using My.Namespace.SomeClass;

I get an error.

I have written quite a lot of static (shared) helper libraries that
act as part of the language, is there any way I can do this in C#? For
example in VB the following code:

DumpXML(objXML)

would have to be written in C#

SomeClass.DumpXML(objXML);

I know this is minor but it really is bugging me.

Thanks,

Paul Stancer
 
I don't believe C# supports this. You can shorten or change the class name
using an alias (using <alias> = <className>), but thats about it. I know it
can be a little troublesome, but in the end your code will likely be far
easier to understand and maintain because your static methods are clearly
associated with a given type.
 
That's partially correct. You can alias a namespace or a class in the
header section, or you can declare a 'used' namespace, but you can't declare
a 'used' class.

IOW, you can do this

using My.NameSpace;
using SC = My.NameSpace.SomeClass;

---
allows
public class AClass
{
public AClass()
{
int x = SC.HelperFunction(); // as opposed to
//int x = My.NameSpace.SomeClass.HelperFunction();
}

}


but not

using My.NameSpace.SomeClass; //error

Just look at the header of a winform for an example.

Ray
 
Ray Beckett said:
That's partially correct. You can alias a namespace or a class in the
header section, or you can declare a 'used' namespace, but you can't declare
a 'used' class.

Which is what I said.
 
Thanks for the feedback. I'd looked at the aliasing, it's the same as
in VB, not a perfect solution but it's one I'm using now. I still
prefer the ability to use a shortcut to a class. Never mind, you can't
have everything.

as for PowerToys I'm a cheapskate and didn't wan't to fork out a
couple of hundred dollars for the sake of a few curly brackets. Also I
was sick of converting code examples to VB.NET, when I wanted to use a
new technique, as most of them seem to be in C# these days.

Thanks for the help.

Stano.
 
I am with you on having to convert VB.NET samples to C#, but I thought that
PowerToys were free (I think it is different from other commercial add-ins),
but maybe I missed something.
 
I hadn't spotted that one. It's looks good, and might be useful for
some of my legacy code that's written in VB. Last time I looked at XML
comments for VB.NET it was using a commercial package and cost about
$200.

Thanks again,

Paul Stancer
 
There are a few freebies out there until Whidbey is released. Check
GotDotNet.com for an XML comment generation add-in for VB.
People who know me know that I can't stand XML documentation :-) Good idea,
bad implementation. With today's kind of tools, you'd think there would be a
way to keep the comments separate from the code, and yet visible according
to context (after all, we DO have neat docking window tools). My biggest
frustration is that GOOD doc comments are far more than normal comments that
developers need to look at in code. Good Doc comments produce the kind of
pages you see in a good MSDN page. That much comment just obscures the code
and makes the code pages 10s of times bigger than they need to be. My last
suggestion was that somebody come up with a context-sensitive (within code)
docking editing tool that writes the comments to a preferred and
configurable medium (you could choose central DB, local file, inline, etc.).

As for putting class names in the using statment (imports), that's purly a
VB thing. It helps conform to the old global instance classes you could
create to expand the function library, so it looked like you had new
embedded functions. I actually kinda like that feature, but it's not in C#.
If you read the C# developers' blogs, they explain that they didn't feel it
was a necessary feature, and furthermore saw potential for confusion when it
came to name collisions. Whether any particular person agrees with that
assessment or not is a matter of personal choice i guess.

-Rob Teixeira [MVP]
 
Back
Top