Solved

Converting C# to VB.NET

Posted on 2009-06-28
4
272 Views
Last Modified: 2013-12-17
I've tried to convert the following code to VB.NET

but have failed and the converters I've tried don't seem to convert the code correctly:

objReader = (enc != null) ? new StreamReader(filestream, enc) : new StreamReader(filestream);

Any help would be great.
0
Comment
Question by:directxBOB
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 350 total points
ID: 24732763
not sure of using Conditional Operator in VB but here is VB code for what the above C# code is trying to accomplish:

If enc IsNot Nothing Then
    objReader = New StreamReader(filestream, enc)
Else
    objReader = New StreamReader(filestream)
End If
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 350 total points
ID: 24732774
Alrite I think IIF(i-i-f) works like conditional operator in VB.NET:

Try like this:
objReader = IIf((enc IsNot Nothing), New StreamReader(filestream, enc), New StreamReader(filestream))

Ref:
http://www.dotnetspider.com/forum/1324-exmple-for-coinditional-operator-vb-net.aspx
http://www.willasrari.com/blog/vbnet-conditional-operators-or-lack-thereof/000296.aspx
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 total points
ID: 24732956
*** BE CAREFUL ***

IIf() has severe limitations...

See: http://msdn.microsoft.com/en-us/library/27ydhh0d.aspx

    "Because the IIf function does NOT use short-circuit evaluation, it always evaluates all three of its arguments."
                                                    ^^^^
In your case, if "enc" IS actually Nothing, then Nothing would be passed to the StreamReader() Constructor since ALL THREE arguments are evaluated regardless of the result of the conditional portion.  This will probably result in an exception being thrown.

.Net 3.5 has an equivalent If() operator:
http://msdn.microsoft.com/en-us/library/bb513985.aspx

If you don't have that version then you need the longer code in guru_sami's first post.
0
 

Author Closing Comment

by:directxBOB
ID: 31597744
Kept it simple
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now