Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Tim

I am working on a VB6 -> .NET (C#) project and
I've been looking for a good reference for converting the built-in
function calls in VB6 to equivalent calls in C#.  I've stumbled across
some newsgroups but I find that people tend to argue over the correct
way to implement a specific function, so you end up finding 50
different ways to implement Val().  Do you know of an official
reference that would tell me how to convert my VB6 specific functions
into their equivalents.  Especially looking for conversions for: Val(),
IsDate(), IsNumeric(), IsArray, etc.

Tim
Tuesday, January 14, 2003

I'm relatively new to C# myself, but for many of those, the 'is' keyword would seem to replace all the IsX() functions (e.g., 'if (foo is SomeType)').  Val() could be replaced by the Parse() methods of System.Int32 and System.Double, etc.  Don't know of any official conversion guide, though.

Sam Gray
Tuesday, January 14, 2003

And remember you can reference "Microsoft.VisualBasic" and use all VB's functions from there - e.g.: Val() can be found in Microsoft.VisualBasic.Conversion.Val().

Although, as Sam mentions I'd prefer to use the "proper" framework mechanisms such as "int.Parse(string)" and "if(x is Array)" etc etc.

Duncan Smart
Tuesday, January 14, 2003

Here's one I just ran into:  what's the .NET equivalent of vbCrLf?  I use this one in string processing all the time & haven't quickly found it in online help.  (=

Sam Gray
Tuesday, January 14, 2003

Never mind, it's Environment.NewLine.  Google Groups is my friend.  (=

Sam Gray
Tuesday, January 14, 2003

or "\r\n" ...

Duncan Smart
Tuesday, January 14, 2003

Remember that the Parse functions throw exceptions if the string isn't in the correct format.  To avoid a try/catch block, you might want to check using "is" (and check for null) before trying Parse.

Monsur
Wednesday, January 15, 2003

Tim's original post asks for "converting the built-in
function calls in VB6 to equivalent calls in C#."

The answer to your question depends strongly on your definition of "equivalent".  For me, I'd define "equivalent" as "every input produces exactly the same output and side effects".

If what you're looking for is a "functional equivalent" -- ie, the same output is produced for only the set of input you care about, that's different.

If you care about the former then I would strongly counsel you to not try and roll your own C# equivalents to VB6 methods, particularly data coercion methods.  (I worked on many of the VBScript data coercion methods, I had the benefit of all the VB6 source code in front of me, and I still didn't get every case right.)

The VB.NET library is as close to "VB6 equivalent" as you're likely to get, and it's been debugged for you already.  I'd use it unless you have a really compelling reason not to.

Eric Lippert
Wednesday, January 15, 2003

Case in point, I just checked the implementation of the VB.NET "Val" method.  It's over 200 lines of reasonably dense VB.NET code.  So there is no "one-liner" C# equivalent, otherwise the VB.NET library would simply call it.

Eric Lippert
Wednesday, January 15, 2003

"And remember you can reference "Microsoft.VisualBasic" and use all VB's functions from there - e.g.: Val() can be found in Microsoft.VisualBasic.Conversion.Val().
"

For some reason, I'm only able to see Microsoft.VisualBasic.VBCodeProvider.  What am I doing wrong?

Tim
Wednesday, January 15, 2003

You need to reference the "Microsoft Visual Basic .NET Runtime" assembly in your project.  The Val() is located in Microsoft.VisualBasic.Conversion, vbCrLf is in Microsoft.VisualBasic.Constants

Tim
Wednesday, January 15, 2003

what are you trying to do?
i don't know much about vb, but i'd encourage strong typing. though C# is in the 'everything is an object' universe, so it's not strictly necessary.

there is a 'typeof' method (or similar) on every object in c#. so you can say the equivalent of if (obj.type() == int32.type()) . of course if it's an int64, it won't match.

also look at the 'convert' namespace or object.

(sorry, i've been working in C++ for the last month and forgot what i learned about C#)

mb
Saturday, January 18, 2003

*  Recent Topics

*  Fog Creek Home