From C# to VB.NET

  • Thread starter Thread starter Robert Scheer
  • Start date Start date
R

Robert Scheer

Hi.

I am trying to port a library from C# to VB.NET. I have 95% of this
code already ported, but I do not know how to translate the line
below:

public override ProblemCollection Check(Member member)
{
Method method = member as Method;
....
}

What kind of construct is this? Is there an equivalent construct in
VB.NET?

Regards,
Robert Scheer
 
Hi.

I am trying to port a library from C# to VB.NET. I have 95% of this
code already ported, but I do not know how to translate the line
below:

public override ProblemCollection Check(Member member)
{
Method method = member as Method;
....
}

What kind of construct is this? Is there an equivalent construct in
VB.NET?

Regards,
Robert Scheer

Off the top of my head:

Public Overrides Function Check(member as Member) As ProblemCollection
Dim method As Method = CType(member, Method)
End Function

Thanks,

Seth Rowe
 
VB.net is not case sensitive.

so in C#


Employee employee = new Employee();

will work

in VB.net

dim employee as Employee = new Employee()

will NOT work
you gotta go with

dim emp as Employee = new Employee

(or similar)

watch those properties too for case sensitivity.
 
The equivalent is not a simple 'CType'.
(via Instant VB)
VB 2005:
Dim method As Method = TryCast(member, Method)
VB 2003:
Dim method As Method = CType(IIf(TypeOf member Is Method, member, Nothing),
Method)

As 'sloan' mentioned you'll also have to rename 'method' since VB is not
case sensitive.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
dim emp as Employee = new Employee
(or similar)

watch those properties too for case sensitivity.

Actually vb lets you get away with a variable the same name as a Type.
It just won't let you have two variables of the same (case
insensitive) name like in C#

i.e.

This compiles fine:

Dim employee As New Employee()
employee.Name = "Seth Rowe"
Console.WriteLine(employee.Name)

And even this will work:

Dim Employee As New Employee()
Employee.Name = "Seth Rowe"
Console.WriteLine(Employee.Name)

However in C# you can do this:

Employee Employee = new Employee();
Employee employee = new Employee();
Employee.Name = "Employee 1";
employee.Name = "employee 2";
Console.WriteLine(Employee.Name);
Console.WriteLine(employee.Name);

But the VB version will fail due to case insensitivity:

Dim Employee As New Employee()
Dim employee As New Employee()
Employee.Name = "Employee 1"
employee.Name = "employee 2"
Console.WriteLine(Employee.Name)
Console.WriteLine(employee.Name)

However, for sake of readability, I would follow you suggestion of
using a shortened version of the type name for the variable.

Thanks,

Seth Rowe
 
The equivalent is not a simple 'CType'.

Thanks David, I forgot "as Type" returned null if the cast failed.
Fortunately I tagged my post with "Off the top of my head"

:-)

Thanks,

Seth Rowe
 
Hmmm.

You're right, I guess I assumed it would fail, and never tested it.

But ... (as mentioned) readability and prone to bugs is still an issue.

THanks for the correction.
 
Robert Scheer said:
Hi.

I am trying to port a library from C# to VB.NET. I have 95% of this
code already ported, but I do not know how to translate the line
below:

public override ProblemCollection Check(Member member)
{
Method method = member as Method;
....
}

What kind of construct is this? Is there an equivalent construct in
VB.NET?

Regards,
Robert Scheer

Either use the inline-if method post from David Anton or:

Public Overrides Function Check(member As Member) As ProblemCollection
Dim method As Method = Nothing
If TypeOf method Is Method
method = CType(method, Method)
End If
...
End Function

AFAIK, using the IIF statement will cause extra (albeit, very small amount
of) overhead because IIF is actually a function itself...remember though,
it's probably negligible for your application...

HTH,
Mythran
 
Turn Option Strict On. Then the compiler will warn you when you give a
variable the same name as a type.

Mike.
 
Turn Option Strict On. Then the compiler will warn you when you give a
variable the same name as a type.

Mike.





Turn Option Strict On. Then the compiler will warn you when you give a
variable the same name as a type.

It does? None of the examples I posted earlier caused a warning to
show. Do you have an example demonstrating this?

Thanks,

Seth Rowe
 
Aha!

I default to that, including setting it in the project properties.

And I set it to "Raise Exception".

Sorry, I only occasionally code vb.net.

Thanks Michael for solving the (small) mystery.
 
Robert Scheer said:
Hi.

I am trying to port a library from C# to VB.NET. I have 95% of this
code already ported, but I do not know how to translate the line
below:

public override ProblemCollection Check(Member member)
{
Method method = member as Method;
....
}

What kind of construct is this? Is there an equivalent construct in
VB.NET?

Regards,
Robert Scheer

I use a C# to VB converter named Instant VB from
http://www.tangiblesoftwaresolutions.com/

You can get a trial copy. I put your code snippet in and got the answer that
several people have given you.

Galen
 
Did you try the OP's code on these free converters? (you might want to do
that)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI
 
It does? None of the examples I posted earlier caused a warning to
show. Do you have an example demonstrating this?

Thanks,

Seth Rowe

Sorry for reviving this thread - but I still can't get a warning from
the compiler for naming a variable the same name as a Type, even with
Option Strict On. Is there an option I'm missing somewhere?

Thanks,

Seth Rowe
 
Back
Top