0

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.

7 Answers 7

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

The direct equivalent is:

Dim writeFile As TWriteFileCommand 
writeFile = DirectCast(CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile), TWriteFileCommand)

1 Comment

Others answered something similar, but this was the one I read first. Thanks.
1

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

2 Comments

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

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.

Comments

0

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.

1 Comment

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

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)

?

1 Comment

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'"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.