0

I like to use Objective-c style of createObject instead of the normal new.

Basically it make it easier for me to just create a basic object and then initialize the variable. Sometimes when I want to create an object of derived class, I simply call a class that initiate the base class.

Also sometimes, often in fact, I want to do some things first before initializing say, the base class, or most of the basic variables.

Something like this

 Public Shared Function createOrders(amount As String, base As String, quote As String, exchange As ExchangesClass, time As String, price As String, id As String, sellinginsteadofbuying As String, identifyingText As String) As OrderAlreadyPlacedAtanExchange
        Debug.Assert(base <> "")
        Debug.Assert(quote <> "")
        Debug.Assert(price <> "")

        Dim a = New OrderAlreadyPlacedAtanExchange
        a.InitializeForBasicSimpleOrder(amount, price, base, quote, exchange, sellinginsteadofbuying, identifyingText)

        time = IIf(time = "", "1-1-2000", time).ToString
        a._timeOrderPlaced = Date.Parse(time)
        a._id = id
        If quote = "IFLT" Then
            Dim b = 1
        End If

        Return a
    End Function

Basically what happened is that the class OrderAlreadyPlacedAtanExchange inherits from class BasicSimpleOrder.

Class OrderAlreadyPlacedAtanExchange
    Inherits BasicSimpleOrder
    

So if I am doing it my way I create an object of type OrderAlreadyPlacedAtanExchange but then I can initialize the base class of the object with

    a.InitializeForBasicSimpleOrder(amount, price, base, quote, exchange, sellinginsteadofbuying, identifyingText)

Since using json.net I am interested in doing it the normal way.

But how can a constructor (or new sub) of OrderAlreadyPlacedAtanExchange initialize their parents?

Should a constructor of OrderAlreadyPlacedAtanExchange called a constructor of BasicSimpleOrder? But then I'll be getting an object of type BasicSimpleOrder

If that's the case then I should keep function InitializeForBasicSimpleOrder and get a new sub to call it then?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user4951
  • 32,206
  • 53
  • 172
  • 282
  • 1
    `MyBase.New(... parameters...)` – Jimi Nov 16 '22 at 12:30
  • Oh I get it. That is why new is a sub instead of a function. it's real function is simply initializing. And calling mybase.new will not return an object with base class. It will simply initialize stuffs. – user4951 Nov 16 '22 at 12:46
  • 1
    That's the idea. In this case, the base class is supposed to have an overloaded Constructor. If it doesn't have one (and you cannot add it), you just initialize base class properties, as in `Public Sub New(someParam as [Type], otherParam as [Type]) MyBase.SomeProperty = someParam MyBase.OtherProperty = otherParam End Sub` -- If you use Constructors as intended in the .Net Platform, you can then deserialize you class with ReadOnly Properties as [previously described](https://stackoverflow.com/q/74432461/7444103) – Jimi Nov 16 '22 at 13:16

0 Answers0