I have some C# code I'm trying to convert to VB.NET. There are some external dependencies I'm referencing in each project. The C# line is
TWriteFileCommand writeFile = (TWriteFileCommand)CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile);
I don't really understand what the TWriteFileCommand in parathesis after the equal sign is for in that statement. When I try to do it in VB.NET, I keep getting errors because I can't get a reference to the CreateCommand function. The closest I've gotten is this:
Dim MyAppBase As OPSupport.App.AppBase
Dim writeFile As TWriteFileCommand
writeFile = MyAppBase.CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile)
However, I have a green underline after the equal sign @ MyAppBase that flags an error "Variable MyAppBase is used before it has been assigned a value. A null reference exception could result at runtime."
What am I missing? Why in the C# code is the instance fine and what do I need to get the equivalent instance in VB.NET? Please advise.
That is a cast; if CreateCommand
returns object
, CommandBase
, or anything like that - it does the type-check to TWriteFileCommand
.
If you have compiling C# code - can you use reflector to do the translation? This shows you C# and equivalent VB at a flip of a drop-down.
I'm not sure what the MyAppBase
is doing; that didn't exist in the C# - why did you add it to the VB?
edit I don't "do" VB, but I looked it up in reflector; (Foo)bar
becomes DirectCast(bar, Foo)
.
It looks like CreateCommand
is a static method in the CommunicationLink
type. Either that, or there's a property called CommunicationLink
, and the type of that has a CreateCommand
method.
The bit in brackets after the = is a cast. Use CType
or DirectCast
to do this in VB.NET.
The ()
are one of C#'s ways of casting from one type to another. The VB equivalent is CType()
.
The direct equivalent is:
Dim writeFile As TWriteFileCommand
writeFile = DirectCast(CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile), TWriteFileCommand)
Where is the CommunicationLink variable decalred and instantiated in the C# code? You need to make sure the VB code uses the same source for that object.
I can' really see where did Dim MyAppBase As OPSupport.App.AppBase
came from.
As far as (TWriteFileCommand)
is concerned, this is a type cast. DirectCast
has (almost?) the same functionality.
It's because you dont assign a variable value to MyAppBase:-)
Dim MyAppBase As OPSupport.App.AppBase = new OPSupport.App.AppBase()
Dim writeFile As TWriteFileCommand
writeFile = MyAppBase.CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile)
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.