Solved

Converting C# to VB.NET

Posted on 2009-06-28
4
276 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

775 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