Cannot assign from type 'System.Object' to type 'System.IntPtr' in Assign activity 'Assign'

Hi,

I’m trying to translate the following piece of code (as someone suggested in this forum) to be able to dynamically use passwords via credentials assets on DSN-based ODBC connections in my application:

public static string ConvertToUnsecureString(this SecureString securePassword)
{
    if (securePassword == null)
        throw new ArgumentNullException("securePassword");

    IntPtr unmanagedString = IntPtr.Zero;
    try
    {
        unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword);
        return Marshal.PtrToStringUni(unmanagedString);
    }
    finally
    {
        Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
    }
}

I almost have the proper sequence of workflows if it weren’t because when I try to translate the
IntPtr unmanagedString = IntPtr.Zero;
via an Assign activity, I get the error from the title. It says it’s a “validation error”, and complains about “Cannot assign from type ‘System.Object’ to type ‘System.IntPtr’ in Assign activity ‘Assign’”, but the type of my variable unManagedString, the one I’m trying to assign the IntPtr.Zero value, is not of generic Object type but IntPtr, so I don’t see where the problem is. You can see this also in the attached picture.

Any hints??

Hi @pere

Try this

  1. Add an Assign activity to your workflow.
  2. Set the left-hand side of the Assign activity to your IntPtr variable (unmanagedString in this case).
  3. Set the right-hand side of the Assign activity to New Object with the value GetType(IntPtr).GetField("Zero").GetValue(Nothing).

I hope it works!!

Hi @pere

Pass like this:
CType(IntPtr.Zero)

Hi @pere

  • Left side: unmanagedString (the variable you want to assign to)
  • Right side: New IntPtr(0) or CType(0, IntPtr)

I’m getting a

“CType(IntPtr.Zero) Syntax error in cast operator; two arguments separated by comma are required”

and I think @pravallikapaluri pointed out what the issue is, as there’s one argument missing.

Still I don’t understand why UiStudio is throwing the original error, and I remember using CType function in the past but I forgot what it’s for, why should I use it and if I’m gonna encounter this situation further in the future and when to expect it to happen…

Thank you all anyway.

Not working because it seems I have “Option Strict On”:

Compiler error(s) encountered processing expression “GetType(IntPtr).GetField(“Zero”).GetValue(Nothing)”.
Option Strict On disallows implicit conversions from ‘Object’ to ‘System.IntPtr’.

By the way, it’s a nightmare to copy&paste these error messages from the editor. Unable to select the text and copy it. It’s displayed in plenty different places and also showed as a hint message when hovering the error or the exclamation icon, but impossible to copy, unless you explicitly run a check for errors via F7 or Shift+F7, then go over the error and, again, not possible to straight copy&paste the error text, but (at least!) this time a “copy” option in the tiny context menu when right-clicking shows up… oh well… I don’t understand why companies make these basic design flaws, overcomplicating the search for errors by explicitly disabling the ability to copy text.

@pere

The CType function is used for type conversion or casting in VB.NET and other .NET languages. It allows you to convert an expression from one data type to another, provided that the conversion is valid. You might encounter the need to use CType when you have variables or values of one data type that you want to convert to a different data type.

1 Like

Thank you; I marked yours as the accepted answer. But I wonder why I need to do this here, and why it allows me to use

New IntPtr(0)

but not

IntPtr.Zero

The piece of code is nothing I found randomly somewhere, but coming from the Microsoft blogs themselves ( How to properly convert SecureString to String | Microsoft Learn, which I would expect to be a reliable, bug-free source…

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.