Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert STR() to VB.Net 2

Status
Not open for further replies.

PatMcLaughlin

Programmer
Dec 10, 2010
97
US
Hi Guys... I am working on a project, converting a foxpro program to VB.NET. I have conquered all except the data sort on 2 reports. The foxpro script looks like this
Code:
   CASE mSORT=1
      INDEX ON DEPT + STR(MSORTN,3) + MSORTA + MODEL FOR QTY > 0 TO DLDSOP
   CASE mSORT=2
      INDEX ON DEPT + STR(PSORTN,3) + PSORTA FOR QTY > 0 TO DLDSOP      
   CASE mSORT=3
      INDEX ON DEPT + STR(PSORTN,3) + PSORTA + STR(MSORTN,3) + MSORTA FOR QTY > 0 TO DLDSOP       
   Case mSort=4
      INDEX ON DEPT+MH_LINE+DTOC(MH_DATE)+STR(SEQ_NO)+OPT+PART FOR QTY > 0 TO DLDSOP
   Case mSort=5
      INDEX ON DEPT+MH_LINE+ STR(MSORTN,3) + MSORTA+Model+Serial+DTOC(MH_DATE)+OPT+PART FOR QTY > 0 TO DLDSOP
   *OTHERWISE
   *   INDEX ON DEPT+MH_LINE+DTOC(MH_DATE)+STR(SEQ_NO)+OPT+PART FOR QTY > 0 TO DLDSOP
My VB.net code looks like this...
Code:
        If rdoModel.Checked = True Then
            Mpp3DV.Sort = "mSortN, mSorta, Model"
        ElseIf rdoPart.Checked = True Then
            Mpp3DV.Sort = "psortn, psorta"
        ElseIf rdoPartModel.Checked = True Then
            Mpp3DV.Sort = "psortn, psorta, msortn, msorta"
        ElseIf rdoLineSeq.Checked = True Then
            Mpp3DV.Sort = "mh_line, seq_no, opt, part"
        ElseIf rdoLineModel.Checked = True Then
            Mpp3DV.Sort = "mh_line, msortn, msorta, model, serial, opt, part"
        ElseIf rdoLineSeqOptPart.Checked = True Then
            Mpp3DV.Sort = "mh_line, seq_no, opt, part"
        Else
The mSortn and pSortn are numeric fields with up to 3 digits
The mSorta and pSorta are alpha fields

Here is my problem, The sorts Almost match. I believe my problem may be the STR() in the foxpro which I do not understand the syntax. (Why the ',3') Can you help me out?
 
Correction to mSortn and pSortn fields, they are fields with numeric data formatted as string.
 
You are right that STR() converts from numeric to a character string. The "3" in this case signifies the number of characters in the resulting string. But be careful. If there numeric contains decimal places, the number of characters will include the decimal point.

So:

[tt]STR(123, 3) --> "123"
STR(1, 3) --> "1" preceded by two spaces
STR(123.45, 6) --> "123.45"
STR(123.45, 8) --? "123.45" preceded by two spaces[/tt]

Hope that makes sense.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Sorry. My mistake. You need to add another parameter if the numeric contains decimals:

[tt]STR(123, 3) --> "123"
STR(1, 3) --> "1" preceded by two spaces
STR(123.45, 6, 2) --> "123.45"
STR(123.45, 8, 2) --? "123.45" preceded by two spaces[/tt]

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike for the quick reply... The data contains no decimals and if I am reading this correctly, I still see no changes to the sort.
 
Pat, it's difficult to answer this without knowing what's in the various fields.

You asked specifically about the STR() function. [tt]STR(MSORTN,3)[/tt] will give you a character field, 3 characters wide. Does that match the MSortN field in the VB version? If it does, you should be OK. Except that you seem to have lost the DEPT field somewhere along the line.

Also, it would help to know exactly what it going wrong. You say the "sorts almost match". In what way do they not match?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Perhaps a case of differing sort order would help find out what's wrong.

STR(field,3) seems unneded, if the data is alread a right aligned numeric string. If your data is not right aligned that may explain the differing sort.

If your data alread is C(3), then it seems you made changes in your database, as STR() is not needed for a string field at all, it even will error, eg STR("2 ",3) results in a type error.

Bye, Olaf.

 
The dept fields were eliminated as the report data is assembled for only 1 department each time so they would always be the same in the data. The mSortn fields are numeric (changed to string) and a maximum of 3 characters wide. By Almost Match I mean that the Foxpro version will put out data like...
ABC100A
ABC105A
ABC105B

The VB.net will put it in this order...
ABC100A
ABC105B
ABC105A

The data is all correct and present, it just differs slightly in the sort. I was grabbing at any difference I could find.
 
It's still unclear what is combined to ABC105B or which parts of ABC105B come from which fields. If the number 105 in it comes from one of the fields added by STR(field,3) in VFP, then there is nothing wrong about that, but obviously the sort order fails on the next field containing A or B. Sure there is no Unicode in the play, or some unprintable char? VB, even VB6 does not use ANSI, AFAIK, but UTF16 encoding.

Bye, Olaf.

 
Based on your returned sort data, it appears to me that the issue probably lies in your VB.NET code and not in your conversion from FP.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
This is still a bit confusing. Are you now saying the problem is not in the conversion of your numeric to a string, but rather in the actual sorting of the data?

I suggest you try the following:

First, reduce the problem to its simplest state. Get rid of the CASE statement in VFP, and the nested IF / ELSEIF in VB. Focus on a single case that produces the error.

Then show us exactly what is in each of the relevant fields. And show us the result of the sort, in both the VFP and VB versions.

Doing that might give us the information we need to diagnose the problem.

By the way, I see you have a dangling Else clause in your VB code. That's probably not relevant - you presumably didn't paste all the code into your message - but I thought I'd mention it anyway.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Found it! When double checking the data to verify right aligned for Olaf, I stumbled on to the answer. Foxpro would sort like this...
1
2
3
4
10

VB.Net sorts the same data as ...
1
10
2
3
4

By adding zeros to the left to force a 3 character field, my sort is correct.
 
In fact, that's not a VFP vs. VB difference. It simply reflects the fact that, in one case you are sorting on the number, and in the other case on the string. The same would happen with that data regardless of the language.

But, still, it's good that you solved it. Be sure to come back if you have any more issues.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
> The same would happen with that data regardless of the language.
Well, only if you have left aligned strings, but STR(1,3) is " 1" not "1 ", and thus sorts correctly vs two and three digit numbers.

The error was in the conversion of the data, obviously, as you said your new table fields are char(3) fields, you didn't convert the numeric VFP data right aligned but left aligned.

Glad you spotted it and I could help.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top