简体   繁体   English

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

[英]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. 我不太擅长VB,并且在解决某些问题时遇到了一些严重的问题。 For school I have to make a task and I have some test that all have to run True 对于学校,我必须完成一项任务,并且进行一些测试,所有测试都必须运行为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: 具有属性的placeToStay的示例:

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. 您可能已经猜到这根本不返回true。 How can I get the Till property from placeToStay1 if it's not given in a parameter? 如果未在参数中给定,如何从placeToStay1获取Till属性

The testcode must remain unchanged (the console.writeline lines) Any help is welcome :) 测试代码必须保持不变(console.writeline行),欢迎任何帮助:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果仅为了使参数起作用而创建对象时,将何时处置? - When will object be disposed if created only for parameter to function? 如何使用api获取inventoer中对象的属性 - how to get the properties of an object in inventoer using api 如何在PropertyGrid中显示静态(共享)对象的属性? - How to display static (shared) object's properties in a PropertyGrid? 如何使用反射和递归从嵌套对象获取属性? - How to get properties from nested object using reflection and recursion? 如果我遍历对象以获取所有属性,性能将如何? - How is performance going to be if I iterate through an object to get all the properties? .Net如何在父对象标记为只读时使属性变为只读 - .Net How to make properties readonly when parent object is marked as readonly “父”对象的实例如何使用其子对象的方法/属性? - How can an instance of a “parent” object use its child object's methods/properties? 如何从不带参数的函数中获取对调用表单的引用 - How to get reference to the Calling Form from function without parameter 如何获取另一个函数的返回值? - How to get another function's returned value? 添加Web服务引用时,我无法获取对象的所有属性 - When adding a web service reference, I can't get all properties of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM