# I'm trying to create an array of handles for a class, by randomize function I'll get the "no of transcations", this determines the no of handles to be created.class\_name obj\_name\[transactions\]; is this right?

**URL:** <https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, class-handle\
**Created:** [September 15, 2015, 5:10pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031 "2015-09-15T17:10:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ammu4392](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@Ammu4392](https://verificationacademy.com/forums/u/Ammu4392)\
**Post date:** [September 15, 2015, 5:10pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/1 "2015-09-15T17:10:39Z")

</div>

I’m trying to create an array of handles for a class, by randomize function I’ll get the “no of transactions”, this determines the no of handles to be created. for example : class\_name obj\_name[transactions]; is this right?

coz I’m getting a syntax error when I do this

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 15, 2015, 5:23pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/2 "2015-09-15T17:23:22Z")

</div>

_In reply to [Ammu4392](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031):_

Can you please show some code, including the declarations of any variables reference by that code, and the actual error message.

---

<div class="post-metadata">

**Author:** ![bmorris](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bmorris/32/2097_2.png) [@bmorris](https://verificationacademy.com/forums/u/bmorris)\
**Post date:** [September 15, 2015, 7:55pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/3 "2015-09-15T19:55:44Z")

</div>

_In reply to [Ammu4392](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/2):_

I believe you want something like…

```verilog
classA obj[]; // dynamic array
...

virtual task body();
  a.randomize();
  num_txns = a.transactions;
  obj=new[num_txns];
  ...

```

---

<div class="post-metadata">

**Author:** ![Ammu4392](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@Ammu4392](https://verificationacademy.com/forums/u/Ammu4392)\
**Post date:** [September 15, 2015, 8:10pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/4 "2015-09-15T20:10:00Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/3):_

```verilog
virtual task body();
	     a.randomize();
	    txns = a.transactions; 
	   classA obj[txns];
 for(int i=0; i<txns; i=i+1)begin
	    `ovm_create(obj[i])
             a.randomize();
	     int x = a.core;
	    `ovm_send(obj[i])
	      wait(obj.rsp != null );
          end   
	endtask : body

```

---

<div class="post-metadata">

**Author:** ![Ammu4392](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@Ammu4392](https://verificationacademy.com/forums/u/Ammu4392)\
**Post date:** [September 15, 2015, 8:11pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/5 "2015-09-15T20:11:36Z")

</div>

_In reply to [bmorris](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/4):_

Yes. But does soemthing like that works?

---

<div class="post-metadata">

**Author:** ![Ammu4392](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@Ammu4392](https://verificationacademy.com/forums/u/Ammu4392)\
**Post date:** [September 15, 2015, 8:16pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/6 "2015-09-15T20:16:20Z")

</div>

_In reply to [Ammu4392](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/5):_

Can u also tel me how I’ll iterate through the array??  
shud i use for loop or foreach?

---

<div class="post-metadata">

**Author:** ![bmorris](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bmorris/32/2097_2.png) [@bmorris](https://verificationacademy.com/forums/u/bmorris)\
**Post date:** [September 15, 2015, 8:26pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/7 "2015-09-15T20:26:45Z")

</div>

_In reply to [Ammu4392](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/6):_

I’d use foreach for an array, but you can use both.

foreach (obj[i]) begin  
…  
end

---

<div class="post-metadata">

**Author:** ![Ammu4392](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@Ammu4392](https://verificationacademy.com/forums/u/Ammu4392)\
**Post date:** [September 15, 2015, 9:26pm UTC](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/8 "2015-09-15T21:26:25Z")

</div>

_In reply to [bmorris](https://verificationacademy.com/forums/t/im-trying-to-create-an-array-of-handles-for-a-class-by-randomize-function-ill-get-the-no-of-transcations-this-determines-the-no-of-handles-to-be-created-class-name-obj-name-transactions-is-this-right/31031/7):_

Can u tel me why we are declaring the obj declaration outside of body?
