[vb.net] How do I make method().method() form in VisualBasic?

Soyoung Lee 21 Reputation points
2020-12-13T18:00:56.03+00:00

If I create my own API that can be used like method().method() ... like below, please let me know how to code using class and namespace.

Excel.Worksheets(sheetName).range("$A$27:$G100").Value

Namespace Excel

    Class Worksheets

         ????

        Class Range

               ????

        End Class


   End Class

End Namespace
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,366 Reputation points
    2020-12-13T22:57:19+00:00

    Hello @Soyoung Lee

    Looks like you can use the builder pattern which is a creational design pattern and can be used to create complex objects step by step as seen in the following Microsoft TechNet article code sample which has several more examples to get ideas from.

    Simple example

    Base class

    Public Class Burger  
        Public ReadOnly Property Size() As Integer  
        Public ReadOnly Property Cheese() As Boolean  
        Public ReadOnly Property Pepperoni() As Boolean  
        Public ReadOnly Property Lettuce() As Boolean  
        Public ReadOnly Property Tomato() As Boolean  
      
        Public Sub New(builder As Classes.BurgerBuilder)  
            Size = builder.Size  
            Cheese = builder.Cheese  
            Pepperoni = builder.Pepperoni  
            Lettuce = builder.Lettuce  
            Tomato = builder.Tomato  
        End Sub  
    End Class  
    

    Builder class

    Public Class BurgerBuilder  
        Public ReadOnly Property Size() As Integer  
      
        Private mCheese As Boolean  
        Public Property Cheese() As Boolean  
            Get  
                Return mCheese  
            End Get  
            Private Set(ByVal value As Boolean)  
                mCheese = value  
            End Set  
        End Property  
        Private mPepperoni As Boolean  
        Public Property Pepperoni() As Boolean  
            Get  
                Return mPepperoni  
            End Get  
            Private Set(ByVal value As Boolean)  
                mPepperoni = value  
            End Set  
        End Property  
        Private mLettuce As Boolean  
        Public Property Lettuce() As Boolean  
            Get  
                Return mLettuce  
            End Get  
            Private Set(ByVal value As Boolean)  
                mLettuce = value  
            End Set  
        End Property  
        Private mTomato As Boolean  
        Public Property Tomato() As Boolean  
            Get  
                Return mTomato  
            End Get  
            Private Set(ByVal value As Boolean)  
                mTomato = value  
            End Set  
        End Property  
      
        Public Sub New(ByVal size As Integer)  
            Me.Size = size  
        End Sub  
      
        Public Function AddPepperoni() As BurgerBuilder  
            Pepperoni = True  
            Return Me  
        End Function  
      
        Public Function AddLettuce() As BurgerBuilder  
            Lettuce = True  
            Return Me  
        End Function  
      
        Public Function AddCheese() As BurgerBuilder  
            Cheese = True  
            Return Me  
        End Function  
      
        Public Function AddTomato() As BurgerBuilder  
            Tomato = True  
            Return Me  
        End Function  
      
        Public Function Build() As Burger  
            Return New Burger(Me)  
        End Function  
    End Class  
    

    Usage

    Dim burger = New Classes.BurgerBuilder(14).  
            AddPepperoni().  
            AddLettuce().  
            AddTomato().  
            Build()  
    

    In the above we can also specify the type

    Dim burger As Burger = New Classes.BurgerBuilder(14).  
            AddPepperoni().  
            AddLettuce().  
            AddTomato().  
            Build()  
    

    Results

    47645-a1.png

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 114.2K Reputation points
    2020-12-13T18:21:01.967+00:00

    Try something like this:

    Module Excel
    
        Public ReadOnly Worksheets As New AllWorksheets
    
    
        Class AllWorksheets
    
            Default Public ReadOnly Property Item(name As String) As Worksheet
                Get
                    Return New Worksheet
                End Get
            End Property
    
        End Class
    
    
        Class Worksheet
    
            Public ReadOnly Property Range(address As String) As Range
                Get
                    Return New Range
                End Get
            End Property
    
        End Class
    
    
        Class Range
    
            Public Property Value As String ' or 'As Object'
                Get
                    Return "some text"
                End Get
                Set(value As String)
    
                End Set
            End Property
    
        End Class
    
    End Module
    
    0 comments No comments