Solved

Converting C# to VB.NET

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

914 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

14 Experts available now in Live!

Get 1:1 Help Now