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 0.20

Print

Comments on this entry:

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

Left by Marco De Sanctis at 05/08/2005 8.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 9.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 9.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 11.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 12.12
Gravatar
Grazie Adrian, ora tutto mi torna! :-D

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

Left by Pierre Greborio at 05/08/2005 12.52
Gravatar
"Perche' VB sa che sei un (ex) Javista e sopratutto perche' VB bisogna saperlo usare :-)"

Markino, passi dalla padella alla brace ? Ma quando farai scelte serie ?

Your comment:



 (will not be displayed)


 
 
 
Please add 8 and 1 and type the answer here:
 

Live Comment Preview:

 
«febbraio»
domlunmarmergiovensab
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910