3

I am trying to port a VB function to VB.NET, but I cannot get the function to work correctly and update properly.

rFormat = Format(Format(Value, fmt), String$(Len(fmt), "@"))

It seems like the problem lies with the String$() function parameter which is used to align decimal points of values. How would I be able to properly fix this, or is there another way to achieve this?

EDIT

The following is an example console application that shows the issues that I am having.

Imports Microsoft.VisualBasic
Module Module1

Sub Main()
    Dim rFormat As String
    Dim fmt As String
    Dim value As Object

    fmt = "########.000"
    value = 12345.2451212
    'value = 12345
    '~~~~~~~~~~~~~~~~~~~~~

    'rFormat = Microsoft.VisualBasic.Format(Microsoft.VisualBasic.Format(value, fmt), "".PadLeft(fmt.Length, "@"c))
    'Console.WriteLine(rFormat) ' <<Not working prints all "@" for any value!>>>

    'rFormat = Microsoft.VisualBasic.Format(Microsoft.VisualBasic.Format(value, fmt), "".PadLeft(fmt.Length))
    'Console.WriteLine(rFormat) '<<Not working prints nothing>>

    'rFormat = (String.Format(value, fmt)).PadLeft(Len(fmt))
    'Console.WriteLine(rFormat) ' <<Not working prints the value 12345.2451212>>> should print>>>>> 12345.245
    'for integer values< works good>

    rFormat = String.Format("{0," + fmt.Length.ToString + "}", String.Format(value, fmt))
    Console.WriteLine(rFormat) ' <<Not working prints the value 12345.2451212>>> should print>>>>> 12345.245
    'for integer values< works good>


End Sub

End Module
Matthew
  • 3,976
  • 15
  • 66
  • 130
  • I'm not sure how tagging VB6 helps you. Sort of like asking an English speaker how to say something in Elbonian. – Bob77 Aug 05 '12 at 00:37
  • 1
    for those of us with rusty (or nonexistent) VB6 skills can you let us know what format you're trying to achieve and we can tackle it that way. – Jim O'Neil Aug 05 '12 at 00:41
  • Here's the documentation for @ in VB6 Format function http://msdn.microsoft.com/en-us/library/aa263413(v=vs.60) – MarkJ Aug 05 '12 at 08:50
  • possible duplicate of [Is there a way to programmatically convert VB6 Formatting strings to .NET Formatting strings?](http://stackoverflow.com/questions/4072490/is-there-a-way-to-programmatically-convert-vb6-formatting-strings-to-net-format) – Hans Passant Aug 06 '12 at 00:35
  • I tagged VB6 so that maybe someone whose had the same issues with updating to VB.NET might be of some assistance. @Jim, I added an edit to my original post to include more detail regarding the format issues I am trying to resolve, thanks for your suggestion! – Matthew Aug 06 '12 at 00:35

3 Answers3

3

All String$ does is repeat the character specified in the second parameter the number of times specified in the first parameter.

So if fmt is, for example "9999", then the String$ command will produce "@@@@".

You can replace this with the String.PadLeft method and continue to use the VB Format function from the Microsoft.VisualBasic namespace:

    rFormat = Microsoft.VisualBasic.Format(Microsoft.VisualBasic.Format(value, fmt), "".PadLeft(fmt.Length, "@"c))

EDIT:

Based on the edit in the question, the correct format logic should be:

    rFormat = String.Format("{0:" & fmt & "}", value)

It is very helpful to review the String.Format documentation since it has a lot of examples and explanation.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • Thanks for your answer. I was strictly trying to update to VB.NET. I updated my post above to include more detail as to the errors I am having. Would you have any idea how to fix these? All of my VB6 code works, but my VB.NET code does not. – Matthew Aug 06 '12 at 00:34
  • Ok, that helps to have a better understanding of what you are looking for. I have updated the answer with some code that will produce the desired output. – competent_tech Aug 06 '12 at 16:18
1

It sounds like you're wanting to pad out your results so they are a fixed length. How about using the String.PadLeft Method or the String.PadLeft(int32,char) Method to Pad out rFormat.

Something like this for spaces:

rFormat = (String.Format(value, fmt)).PadLeft(Len(fmt))

Edit

Boy is it hard to find VB6 documentation online. It appears that the @ in a VB6 Custom Format has to do with String justification per this Forum posting and this SO answer they suggest something something like this.

rFormat = String.Format("{0," + fmt.Length.ToString + "}", String.Format(value, fmt))

This is using the Composite Formatting Alignment Component

Alignment Component

The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.


The main issue that I see in your updated example is that you are using an object to store your Double. By changing values declaration to a Decimal and changing the format function I was able to get it to work.

Sub Main()
    Dim rFormat As String
    Dim fmt As String
    Dim value As Double

    fmt = "#######0.000"
    value = 12345.2451212



    rFormat = String.Format("{0," + fmt.Length.ToString + "}", value.ToString(fmt))
    Console.WriteLine(rFormat) 
    Console.ReadLine()
End Sub

enter image description here

Community
  • 1
  • 1
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • Here's the documentation for @ in VB6 Format function http://msdn.microsoft.com/en-us/library/aa263413(v=vs.60) – MarkJ Aug 05 '12 at 08:50
  • Thanks @Mark, your solution worked for me when using integer values, although when using double values I am having issues. I added an edit to my original post which should explain the problems I am having. – Matthew Aug 06 '12 at 00:30
  • @Matthew See my edit, part of the problem is that you are using an object. I also modified the conversion Format. see if it works for you. – Mark Hall Aug 06 '12 at 01:37
0

In VBNet, you can also do this:

Dim rFormat As String = String.Empty
Dim fmt As String = "########.000"
Dim value As Object = 12345.2451212

rFormat = (CDbl(value)).ToString(fmt)
Ambrose
  • 501
  • 3
  • 13