visual videos net desde descargar curso codigofacilito cero vb.net vb.net-2010

videos - ¿Cómo configuro un DateTime que se puede anular para anular en VB.NET?



visual.net 2010 (5)

El problema es que primero examina el lado derecho de esta tarea y decide que es del tipo DateTime ( ? no ? ). Luego realizando la tarea.

Esto funcionará:

Dim fromDate As DateTime? = If(fromDatePicker.Checked, _ fromDatePicker.Value, _ CType(Nothing, DateTime?))

¿Porque obliga al tipo del lado derecho a ser DateTime? .

Como dije en mi comentario, Nothing puede ser más parecido al default(T) C # default(T) lugar de null :

Nothing representa el valor predeterminado de un tipo de datos. El valor predeterminado depende de si la variable es de un tipo de valor o de un tipo de referencia.

Estoy intentando configurar un filtro de rango de fechas en mi UI, con casillas de verificación para decir si se debe usar el valor de DateTimePicker, por ej.

Dim fromDate As DateTime? = If(fromDatePicker.Checked, fromDatePicker.Value, Nothing)

Sin embargo, establecer fromDate to Nothing no da como resultado que se establezca en Nothing sino en ''12: 00: 00 AM '', y la siguiente instrucción If ejecuta el filtro incorrectamente porque startDate no es Nothing .

If (Not startDate Is Nothing) Then list = list.Where(Function(i) i.InvDate.Value >= startDate.Value) End If

¿Cómo realmente me startDate que startDate obtenga un valor de Nothing ?


Usa una New Date como un número mágico:

Dim fromDate As New Date If fromDatePicker.Checked Then fromDate = fromDatePicker.Value End If If fromDate <> New Date Then list = list.Where(Function(i) i.InvDate.Value >= fromDate.Value) End If


squery = "insert into tblTest values(''" & Me.txtCode.Text & "'', @Date)" Dim cmd = New SqlCommand(squery, con) cmd.Parameters.Add("@Date", SqlDbType.DateTime) If txtRequireDate.Text = "" Then cmd.Parameters("@Date").Value = DBNull.Value Else cmd.Parameters("@Date").Value = txtRequireDate.Text End If


Además de la respuesta correcta de @Damien_The_Unbeliever, ¿con New DateTime? también funciona:

Dim fromDate As DateTime? = If(fromDatePicker.Checked, _ fromDatePicker.Value, _ New DateTime?)

Puede encontrar que parece un poco contra intuitivo, ¿por qué tal vez el CType(Nothing, DateTime?) Es preferible.


Con VB.NET y EF 6.X para guardar null es:

Dim nullableData As New Nullable(Of Date)