Solved

Converting C# to VB.NET

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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