Visualizzazione dei risultati da 1 a 9 su 9
  1. #1

    Trasformazione codice da VB a C#

    Ho dei problemi a tradurre questa funzione da VB a C#

    codice VB:

    Public Function setFormatDate(ByVal data As String)

    If Len(data) = 8 Then
    Return "20" & Right(data, 2) & "/" & Mid(data, 4, 2) & "/" & Left(data, 2)
    ElseIf Len(data) = 10 Then
    Return Right(data, 4) & "/" & Mid(data, 4, 2) & "/" & Left(data, 2)
    Else
    Throw New Exception("La data " & data & " da formattare ha un formato non riconosciuto")
    End If

    End Function


    per ora ho fatto:

    protected string setFormatDate(string data)
    {
    try
    {
    if (lenght(data)==8)
    {
    return "20" + Right(data, 2) + "/" + Mid(data, 4, 2) + "/" + Left(data, 2);
    }

    else if (lenght(data)==10)
    {
    return Right(data, 4) + "/" + Mid(data, 4, 2) + "/" + Left(data, 2);
    }
    }

    catch (Exception exc)
    {
    string debug = "La data " & data & " da formattare ha un formato non riconosciuto " + exc.Message;
    return null;
    }
    }


    ma i comandi Mid, left, right, lenght non vanno bene...

    qualcuno mi può aiutare??

    Grazie
    Iceberg

  2. #2
    Semplicemente perché quei "comandi" non esistono in C#.

    Per far prima aggiungi queste funzioni:
    codice:
    void Left(string txt, int length) {
    return txt.Substring(0, length);
    }
    
    void Right(string txt, int length) {
    return txt.Substring(txt.Length-length, length);
    }
    
    void Mid(string txt, int start, int length) {
    return txt.Substring(start, length);
    /*
    se il carattere di partenza non è zero-based allora
    devi aggiungere 1 a start:
    return txt.Substring(start++, length);
    */
    }
    Ora, io le ho scritte al volo.. andrebbero riviste un attimino tanto per aggiungere qualche controllo sulla lunghezza, caratteri di partenza ecc..

    Vedi poi tu

  3. #3
    Utente di HTML.it L'avatar di mcp07
    Registrato dal
    Apr 2004
    Messaggi
    174

    Re: Trasformazione codice da VB a C#

    Originariamente inviato da iceberg
    Ho dei problemi a tradurre questa funzione da VB a C#

    codice VB:

    Public Function setFormatDate(ByVal data As String)

    If Len(data) = 8 Then
    Return "20" & Right(data, 2) & "/" & Mid(data, 4, 2) & "/" & Left(data, 2)
    ElseIf Len(data) = 10 Then
    Return Right(data, 4) & "/" & Mid(data, 4, 2) & "/" & Left(data, 2)
    Else
    Throw New Exception("La data " & data & " da formattare ha un formato non riconosciuto")
    End If

    End Function


    per ora ho fatto:

    protected string setFormatDate(string data)
    {
    try
    {
    if (lenght(data)==8)
    {
    return "20" + Right(data, 2) + "/" + Mid(data, 4, 2) + "/" + Left(data, 2);
    }

    else if (lenght(data)==10)
    {
    return Right(data, 4) + "/" + Mid(data, 4, 2) + "/" + Left(data, 2);
    }
    }

    catch (Exception exc)
    {
    string debug = "La data " & data & " da formattare ha un formato non riconosciuto " + exc.Message;
    return null;
    }
    }


    ma i comandi Mid, left, right, lenght non vanno bene...

    qualcuno mi può aiutare??

    Grazie

    prova ad andare qui
    Gran Duca del Fancazzismo, con Pieni Poteri per la Sezione Toscana del Clan, e Comandante Supremo delle Forze Aeree

    Non Esistono Cose Impossibile, ma Solamente la Nostra Limitata Percezione di Ciò che è Possibile!
    Ciao a tutti belle e rutti :maLOL:

  4. #4
    Grazie ad entrambi...
    Iceberg

  5. #5
    Originariamente inviato da ZofM
    Semplicemente perché quei "comandi" non esistono in C#.

    Per far prima aggiungi queste funzioni:
    codice:
    void Left(string txt, int length) {
    return txt.Substring(0, length);
    }
    
    void Right(string txt, int length) {
    return txt.Substring(txt.Length-length, length);
    }
    
    void Mid(string txt, int start, int length) {
    return txt.Substring(start, length);
    /*
    se il carattere di partenza non è zero-based allora
    devi aggiungere 1 a start:
    return txt.Substring(start++, length);
    */
    }


    protected string setFormatDate(string data)
    {

    string DataUK;

    try
    {
    if (lenght(data)==8)
    {
    DataUK = "20" + Right(data, 2) + "/" + Mid(data, 4, 2) + "/" + Left(data, 2);
    return DataUK;
    }

    else if (lenght(data)==10)
    {
    DataUK = Right(data, 4) + "/" + Mid(data, 4, 2) + "/" + Left(data, 2);
    return DataUK;
    }
    }

    catch (Exception exc)
    {
    string debug = "La data " + data + " da formattare ha un formato non riconosciuto " + exc.Message;
    return null;
    }
    }


    sto cercando di seguire la tua soluzione ma ho alcuni problemi:

    1) mi manca la funzione lenght...
    2) perchè questo mi da errore in:
    "20" + Right(data, 2)
    Right(data, 4) + "/"

    e in tutti i returno delle funzioni
    return txt.Substring(0, length);
    return txt.Substring(txt.Length-length, length);
    return txt.Substring(start, length);


    Grazie per l'aiuto
    Iceberg

  6. #6
    Utente di HTML.it L'avatar di Legnetto
    Registrato dal
    May 2002
    Messaggi
    1,419
    Prova questa:
    codice:
    public object setFormatDate(string data)
    {
     object obj1;
    string[] textArray1;
    if (Strings.Len(data) == 8)
    {
     textArray1 = new string[6];
    textArray1[0] = "20";
    textArray1[1] = Strings.Right(data, 2);
    textArray1[2] = "/";
    textArray1[3] = Strings.Mid(data, 4, 2);
    textArray1[4] = "/";
    textArray1[5] = Strings.Left(data, 2);
    return string.Concat(textArray1);
     
    }
    else
    {
     if (Strings.Len(data) == 10)
    {
     textArray1 = new string[5];
    textArray1[0] = Strings.Right(data, 4);
    textArray1[1] = "/";
    textArray1[2] = Strings.Mid(data, 4, 2);
    textArray1[3] = "/";
    textArray1[4] = Strings.Left(data, 2);
    obj1 = string.Concat(textArray1);
    return obj1;
    goto Label_00C0;
     
    }
    throw new Exception(string.Concat("La data ", data, " da formattare ha un formato non riconosciuto"));
     
    }
     
    Label_00C0:
     return obj1;
     
    }
    L'ho fatta convertire da vb a c# da un programmino...(quindi nessuna garanzia)
    Ciao
    Legnetto

  7. #7
    Ottengo errore in tutte le Strings...

    Iceberg

  8. #8
    Utente di HTML.it L'avatar di Legnetto
    Registrato dal
    May 2002
    Messaggi
    1,419
    Originariamente inviato da Legnetto
    ...(quindi nessuna garanzia)
    Mi spiace ma non so aiutarti...
    Ciao

  9. #9
    grazie ugualmente
    Iceberg

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.