1

Hello all i have tried to convert c# code to vb but getting error in this line as "Expression Expected" what can be the error and what is the correct syntax. Error is in second line

Dim m As MenuItem = TryCast(sender, MenuItem)
audioDevice = (If(m.Index>0, filters.AudioInputDevices(m.Index-1), Nothing))

C# CODE

MenuItem m = sender as MenuItem;
audioDevice = ( m.Index>0 ? filters.AudioInputDevices[m.Index-1] : null );
5
  • 3
    post your C# code too Commented Jan 10, 2014 at 9:32
  • 1
    Maybe If must be Iif? Commented Jan 10, 2014 at 9:35
  • 2
    @nathan742 Noooo - never use IIf !! Commented Jan 10, 2014 at 9:35
  • As per MSDN, your code must be working properly on VS2008 and up msdn.microsoft.com/en-us/library/bb513985(v=vs.90).aspx If you do not want to use Iif then your choice... Commented Jan 10, 2014 at 9:42
  • @nathan IIf will cause the code to crash Commented Jan 10, 2014 at 9:43

3 Answers 3

4

Are you running VB.Net 2008 or later? The If operator is not supported in earlier versions.

Since the non-short-circuit Iif function will cause the true-part of the expression to cause an index out of range, you should use an If Then Else statement if you want a version prior to 2008 to be supported.

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

2 Comments

Also note that the IF operator is type safe whereas IIF is not.
Just to be pedantic, IIF is not an operator, it is a function. IF in newer versions of VB is an operator.
1

Try this

Dim m As MenuItem = TryCast(sender, MenuItem)
audioDevice = (IIf(m.Index>0, filters.AudioInputDevices(m.Index-1), Nothing))

IIf Function MSDN Documentation

As IIf Function is deprecated use like this

If m.Index>0 Then
    audioDevice = filters.AudioInputDevices(m.Index-1)
  Else
    audioDevice = Nothing
  End If

Sample

Hope it helps

4 Comments

that's what I've said
@nathan742 Yup you are right in comments, but i too found out that it might be mistake and i was in search of online documentation of it..
Iif is deprecated in favor of If
see he was asking where the error may be, i pointed out that.
-1

Here is probably the best tool I have ever found! And having been a developer for over 10 years, when I found this (And with most examples online being in C, this was just GOLDEN!) .. Hope this helps you! (And anyone else that hasn't stumbled across this 'gem'... Finally VB coders have better access to online examples... NOTE* this isn't the only code converter, but it's by far the best one I have ever used.. Perhaps if you convert the code again using this, your troubles might be solved.......

http://converter.telerik.com/ (Does an amazing job on converting)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.