Welcome! Log In Create A New Profile

Advanced

[WB23] How do I reference an object of which current object is a member?

Posted by vincent_stoop 
[WB23] How do I reference an object of which current object is a member?
December 02, 2018 02:24PM
Hello all,

I'm hoping someone can tell me the solution to the following...

I have 2 classes (Person, Account)
There is an object of type Account, which is a member of Person.
When in the code of Account, I want/need to be able to access properties of Person, but from an instance of Account directly. I would like to be able to do something like this:

Person is Class
	LastName is string
	MyAccount is Account

	Constructor
		MyAccount = new Account(LastName)

Account is Class
	Accountnumber is string <mapping....>
	AccCode is string
	Constructor(LN is string)
		IF HReadSeekFirst(Account, LastName, LN) THEN
			FileToMemory(object, Account)
		END
		...
		AccCode = parent.LastName + "-" + Accountnumber

MyFirstPerson is new Person("Stoop")
Info(MyFirstPerson.MyAccount.Accountnumber) // Returns the correct accountnumber, as expected.

AccountDetails is Account = MyFirstPerson.MyAccount
Info(AccountDetails.parent.LastName)
Info(AccountDetails.AccCode)

Is this possible in WLanguage? Without having to call a specific variable of type Person in the code of Account, or having to pass every Person detail as a parameter to the Account constructor?

Kind regards,

Vincent Stoop



Edited 1 time(s). Last edit at 12/02/2018 02:32PM by vincent_stoop.
Re: [WB23] How do I reference an object of which current object is a member?
December 03, 2018 11:40AM
Hello Vincent,

What about this?

Account is class
Acountnumber is string
AccCode is string

Constructor(LN is string)
If Hreadseek(Account, LastName, LN) then
FileToMemory(object, Account)
end
end

Person is class
LastName is string
MyAccount is Account
Constructor(LN is string)
LastName = LN
MyAccount is New Account(Lastname)
end


MyFirstPerson is new Person("Stoop")

Info(MyFirstPerson.Lastname)
info(MyFirstPerson.MyAccount.accCode)

Regards,
Bart
Re: [WB23] How do I reference an object of which current object is a member?
December 03, 2018 12:30PM
Hi Bart,

Thanks for your answer, but as I see it, it still doesn't let me address the parent's properties. In other languages, i.e. Ruby, if you say in the Person class 'has_many accounts' and in the Account class 'belongs_to person', you can then later say person.accounts (which of course is similar in WLanguage) but you can also say account.person.lastname and get the last name, even though this is not a member of the Account class.

I've worked around it by having a structure with some data from the Person class, and filling it with data from the appropriate record in the HFSQL file, but it does mean an extra call to the database, while the information is already in memory.

Regards,

Vincent
Re: [WB23] How do I reference an object of which current object is a member?
December 07, 2018 05:09PM
To operate with Person from Account you should have a reference to Person object. Your objects are not relative so you can't use word "parent"!

Person is Class
  LastName is string
  MyAccount is Account dynamic
END

Constructor PROCEDURE(LN string)
  LastName = LN
  MyAccount = new Account(LastName, object)
END

Account is Class
  AccCode is string
  MyPerson is Person dynamic
END

Constructor PROCEDURE(LN string, accPerson Person)
  MyPerson = accPerson
  AccCode = MyPerson.LastName + 'AccountNumber'
END



Edited 3 time(s). Last edit at 12/07/2018 05:13PM by WxDeveloper.
Re: [WB23] How do I reference an object of which current object is a member?
December 09, 2018 11:56PM
Hallo Vincent,

Inherits from is your friend.

regards
Hi Vincent,

Account is class
inherits Person


If you have the same members in both classes you can explicitely refer to teh parent member by using the ancestor keyword.
e.g.: ancestor.name


Cheers,

Peter Holemans
Re: [WB23] How do I reference an object of which current object is a member?
December 13, 2018 10:52AM
@WxDeveloper, I did something similar, but that didn't work. When I'm working on that part of the application again I'll try that.

@Allard, @Peter Holemans: why would an account inherit from a person? They're not similar at all, so no shared members. With inheritance I can address the ancestor from inside the class, but not from an instance of the class itself...
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: