# Unable to cast Metadata to StringMetadata

**URL:** <https://forum.techsoft3d.com/t/unable-to-cast-metadata-to-stringmetadata/1278>\
**Category:** HOOPS Visualize Desktop\
**Tags:** cad\
**Created:** [January 3, 2023, 9:30pm UTC](https://forum.techsoft3d.com/t/unable-to-cast-metadata-to-stringmetadata/1278 "2023-01-03T21:30:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie.portelli](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@charlie.portelli](https://forum.techsoft3d.com/u/charlie.portelli)\
**Post date:** [January 3, 2023, 9:30pm UTC](https://forum.techsoft3d.com/t/unable-to-cast-metadata-to-stringmetadata/1278/1 "2023-01-03T21:30:53Z")

</div>

I have a CADModel loaded in HOOPS, and I’m able to get the list of Metadata from the model. I’m trying to iterate over the list of Metadata objects to get the name and value of each metadata object.  
I’m writing this in C#.

```auto
List<CADModel> cadModels = Factory.GetCADModels().ToList();
Console.WriteLine("Number of CAD Models: " + cadModels.Count);

CADModel cadModel = cadModels[0];

List<Metadata> metadatas = cadModel.GetAllMetadata().ToList();

foreach (Metadata metadata in metadatas)
{
      string name = metadata.GetName();
      
      // this line throws a System.InvalidCastException: "Unable to cast object type HPS.Metadata to type HPS.StringMetadata.
      StringMetadata str = (StringMetadata)cadModel.GetMetadata(name);
      Console.WriteLine(metadata.GetName() + str.GetValue());
}

```

I’m using the [Hoops BIM Viewer Tutorial](https://github.com/techsoft3d/bim_viewer_tutorial/blob/5f1226f6cbb2d9007510d29a28553b2527e41ec7/IFCUtils.cpp#L28) as an example. In the BIM viewer example, this line of code works fine.

Any advise would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![charlie.portelli](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@charlie.portelli](https://forum.techsoft3d.com/u/charlie.portelli)\
**Post date:** [January 3, 2023, 9:47pm UTC](https://forum.techsoft3d.com/t/unable-to-cast-metadata-to-stringmetadata/1278/2 "2023-01-03T21:47:50Z")

</div>

I was able to solve the issue by creating a new StringMetadata object and using the metadata object in the constructor.

```auto
CADModel cadModel = cadModels[0];

List<Metadata> metadatas = cadModel.GetAllMetadata().ToList();

foreach (Metadata metadata in metadatas)
{
     string name = metadata.GetName();
                 
     StringMetadata str = new StringMetadata(metadata);
     Console.WriteLine(metadata.GetName() + " " + str.GetValue());
}

```

---

<div class="post-metadata">

**Author:** ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)\
**Post date:** [January 4, 2023, 2:05pm UTC](https://forum.techsoft3d.com/t/unable-to-cast-metadata-to-stringmetadata/1278/3 "2023-01-04T14:05:44Z")

</div>

Glad to hear you were able to solve it! 🙂 Thanks for sharing
