简体   繁体   中英

how to get properties of an object in a function when it's not in the parameter

I'm not that good at VB yet and I have some serious issues fixing something. For school I have to make a task and I have some test that all have to run True

    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

An example of a placeToStay with the properties:

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

This is what I have in my class:

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 

As you might have guessed this doesn't return true at all. How can I get the Till property from placeToStay1 if it's not given in a parameter?

The testcode must remain unchanged (the console.writeline lines) Any help is welcome :)

I assume two instances "overlap" if the From/Till date of one, falls within the From/Till date of the other?

If yes, then try something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM