Error Type 'MarshalAs' is not defined

I recently converted a project from VB2003 to VB2005.  I am getting this error and I can't figure out why...

Type 'MarshalAs' is not defined

Here is an example of the code that is causing the error:

<StructLayout(LayoutKind.Sequential)> Friend Structure VersionInfoDef

   Dim SizeofVersionInfo As Byte
   Dim BootVersion As VersionDef
   Dim OSVersion As VersionDef
   Dim NumOSExtensions As Byte
   <MarshalAs(UnmanagedType.ByValArray, SizeConst:=40)> Dim OSExt() As Byte
   
   Public
Sub Initialize()
      
ReDim OSExt(39)
   End Sub
End Structure


I have tried every Imports statement I can think of.  Here is what I have now...

Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.UnmanagedType
Imports System.Runtime.InteropServices.Marshal
Imports System.Runtime.InteropServices.StructLayoutAttribute

I also notice that when I click on the error in the error list, it does not take me to the error even though it shows a line number with the error.

Thanks for your help..

Clark



Answer this question

Error Type 'MarshalAs' is not defined

  • Steve Roszko

    I literally just copied and pasted your code into the editor and vs fixed most of the issues for me. So I am going to have to say its something wrong with VS because it compiles just fine for me. I do not know how it could be a bug if it works fine for me and not for you. UNless there is something in your enviorment that causes it not to work correctly.


  • Josh Pendergrass

    Intellisense is working the same for me too.  The error happens when I try to compile.  Since you can compile ok, it is clear that I will have to repair VS, reinstall VS, or kill my computer....   HA!

    Thanks for your help!!!

    Clark


  • Hugo Flores

    Wacko

    Thanks for your reply.  When I change my code to your code I get another error "Type 'Runtine.InteropServices.MarshalAs' is not defined.  I am now getting this error on this structure instead of the previous error.  It seems that Runtime.InteropServices is messed up.

    Is something wrong with my installation of VB2005 or is this a bug

    Clark



  • Tom Albrecht

    I fixed it.  The problem was something in the project.  Perhaps the importing from VS2003 has a problem.  When I cut and pasted the code into a new project it started working. 


  • krissmith

    I did not import anything...I just copied your code into a newly created VB project and then fixed the issues by adding the Runtime.InteropServices in front of the MarshalAs I tested it again here at work and what i posted up there copies and pasts into a newly created VB project and compiles. I mean at this point the only think imported is system.

    I also just to confirm type in "<Runtime." and intellisence picked it up after that for interopservices and MarshalAs. So I am going to say its your enviorment.



  • Joe Szymanski

    I agree, doesn't sound like a bug.  Somehow my VS studio environment does not know about MarshalAs.

    Just a couple of last followup questions, wacko...I didn't totally understand what you were saying at the start of your first post....with respect to Imports. 

    Are you saying you have this imports statement in your program...
    Imports Runtime.InteropServices.MarshalAs

    When I try to enter that, there is no MarshalAs available after InteropServices.  I only have Marshal and MarshalAsAttribute.  Does your system behave this way   Is your system different than mine   I have tried imported both Marshal and MarshalAsAttribute....neither helps.    

    Thanks again for taking the time to try to help me.  Unless I can quickly find what is wrong with my system, I guess I will reinstall VS2005 on a clean machine and see what happens.  

    Clark



     

  • Michael Friedman

    should be "Runtime.InteropServices.MarshalAs" and then the code for some reason it does not want to grab from a import, but your code does not have the error is its Runtime.InteropServices.MarshalAs here is the code to make it work in VB 2005



    <Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> Friend Structure VersionInfoDef
        Dim SizeofVersionInfo As Byte
        Dim BootVersion As Version
        Dim OSVersion As Version
        Dim NumOSExtensions As Byte
        <Runtime.InteropServices.MarshalAs(Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=40)> Dim OSExt() As Byte

        Public Sub Initialize()
            ReDim OSExt(39)
        End Sub
    End Structure

     



  • ez.xcess

    Well I am glad you got it fixed..


  • Error Type 'MarshalAs' is not defined