如何在函数不在参数中时获取对象的属性

时间:2014-02-22 11:41:56

标签: vb.net visual-studio-2012

我还不擅长VB,而且我有一些严重的问题需要修复。 对于学校我必须完成一项任务,我有一些测试,所有必须运行

    Console.WriteLine(placeToStay1.Overlapswith(placeToStay2) = False)     'True     
    Console.WriteLine(placeToStay1.Overlapswith(placeToStay3) = False)     'True
    Console.WriteLine(placeToStay1.Overlapswith(placeToStay4) = True)      'True   
    Console.WriteLine(placeToStay1.Overlapswith(placeToStay5) = True)      'True
    Console.WriteLine(placeToStay1.Overlapswith(placeToStay6) = False)     'True
    Console.WriteLine(placeToStay1.Overlapswith(placeToStay7) = False)     'True

具有属性的placeToStay示例:

Dim placeToStay1 As New placeToStay
placeToStay1.Room = 123           'String
placeToStay1.From = #10/23/2013#  'Date
placeToStay1.Till = #10/28/2013#  'Date

这就是我班上的内容:

Public Class placeToStay
    Public Property Room As String
    Public Property From As Date
    Public Property Till As Date

    Dim _tillDate As Date = Till
    Public Function OverlapsWith(date2 As placeToStay) As Boolean
        Dim TheBool As Boolean
        If _tillDate > date2.From Then
            TheBool = True
        Else
            TheBool = False
        End If
        Console.WriteLine(_tillDate)
        Return TheBool 
    End Function

End Class 

正如您可能已经猜到的那样,这根本不会回归真实。 如果没有在参数中给出,如何从placeToStay1获取 Till属性

测试代码必须保持不变(console.writeline行) 欢迎任何帮助:)

1 个答案:

答案 0 :(得分:1)

如果From / Till日期为1,我认为两个实例“重叠”,是否属于另一个的起始日期?

如果是,请尝试类似:

Public Class placeToStay

    Public Property Room As String
    Public Property From As Date
    Public Property Till As Date

    Public Function OverlapsWith(ByVal pts As placeToStay) As Boolean
        Return (pts.From >= Me.From AndAlso pts.From <= Me.Till) OrElse
            (pts.Till >= Me.From AndAlso pts.Till <= Me.Till)
    End Function

End Class