# VTK Variant

**URL:** <https://discourse.vtk.org/t/vtk-variant/14992>\
**Category:** Development\
**Created:** [December 9, 2024, 2:23pm UTC](https://discourse.vtk.org/t/vtk-variant/14992 "2024-12-09T14:23:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PratikKarangle](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/4af34b/32.png) [@PratikKarangle](https://discourse.vtk.org/u/PratikKarangle)\
**Post date:** [December 9, 2024, 2:23pm UTC](https://discourse.vtk.org/t/vtk-variant/14992/1 "2024-12-09T14:23:17Z")

</div>

I am working with `vtkSQLiteDatabase`, `vtkTable`, and `vtkVariant`. The `vtkVariant` class provides various methods to convert the data it holds into different formats such as `int`, `float`, `char`, and `string`. However, when I use the `toString` method, I am unable to cast the data to `std::string` type.

I also tried the simplest example of `vtkVariant` from the VTK examples (included below), but it doesn’t seem to work. Has anyone else encountered similar issues with `vtkVariant` or have any suggestions or solutions? I am using VTK 9.4 and the ISO C++20 Standard on Windows.

VTK variant example:

#include \<vtkVariant.h\>

#include   
#include

int main(int, char\*)  
{  
double dVal = vtkVariant(“2”).ToDouble();  
std::cout \<\< "dVal: " \<\< dVal \<\< std::endl;  
// I get the error in following line of code  
std::string strVal = vtkVariant(dVal).ToString();  
std::cout \<\< "strVal: " \<\< strVal \<\< std::endl;  
return EXIT\_SUCCESS;  
}

---

<div class="post-metadata">

**Author:** ![Jens\_Munk\_Hansen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jens_munk_hansen/32/1215_2.png) [@Jens\_Munk\_Hansen](https://discourse.vtk.org/u/Jens_Munk_Hansen)\
**Post date:** [December 9, 2024, 5:31pm UTC](https://discourse.vtk.org/t/vtk-variant/14992/2 "2024-12-09T17:31:11Z")

</div>

> [@PratikKarangle](#):
>
> #include \<vtkVariant.h\>
> 
> #include  
> #include
> 
> int main(int, char\*)  
> {  
> double dVal = vtkVariant(“2”).ToDouble();  
> std::cout \<\< "dVal: " \<\< dVal \<\< std::endl;  
> // I get the error in following line of code  
> std::string strVal = vtkVariant(dVal).ToString();  
> std::cout \<\< "strVal: " \<\< strVal \<\< std::endl;  
> return EXIT\_SUCCESS;  
> }

Here an example like this compiles

```auto
cmake_minimum_required(VERSION 3.13)
project(Variant
LANGUAGES CXX)

# -----------------------------------------------------------------------------
# Handle VTK dependency
# -----------------------------------------------------------------------------

# Enable C++20 for the project
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) # Ensure the standard is required.
set(CMAKE_CXX_EXTENSIONS OFF) # Use only standard-compliant C++.

find_package(VTK
  COMPONENTS
    CommonCore)

if (NOT VTK_FOUND)
  message("Skipping issue: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif ()

# -----------------------------------------------------------------------------
# Compile example code
# -----------------------------------------------------------------------------

add_executable(Variant Variant.cxx)

target_link_libraries(Variant
  PRIVATE
    VTK::CommonCore
)

```

Perhaps, you have an issue linking to debug from a release build or vice versa.
