views:

426

answers:

4

There is String.Format function that is referred to in the documentation as the analog for Format function from VB6. There's also Format function from VisualBasic namespace that is provided for compatibility and basically has same powers as String.Format.

Indeed, those two format dates and numbers.

But VB6's function was also able to format strings:

? format$("hi there", ">")
HI THERE
? format$("hI tHeRe", "<")
hi there
? format$("hi there", ">!@@@... not @@@@@")
HI ... not THERE

String.Format is not able to do that, as far as I'm concerned, nor is the new Format. I also couldn't find any mention in the compatibility Format documentation that certain parts of VB6 functionality is lost, seems like the feature was deprecated "silently."

Is there anything in the framework that can do this type of formatting?

+1  A: 

It's the Format function from the VisualBasic namespace that is supposed to be as close as possible to the Format function from VB 6, so it's there that you would possibly look for that kind of string formatting.

The String.Format method is developed independently from any language specific inheritance. Any features that it inherits from VB 6 (or other languages) is based on things like their usefulness and not to be backwards compatible.

Guffa
It's a pity the backwards compatibility philosophy used in developing Windows itself wasn't applied when VB.Net was being developed. http://blogs.msdn.com/oldnewthing/archive/2010/01/21/9951193.aspx
MarkJ
I have to disagree. In almost every case I can think of, every breaking change was a change for the better.
recursive
+1  A: 

This MSDN page seems to confirm that support was dropped from VB6 to VB.NET. You would have to implement it yourself, look around on the internet for some 3rd party code or (preferrably) rewrite the code to use String.Format and/or ToUpper/ToLower.

Your last example would be something like:

myString = String.Format("{0,-3}... not {1,-5}", "hi".ToUpper(), "there".ToUpper())

You could implement your own IFormatProvider to support uppercasing and lowercasing through format strings, but I'm not sure it's worth doing that.

Thorarin
Why, it is worth. The point is, those formats come from different places not all of which I control. And I must format strings according to them. And for some of such sources, I'm positive that they will not change format strings as they use VBA a lot (well, I do, too).
GSerg
Unfortunately I haven't found any equivalent implementation of the old Format function yet. Looks like you might have to do-it-yourself™ :P
Thorarin
+1. Although there's an implementation in a commercial migration tool. See my answer http://stackoverflow.com/questions/2116244/vb6-format-function-analog-in-net/2117144#2117144
MarkJ
+1  A: 

VBMigration.com have a commercial solution for migrating VB6 to VB.Net. Their website documents various differences in behaviour between VB6 Format and the VB.Net versions, including the ones in this question.

If you buy their VB Migration Partner tool, your VB6 Format code will be migrated to use a runtime library function Format6 which emulates the VB6 Format behaviour. I've never used their tool but it sounds great. If you have lots of code, I think it could save serious money.

MarkJ
Looks like a well thought-out tool. I'm almost sorry I don't have any VB6 code to convert :)
Thorarin
+2  A: 

Another solution to look at is to use the Microsoft.VisualBasic.Compatibility.VB6 namespace, which contains several classes and methods that are backwards compatible with Visual Basic 6. It's primarily meant for upgrade tools, but it will save you the hassle of having to purchase a migration tool or write the code yourself.

MSDN Documentation: Support.Format Method (Microsoft.VisualBasic.Compatibility.VB6)

The parameters don't change and it basically supports the same functionality at least given your examples:

Imports Microsoft.VisualBasic.Compatibility.VB6

Console.WriteLine("HI THERE ")
Console.WriteLine(Support.Format("hi there", ">"))

Console.WriteLine("hi there ")
Console.WriteLine(Support.Format("hI tHeRe", "<"))

Console.WriteLine("HI ... not THERE")
Console.WriteLine(Support.Format("hi there", ">!@@@... not @@@@@"))
Migration Specialist