locked
Why Properties can initialize and return more than one values? RRS feed

  • Question

  • User-1976290245 posted

    Hi, have a look at this code

    Public Class PropertiesTest
    
        Dim num1 As Integer
        Dim num2 As Integer
    
        Public Property Assign() As Integer
            Get
                Return num1
                Return num2
            End Get
            Set(ByVal value As Integer)
                num1 = value
                num2 = value
    
            End Set
        End Property
    End Class


    Here Assign property is initializing and returning more than one values, but without any use?

    Since, when i create object of this class , it is returning only one value. So what is the use of allowing  such type of initialization and return statement? 

    Friday, May 14, 2010 3:30 AM

Answers

  • User-1802908944 posted

    becuase when you use Return command. it will not execute next line after Return statement.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 6:29 AM
  • User-952121411 posted

    Ok the SET statement is completely legit.  You could theoretically assign as many variables as you want in the SET as .NET doesn't know which variable is assigned as the single backing variable. 

    In the GET, it is interesting that the IDE does not at least give a warning like "Not all code paths will be hit" or something along those lines, because as soon as the 1st Return statement is hit, execution will jump to the End Get and return the single value as a Property should.  You obviously get a warning in the IDE if you have no Return statement at all, so I think it ensures you have at least (1), and then gives no further warnings.  This is actually not specific to Properties.  Try placing (2) Return statements in a Function.  The same result occurs.

    The power of multiple Return statements is when logic is wrapped around your code.  Look at the following modified version of your code which does indeed use both Return statements:

    Public Class PropertiesTest   
      
        Dim num1 As Integer   
        Dim num2 As Integer = 5  'Static assignment based on business rule   
      
        Public Property Assign() As Integer   
            Get   
                If num1 > num2 Then
                 Return num1   
                Else
                  Return num2
                End If
            End Get   
            Set(ByVal value As Integer)   
    
                num1 = value     
            End Set   
        End Property   
    End Class  

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 10:36 AM

All replies

  • User-1802908944 posted

    becuase your public property's return type is Integer

    Public Property Assign() As Integer 

    It will return only one value.

    this is not need of such type of initialization



     

    Friday, May 14, 2010 6:19 AM
  • User-1802908944 posted

    becuase when you use Return command. it will not execute next line after Return statement.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 6:29 AM
  • User-1976290245 posted

    Ok, i removed As Integer , but still it is returning only one value.

    My query is why .NET framework is not giving error at compile time that there are two return statement. And one is of no use.

    Thanks 

    Friday, May 14, 2010 6:35 AM
  • User-952121411 posted

    Ok the SET statement is completely legit.  You could theoretically assign as many variables as you want in the SET as .NET doesn't know which variable is assigned as the single backing variable. 

    In the GET, it is interesting that the IDE does not at least give a warning like "Not all code paths will be hit" or something along those lines, because as soon as the 1st Return statement is hit, execution will jump to the End Get and return the single value as a Property should.  You obviously get a warning in the IDE if you have no Return statement at all, so I think it ensures you have at least (1), and then gives no further warnings.  This is actually not specific to Properties.  Try placing (2) Return statements in a Function.  The same result occurs.

    The power of multiple Return statements is when logic is wrapped around your code.  Look at the following modified version of your code which does indeed use both Return statements:

    Public Class PropertiesTest   
      
        Dim num1 As Integer   
        Dim num2 As Integer = 5  'Static assignment based on business rule   
      
        Public Property Assign() As Integer   
            Get   
                If num1 > num2 Then
                 Return num1   
                Else
                  Return num2
                End If
            End Get   
            Set(ByVal value As Integer)   
    
                num1 = value     
            End Set   
        End Property   
    End Class  

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 14, 2010 10:36 AM
  • User-1976290245 posted

    The power of multiple Return statements is when logic is wrapped around your code.
     

     

    Thanks a lot

    Saturday, May 15, 2010 7:01 AM