The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Collection<user-defined class>
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

MBIMO

Belgium
Posts

Posted - 08/18/2005 :  10:43:29 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5 SR5
Operating System: Win2k

Hi everybody,

is there a simple way to implement an array of instances of a
user-defined class?
Or to make myself more clear, is it possible to emulate the behaviour of the internal collections like Pages, Layers,...

Collection<aClass> obviously works only for internal Origin objects.

So far I figured out that I could save the pointers to the instances in a vector<unit>. Is this feasible or are there better ways to do this.

Thanks in advance.

Best regards,

Martin


MBIMO

Belgium
Posts

Posted - 08/19/2005 :  05:50:06 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi everybody,

meanwhile I came up with the following code.

"TestClass" is just an arbitrary user-defined class that holds an interger value.
The function "Test" creates a couple of instances and adds their addresses to a vector<uint> list.

Here's the code:

-- 8< ---------------

class TestClass
{
private:
protected:
public:
TestClass(); //constructor
~TestClass(); //destructor

int Tag;
};


void Test()
{
vector<uint> ObjectList; //the list that stores the objects
TestClass Item0,Item1,Item2; //some instances of the TestClass
int Tag;
TestClass myItem;
uint Ptr;
int i;

Item0.Tag=0; //sets the "Tag" variable...
ObjectList.Add((uint) &Item0); //...and adds the address to the list

Item1.Tag=1;
ObjectList.Add((uint) &Item1);

Item2.Tag=2;
ObjectList.Add((uint) &Item2);


for (i=0; i<=ObjectList.GetSize()-1; i++) //loop through the list...
{
Ptr=ObjectList[i]; //...get the address...
myItem=*((TestClass*)Ptr); //...typecase it...
out_int("Tag: ",myItem.Tag); //...and finally print it
};
}

-- 8< ---------------

So far it's working. However I'm stuck now with the following problem.
How can I create the instances in a loop i.e. how can I call the constructor explicitly and create a new instance?

I tried to use something like this:

TestClass *newItem = new TestClass;

However Origin crashed when running this in a for-loop.

Can anybody help me?

Best regards,

Martin



Edited by - MBIMO on 08/19/2005 08:02:23 AM
Go to Top of Page

rlewis

Canada
253 Posts

Posted - 08/19/2005 :  09:32:17 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
See also ....
http://www.originlab.com/forum/topic.asp?TOPIC_ID=3560
Go to Top of Page

MBIMO

Belgium
Posts

Posted - 08/19/2005 :  10:40:18 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks rlewis,

I will give it a shot.

Best regards,

Martin
Go to Top of Page

MBIMO

Belgium
Posts

Posted - 08/22/2005 :  04:03:31 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
During the weekend I had time to test the code from
http://www.originlab.com/forum/topic.asp?TOPIC_ID=3560

It works fine. However there remains one further question. In the code above there's a member function which iterates through the nodes by using a foreach loop on the "Children" data field:


void ShowSequence()
{
foreach(TreeNode tr in m_trNode.Children)
{
AminoAcidResidue aar(tr);
printf("%c", aar.GetType());
}
printf("\n");
}


Since Children is defined a Collection<TreeNode> I tried something like this to access a specific child directly:


Collection<TreeNode> aCollection;

aCollection=m_trNode.Children;
TreeNode myChild;
myChild=aCollection.Item(anIndex); //<<Error, Incompatible variable types in expression


The compiler aborted with the error mentioned in the code above.
If I use an explicit typecast in the marked line:

 
myChild=(TreeNode)aCollection.Item(anIndex); //<<Error, invalid explicit cast


the copiler still refuses to compile.

I think I'm missing something fundamental. Can somebody please point me to the correct direction? What is the proper way to access a specific item of the Children collection? I know that I could use the
 TreeNode  GetNode (LPCSTR Name) 
member function but since I don't know the name, I'd like to access the child by index.

Thanks for your help.

Best regards,

Martin
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 08/22/2005 :  11:42:38 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Looks like you have found a bug. I have created bug tracking number 8037 to be fixed in our next release.

For the time being, you will have to write your own foreach loop to get a tree node, or you can prepare your tagName with enumeration to get around this bug.

CP


Go to Top of Page

MBIMO

Belgium
Posts

Posted - 08/22/2005 :  11:51:50 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank you CP, I will implement your workaround.

I'm glad it's a bug, I really had second thoughts about my programming skills :-)

Best regards,

Martin

Go to Top of Page

MBIMO

Belgium
Posts

Posted - 08/24/2005 :  03:57:22 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi it's me again,

to finally conclude this topic here's the solution I came up with.

Personally I find it more elegant then CP's approach of abusing the tagNames of the TreeNodes.

The code uses the FirstNode and NextNode data fields to loop through all nodes until the n'th one.



TreeNode Item(int n)
{
TreeNode tr;
int i;

tr=m_trNode.FirstNode;

for (i=0; i<=anIndex; i++)
{
tr=tr.NextNode;
};

return tr;
}


It works perfect.

Best regards,

Martin


Edited by - MBIMO on 08/24/2005 03:59:10 AM
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 08/30/2005 :  5:49:04 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I just found out that


trN = tr.Children.Item(ii);


has already been added to our next version, just it was not implemented in Origin 7.5

CP


Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000