about casting..

I have a class called AStudy that is derived from Dictionary. AStudy::Dictionary (Pardon the C++, I don’t know how to write that in Xojo)

I have a method GetAll, that returns an array of Dictionaries.

Dim Studies(-1) as AStudy
Studyies = GetAll()

How do I cast this?

There’s no built in operator.

If GetAll is really returning AStudy instances then why not return AStudy() instead of Dictionary()?

Otherwise you’ll need a down casting method like…

[code]Sub downCast(arr1() As Object, arr2() As Object)
dim last As integer = arr1.Ubound
redim arr2(last)
for i As integer = 0 to last
arr2(i) = arr1(i)
next
End Sub

//then

Dim Studies(-1) as AStudy
downCast(GetAll(), Studies)[/code]

Arrays are impossible to cast. You have to copy their elements, casting each one.

Perhaps I’ve over complicated the issue, in my example I was copying an array.
however…

dim x as new dicom.APatient
dim y as new Dictionary
y.Value(“PatientID”)=“123”
x = y
type mismatch error.

Is there an assignment operator that must be defined or must a constructor(d as dictionary) that must be made?

[quote=62562:@Brian O’Brien]Perhaps I’ve over complicated the issue, in my example I was copying an array.
however…

dim x as new dicom.APatient
dim y as new Dictionary
y.Value(“PatientID”)=“123”
x = y
type mismatch error.

Is there an assignment operator that must be defined or must a constructor(d as dictionary) that must be made?[/quote]

You’re trying to assign a value of type Dictionary (x) to a variable of type dicim.APatient (x). The two types aren’t compatible, hence the compile error.

Xojo doesn’t have assignment operators, but does have Operator_Convert to allow implicit conversions. However, since I think that implicit conversions are a horrible idea, I’d definitely recommend you just make a constructor on dicim.APatient that takes a Dictionary. Then your code would just read:

dim x as dicom.APatient
dim y as new Dictionary
y.Value("PatientID")="123"
x = new dicom.APatient( y)

But APatient ISA dictionary.
APatient::Dictionary.

So unless I make a constructor that accepts a dictionary:
x = new APatient(y)
won’t work.

[quote=62609:@Brian O’Brien]But APatient ISA dictionary.
APatient::Dictionary.

So unless I make a constructor that accepts a dictionary:
x = new APatient(y)
won’t work.[/quote]

D’oh, sorry, I misread the original post. Your problem is that while APatient is a Dictionary, Dictionary is not a APatient. It’s as if you wrote this C++ code: http://ideone.com/WeNfjO.

OMG isn’t it?!

I’ve had a while to think about this and I’m thinking this isn’t the way to go about things…
I’m going to scratch my head some more and come up with a different scheme.