VB a C # Funzioni
-
19-09-2019 - |
Domanda
che sono l'equivalente dei seguenti operatori di VB.Net a C #?
- UBound ()
- LBound ()
- IsNothing ()
- Chr ()
- Len ()
- UCase ()
- LCase ()
- Sinistra ()
- destro ()
- RTrim ()
- LTrim ()
- Trim ()
- Mid ()
- Sostituire ()
- Split ()
- Join ()
- MsgBox ()
- IIF ()
Soluzione
VB C#
UBound() = yourArray.GetUpperBound(0) or yourArray.Length for one-dimesional arrays
LBound() = yourArray.GetLowerBound(0)
IsNothing() = Object.ReferenceEquals(obj,null)
Chr() = Convert.ToChar()
Len() = "string".Length
UCase() = "string".ToUpper()
LCase() = "string".ToLower()
Left() = "string".Substring(0, length)
Right() = "string".Substring("string".Length - desiredLength)
RTrim() = "string".TrimEnd()
LTrim() = "string".TrimStart()
Trim() = "string".Trim()
Mid() = "string".Substring(start, length)
Replace() = "string".Replace()
Split() = "string".Split()
Join() = String.Join()
MsgBox() = MessageBox.Show()
IIF() = (boolean_condition ? "true" : "false")
Note
-
yourArray.GetUpperBound(0)
vsyourArray.Length
: se la matrice è di lunghezza zero, GetUpperBound tornerà -1, mentre intera tornerà 0.UBound()
in VB.NET tornerà -1 per array di lunghezza zero .
- Le funzioni stringa VB utilizza un indice basato uno, mentre il metodo NET utilizza un indice in base zero. Cioè
Mid("asdf",2,2)
corrisponde a"asdf".SubString(1,2)
. -
?
non è l'esatto equivalente diIIf
perchéIIf
valuta sempre sia argomenti, e?
valuta solo quello di cui ha bisogno. Questo potrebbe importa se ci sono effetti collaterali della valutazione ~ brivido! - le molte funzioni VB String classici, tra cui
Len()
,UCase()
,LCase()
,Right()
,RTrim()
, eTrim()
, tratterà un argomento diNothing
(Null
in C #) come equivalente a una stringa di lunghezza zero. Esecuzione di metodi stringa suNothing
, naturalmente, un'eccezione. - Si può anche passare
Nothing
alle classiche funzioni di VBMid()
eReplace()
. Invece di un'eccezione, questi tornerannoNothing
.
Altri suggerimenti
UBound() "array".Length
LBound()
IsNothing(): "object" == null
Chr() (char)"N"
Len() "string".Length
UCase() "string".ToUpper()
LCase() "string".ToLower()
Left() "string".Substring(from, to)
Right() "string".Substring(from, to)
RTrim() "string".TrimEnd()
LTrim() "string".TrimStart()
Trim() "string".Trim()
Mid() "string".Substring(from, to)
Replace() "string".Replace()
Split() "string".Split()
Join() String.Join()
MsgBox() MessageBox.Show()
IIF() validate ? iftrue : iffalse;
Tutte queste funzioni sono metodi membro della classe Microsoft.VisualBasic.Information
, nell'assemblea Microsoft.VisualBasic
, in modo da poterli utilizzare direttamente. Tuttavia, la maggior parte di loro hanno C # equivalenti, o equivalenti specifiche non di lingua in classi del framework .NET di base:
- UBound ():
Array.GetUpperBound
- LBound ():
Array.GetLowerBound
- IsNothing ():
== null
- Chr ():
(char)intValue
(getto) - Len ():
String.Length
- UCase ():
String.ToUpper
- LCase ():
String.ToLower
- Sinistra (), Destra () e Mid ():
String.Substring
(con argomenti diversi) - RTrim ():
String.TrimEnd
- LTrim ():
String.TrimStart
- Trim ():
String.Trim
- Replace ():
String.Replace
- Split ():
String.Split
- Join ():
String.Join
- MsgBox ():
MessageBox.Show
- IIF ():
condition ? valueIfTrue : valueIfFalse
(operatore condizionale)
Link
La maggior parte di questi sarebbe metodi di istanza per l'oggetto stringa che restituisce la stringa modificata.
MsgBox vs. MessageBox.Show(..)
IIF vs. (expression?returnValueIfTrue:returnValueElse)
>> IIf(test, trueval, falseval)
(test ? trueval : falseval);
>> IsNothing(obj)
(obj == null);
>> UCase(str)
str.ToUpper();
>> LCase(str)
str.ToLower();
Prima di tutto, la maggior parte di coloro che non sono operatori. Sono le funzioni e le funzioni sono inclusi solo in VB.Net per ragioni di compatibilità. Ciò significa che non li usare in VB.net entrambi, e di utilizzare invece gli equivalenti forniti dalla nuova API.
- UBound () -
arrayVar.Length
- LBound () - obsoleto, limite inferiore è sempre 0 in una normale matrice Net
- IsNothing () - obsoleta. Utilizzare
Is Nothing
in VB.Net e== null
in C # - Chr () -
Convert.ToChar()
o(char)someVar
- Len () -
stringVar.Length
utilizzare questo in VB troppo - UCase () -
stringVar.ToUpper()
utilizzare questo in VB troppo - LCase () -
stringVar.ToLower()
utilizzare questo in VB troppo - Sinistra () -
stringVar.Substring(0, n)
utilizzare questo in VB troppo - destro () -
stringVar.Substring(stringVar.Length - n)
utilizzare questo in VB troppo - RTrim () -
stringVar.TrimEnd()
utilizzare questo in VB troppo - LTrim () -
stringVar.TrimStart()
utilizzare questo in VB troppo - Trim () -
stringVar.Trim()
utilizzare questo in VB troppo - Mid () -
stringVar.Substring(n, m)
utilizzare questo in VB troppo - replace () -
stringVar.Replace()
utilizzare questo in VB troppo - Split () -
stringVar.Split()
utilizzare questo in VB troppo - Join () -
String.Join()
utilizzare questo in VB troppo - MsgBox () -
MessageBox.Show()
- IIF () -
(condition) ? truepart : falsepart
- "?" Notare che ci sono alcune differenze, perché è un operatore e non una funzione
Troverete la conversione per molte di queste funzioni questa pagina wikipedia .
Credo che alcuni di questi Mid()
simili sono ancora disponibili in .NET Framework nello spazio dei nomi Microsoft.VisualBasic, che si può ancora fare riferimento dal codice C #.
Un altro ...
VB - IsDBNull (valore)
C # - yourdatarow.IsNull ( "columnName")
Se si guarda su MSDN si vede che la maggior parte del tempo ci sono codice di esempio per entrambe le lingue.
- UBound () -> se x è un array di stringa [] per esempio: x.GetUpperBound ();
- LBound () -> se x è un array di stringa [] per esempio: x.GetLowerBound ();
- IsNothing () -> if (x == null)
- Chr () -> char x = (char) 65;
- Len () -> x.length ();
- UCase () -> assumere x è una stringa: x.ToUpper ();
- LCase () -> assumere x è una stringa: x.ToLower ();
- Sinistra () -> assumere x è una stringa: x.Substring (0, 10); // primi 10 caratteri
- destro () -> assumere x è una stringa: x.Substring (x.length - 10); // ultimi 10 caratteri
- RTrim () -> x.TrimEnd ();
- LTrim () -> x.TrimStart ();
- Trim () -> x.Trim ();
- Mid () -> assumere x è una stringa: x.Substring ()
- replace () -> assumere x è una stringa: x.Replace ();
- Split () -> assumere x è una stringa: x.Split ();
- Registrazione () -> String.Join ();
- MsgBox () -> MessageBox.Show ();
- IIF () -> operatore ternario (x == true vero valore:? Falso-value);
Un altro oltre a questo potrebbe essere IndexOf () per Trova stringa all'interno della stringa
Un esempio qui sotto ...
string MainString = "String Manipulation";
string SearchString = "pul";
int FirstChr = MainString.IndexOf(SearchString);
//SHOWS START POSITION OF STRING
MessageBox.Show("Found at : " + FirstChr );
In aggiunta alle risposte di cui sopra. Fate attenzione con la sostituzione Len () -> x.length. VB Len () consente di passare nulla, ma in C # si otterrà un'eccezione. A volte sarebbe meglio usare String.IsNullrEmpty () (Se la situazione lo consente)
La funzione di spazio non è presente l'elenco di tutti gli altri:
Space(16) -> new String(" ", 16)