VB6 Format function: analog in .NET

安稳与你 提交于 2019-12-19 21:54:04

问题


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:


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 @@@@@"))



回答2:


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.




回答3:


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.




回答4:


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.




回答5:


Can possible use

Dim value As Double

value = 1.2
TextBox1.Text = (value.ToString("0.00",System.Globalization.CultureInfo.InvariantCulture))

more info http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#Y3200



来源:https://stackoverflow.com/questions/2116244/vb6-format-function-analog-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!