System.DateTime, Operator '-' e il VB.NET

In c# scrivo:

  DateTime d  = new DateTime(1976, 3, 15);
  TimeSpan t   = DateTime.Today - d;

e tutto funziona, poi traduco il tutto in VB.NET

  Dim d As System.DateTime = New DateTime(1976, 3, 15)
  Dim t As TimeSpan = System.DateTime.Today - d

e Operator '-' is not defined for types 'Date' and 'Date'.

Certo la soluzione è semplice: non è definito l'operatore di sottrazione per il tipo "Date".... ma perchè usa "Date" se io ho esplicitamente indicato "System.DateTime"? In ogni caso perchè per "Date" non trova l'overload dell'operatore se la documentazione dice "The equivalent .NET data type is System.DateTime", http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vadatDate.asp ?

Si è fatto tardi... domani - tempo permettendo - indagheremo :-p

posted @ venerdì 5 agosto 2005 03:20

Print

Comments on this entry:

# re: System.DateTime, Operator '-' e il VB.NET

Left by Marco De Sanctis at 05/08/2005 11:19
Gravatar
Perchè purtroppo VB.NET non supporta l'overload degli operatori

# re: System.DateTime, Operator '-' e il VB.NET

Left by Corrado Cavalli at 05/08/2005 12:25
Gravatar
Perche' VB sa che sei un (ex) Javista e sopratutto perche' VB bisogna saperlo usare :-)

Dim d As System.DateTime = New DateTime(1976, 3, 15)
Dim t As TimeSpan = DateTime.op_Subtraction(System.DateTime.Today, d)

In VS 2005 presumo che il tutto sia stato allineato... :-)

# re: System.DateTime, Operator '-' e il VB.NET

Left by M.rkino at 05/08/2005 12:29
Gravatar
ciao Corrado... hai/avete ragione.. si vede che VB.NET lo uso poco :-p Pensavo che il fatto che non supportasse l'overload degli operatori fosse solo un questione che non potesse definirne ma non che non potesse usare gli overload degli operatori definiti da altri.

In effeti ho provato questo in c#:

public class MyType
{
static public MyType operator + (MyType a, MyType b)
{
return new MyType();
}
}

consumato in VB.NET

Dim a As New MyType
Dim b As New MyType
Dim c As MyType = a + b

Ovviamente errore... :-p

# re: System.DateTime, Operator '-' e il VB.NET

Left by Adrian Florea at 05/08/2005 14:27
Gravatar
questione di CLS: "languages that do not provide operator overloading can still access the underlying functionality without the
addition of special syntax"

# re: System.DateTime, Operator '-' e il VB.NET

Left by M.rkino at 05/08/2005 15:12
Gravatar
Grazie Adrian, ora tutto mi torna! :-D
Comments have been closed on this topic.
«aprile»
domlunmarmergiovensab
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011