?
Solved

Converting C# to VB.NET

Posted on 2009-06-28
4
Medium Priority
?
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 1400 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 1400 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 600 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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