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)>FriendStructure VersionInfoDef

Dim SizeofVersionInfoAsByte
Dim BootVersionAs VersionDef
Dim OSVersionAs VersionDef
Dim NumOSExtensionsAsByte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=40)>Dim OSExt()AsByte

Public
Sub Initialize()
ReDim OSExt(39)
EndSub
EndStructure


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

[2776 byte] By [csann] at [2008-2-6]
# 1
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

wacko at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
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

csann at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
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.
wacko at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
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

csann at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
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.

wacko at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

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

csann at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7
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.
csann at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8
Well I am glad you got it fixed..
wacko at 2007-9-8 > top of Msdn Tech,Visual Basic,Visual Basic Language...