2010-11-01 13 views
7
  1. Qualcuno sa di un buon riferimento per le stringhe di formato VB6?
  2. Qualcuno sa di un convertitore da stringhe di formattazione VB6 alle stringhe .NET?

Sto lavorando al porting di una grande base di codice VB6 su .NET. Si tratta di una parte di software basata su database e il database contiene stringhe di formato VB6 che vengono successivamente caricate e utilizzate per visualizzare altri dati nel database.Esiste un modo per convertire a livello di codice le stringhe di formattazione VB6 in stringhe di formattazione .NET?

La mia domanda, come this article, è come effettuare il porting. Tuttavia, la risposta scelta per quella domanda non è adeguata alle mie esigenze. Mi sento a disagio nel fare affidamento su librerie specificamente progettate per la retrocompatibilità con un linguaggio che sono stato specificamente assunto per portare via.

+0

I formati contenuti nel database sono un set noto? In tal caso, potrebbe essere meglio scrivere solo script per eseguire semplici aggiornamenti del database su quel sottoinsieme piuttosto che trovare uno strumento. Dovrai comunque ricontrollare i risultati. –

+0

:-D Vorrei. Ne ho già pensato completamente, ma non è un insieme noto (anche se la maggior parte di essi * è * nullo). Sfortunatamente ha anche il rovescio della medaglia nel senso che i nostri migliaia di utenti potrebbero rimanere bloccati nel limbo ... ci sono dei modi per aggirarlo, ma una ricerca e una sostituzione non funzionerebbero davvero, no. – Crisfole

risposta

14

La routine di formattazione utilizzata da VB6 è effettivamente incorporata nel sistema operativo. Oleaut32.dll, la funzione VarFormat(). È in giro da 15 anni e resterà per sempre, considerando quanto il codice si basi su di esso. Cercare di tradurre le stringhe di formattazione in una stringa di formattazione .NET composita è un'attività disperata. Basta usare la funzione OS.

Ecco un esempio di programma che fa questo, utilizzando gli esempi dal filo collegato:

using System; 
using System.Runtime.InteropServices; 

class Program { 
    static void Main(string[] args) { 
     Console.WriteLine(Vb6Format("hi there", ">")); 
     Console.WriteLine(Vb6Format("hI tHeRe", "<")); 
     Console.WriteLine(Vb6Format("hi there", ">[email protected]@@... not @@@@@")); 
     Console.ReadLine(); 
    } 

    public static string Vb6Format(object expr, string format) { 
     string result; 
     int hr = VarFormat(ref expr, format, 0, 0, 0, out result); 
     if (hr != 0) throw new COMException("Format error", hr); 
     return result; 
    } 
    [DllImport("oleaut32.dll", CharSet = CharSet.Unicode)] 
    private static extern int VarFormat(ref object expr, string format, int firstDay, int firstWeek, int flags, 
     [MarshalAs(UnmanagedType.BStr)] out string result); 
} 
+0

Hans, sei un salvatore. Grazie per lo sfondo. Non ho realizzato la storia; era sotto l'impressione che fosse una graffetta VB6. – Crisfole

+0

Semplice e Sly, mi piace, +1 – smirkingman

0

La soluzione migliore potrebbe essere quella di scrivere autonomamente la libreria di conversione, ma probabilmente non è stata la risposta che si sperava di ottenere.

+0

Non avevo davvero le mie speranze in alto ... è per questo che ho anche chiesto una risorsa stringa di formato VB6, perché ho pensato che avrei dovuto riscriverlo. – Crisfole

0

Ecco alcuni codice F # che traduce la maggior parte dei pre-definiti e personalizzati VB6 in stile numerici e stringhe di formato data a qualcosa adatto per String.Format. È facilmente chiamato da C# o VB, naturalmente.

open System 

module VB6Format = 

    /// Converts a VB6-style format string to something suitable for String.Format() 
    let Convert(vb6Format) = 
     if String.IsNullOrWhiteSpace(vb6Format) then "{0}" else 
     match if vb6Format.Length > 1 then vb6Format.ToUpperInvariant() else vb6Format with 
     // PREDEFINED NUMERIC: http://msdn.microsoft.com/en-us/library/y006s0cz(v=vs.71).aspx 
     | "GENERAL NUMBER" | "G"  -> "{0:G}" 
     | "g"       -> "{0:g}" 
     | "CURRENCY" | "C" | "c"  -> "{0:C}" 
     | "FIXED" | "F"    -> "{0:F}" 
     | "f"       -> "{0:f}" 
     | "STANDARD" | "N" | "n"  -> "{0:N}" 
     | "PERCENT" | "P" | "p"  -> "{0:P}" 
     | "SCIENTIFIC"     -> "{0:E2}" 
     | "E" | "e"     -> "{0:E6}" 
     | "D" | "d"     -> "{0:D}" 
     | "X" | "x"     -> "{0:X}" 
     | "YES/NO" | "ON/OFF"   // we can't support these 
     | "TRUE/FALSE"     -> "{0}" 
     // PREDEFINED DATE/TIME: http://msdn.microsoft.com/en-us/library/362btx8f(v=VS.71).aspx 
     | "GENERAL DATE" | "G"   -> "{0:G}" 
     | "LONG DATE" | "D"   -> "{0:D}" 
     | "MEDIUM DATE" 
     | "SHORT DATE" | "d"   -> "{0:d}" 
     | "LONG TIME" | "T"   -> "{0:T}" 
     | "MEDIUM TIME" 
     | "SHORT TIME" | "t"   -> "{0:t}" 
     | "M" | "m"     -> "{0:M}" 
     | "R" | "r"     -> "{0:R}" 
     | "s"       -> "{0:s}" 
     | "u"       -> "{0:u}" 
     | "U"       -> "{0:U}" 
     | "Y" | "y"     -> "{0:Y}" 
     // USER-DEFINED: http://msdn.microsoft.com/en-us/library/4fb56f4y(v=vs.71).aspx 
     //    http://msdn.microsoft.com/en-us/library/73ctwf33(v=VS.71).aspx 
     // The user-defined format strings translate more-or-less exactly, so we're just going to use them. 
     | _       -> sprintf "{0:%s}" vb6Format 
Problemi correlati