tags:

views:

179

answers:

7

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.

+4  A: 

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).

Marc Gravell
+1  A: 

The () are one of C#'s ways of casting from one type to another. The VB equivalent is CType().

Andrew Hare
He will need to cast, but the current error is because his MyAppBase object is null, and not static/shared.
Joel Coehoorn
Good point - it did also seem like he was confused as to how to do a cast with VB syntax.
Andrew Hare
A: 

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.

Joel Coehoorn
A: 

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.

Anton Gogolev
C# () cast is somewhere in between CType() and DirectCast, since it will honor implicit/explicit conversions where DirectCast will not, but won't do as much extra work as the CType() operator.
Joel Coehoorn
+3  A: 

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.

Jon Skeet
A: 

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)

?

Yossarian
I had tried declaring it as new before but it gives me an error "'New' cannot be used on a class that is declared 'MustInherit'"
Jason Shoulders
+1  A: 

The direct equivalent is:

Dim writeFile As TWriteFileCommand 
writeFile = DirectCast(CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile), TWriteFileCommand)
Patrick McDonald
Others answered something similar, but this was the one I read first. Thanks.
Jason Shoulders