473,769 Members | 5,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# to VB.net conversion for "?" conditionals

I need to convert some C# ?Conditionals over to vb.net (most likely in
vb.net using If..Then statements), but the C#2VBNETconvert er by
KamalPatel isn't converting them:

string PurchaseType = (Convert.ToStri ng(drwData["DailyItem"]) ==
"True") ? "ID" : "PID";

XmlNodeList Selections = xdcData.GetElem entsByTagName(( X == 0) ?
"Highlight" : "Selection" );

TIA
NetSports

Jan 26 '06 #1
11 1798
Take a look at other converters. Free SharpDevelop from
http://www.icsharpcode.net/OpenSource/SD/ can do this. There is also
online version at
http://www.developerfusion.co.uk/uti...sharptovb.aspx.
It seems it handles these statements correctly.

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Jan 26 '06 #2
Hi,

VB has similar function called IIF

<From MSDN>
Public Function IIf( _
ByVal Expression As Boolean, _
ByVal TruePart As Object, _
ByVal FalsePart As Object _
)

Keep in mind that this is a FUNCTION as oposed to C# ?:, which is operator.
What does it mean? This means that TruePart and FalsePart are passed as
parameter to the function and if they are emthods' return values sides are
going to be executed regardless of the value of the Expression. This might
lead to hard-to-discover semantic errors if calculations of the true-part
and/or false-part have side effects.
--
HTH
Stoitcho Goutsev (100)

".Net Sports" <ba********@cox .net> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
I need to convert some C# ?Conditionals over to vb.net (most likely in
vb.net using If..Then statements), but the C#2VBNETconvert er by
KamalPatel isn't converting them:

string PurchaseType = (Convert.ToStri ng(drwData["DailyItem"]) ==
"True") ? "ID" : "PID";

XmlNodeList Selections = xdcData.GetElem entsByTagName(( X == 0) ?
"Highlight" : "Selection" );

TIA
NetSports

Jan 26 '06 #3
(You get what you pay for with free converters)

Our Instant VB demo edition gives:

Dim PurchaseType As String
If (Convert.ToStri ng(drwData("Dai lyItem")) = "True") Then
PurchaseType = "ID"
Else
PurchaseType = "PID"
End If

Dim Selections As XmlNodeList
If (X = 0) Then
Selections = xdcData.GetElem entsByTagName(" Highlight")
Else
Selections = xdcData.GetElem entsByTagName(" Selection")
End If

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

".Net Sports" wrote:
I need to convert some C# ?Conditionals over to vb.net (most likely in
vb.net using If..Then statements), but the C#2VBNETconvert er by
KamalPatel isn't converting them:

string PurchaseType = (Convert.ToStri ng(drwData["DailyItem"]) ==
"True") ? "ID" : "PID";

XmlNodeList Selections = xdcData.GetElem entsByTagName(( X == 0) ?
"Highlight" : "Selection" );

TIA
NetSports

Jan 27 '06 #4
Note that replacing "?" with IIf (which is what the free converters do) is
not correct.
The correct conversion is if/else.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Peter Macej" wrote:
Take a look at other converters. Free SharpDevelop from
http://www.icsharpcode.net/OpenSource/SD/ can do this. There is also
online version at
http://www.developerfusion.co.uk/uti...sharptovb.aspx.
It seems it handles these statements correctly.

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code

Jan 27 '06 #5
".Net Sports" <ba********@cox .net> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
I need to convert some C# ?Conditionals over to vb.net

string PurchaseType = (Convert.ToStri ng(drwData["DailyItem"]) ==
"True") ? "ID" : "PID";


The direct equivalent of the "?" operator is Visual Basic's IIf function:

Dim PurchaseType As String _
= DirectCast( IIf( drwData.Item("D ailyItem").Equa ls( "True" ) _
, "ID" _
, "PID" _
), String )

(line breaks for clarity(?) only)

HTH,
Phill W.
Jan 27 '06 #6
Yes... but no...

As David observed - the *correct* replacement is If / Else / End If

The difference is that IIf always evaluates both sides, where-as ? and the
If / Else / End If construct only evaluate the side appropriate based on the
logical test.

So yes, IIf looks very similar, but it doesn't work the same, and you could
end up running more methods than you want; bad if the two sides are
expensive functions (for example).

You get similar problems with short-circuiting boolean expressions - hence
VB.Net introducing the AndAlso / OrElse operators (not present in VB6).

Aside: to quote Michael S a while ago:
I think we should have the OrElse keyword in C#. But only as an empty
keyword to threaten the compiler:

myList.Add(mySt ring) OrElse;

Or maybe it could be used to eat exceptions. Hey, that would be quite
handy sometimes.
One of my favourite quotes; another from my top 10 (also from Michael S):
Visual Basic Sucks So Hard It Bends Light.


Marc
Jan 27 '06 #7
> One of my favourite quotes; another from my top 10 (also from Michael S):

Visual Basic Sucks So Hard It Bends Light.

Marc


Never heard that before. Even though I don't like VB, after a comment
like that I feel sorry for it.

Scott
Jan 27 '06 #8
To be fair, the full post went into quite some justification; look in the
archives if you want to see them.

I didn't want to start (yet another) VB dissin' session, so I'll stop here
;-p

Marc
Jan 27 '06 #9
Phil,
If your on VS 2005, check out my generic IIf (link previously posted), you
can simplify your statement to:

| Dim PurchaseType As String _
| = IIf( drwData.Item("D ailyItem").Equa ls( "True" ) _
| , "ID" _
| , "PID" _
| )

The above line is with Option Strict On! My generic IIf uses generics & type
inference to "create" a "IIf(boolea n, String, String) As String" as needed
and call it, no ugly DirectCast needed, no boxing of value types. Totally
Option Strict On friendly!
Of course there may be side effects...

Consider the following:

' note we are explicitly setting the seed values in each case to
' ensure we get the same sequence of pseudo random numbers back.

Dim falseCounter As New Random(1)
Dim trueCounter As New Random(2)
Debug.WriteLine (IIf(True, trueCounter.Nex t(), falseCounter.Ne xt()),
"true")
Debug.WriteLine (IIf(False, trueCounter.Nex t(), falseCounter.Ne xt()),
"false")

In VB it displays:

true: 1655911537
false: 237820880

While in alleged direct equivalent in C#:

Random falseCounter = new Random(1);
Random trueCounter = new Random(2);
Debug.WriteLine (true ? trueCounter.Nex t() : falseCounter.Ne xt(),
"true");
Debug.WriteLine (false ? trueCounter.Nex t() :
falseCounter.Ne xt(), "false");

Displays:

true: 1655911537
false: 534011718
Notice that the second line doesn't match, this is because VB called the
falseCounter.Ne xt function twice (once on each line), while C# only called
it once (only on the second line).

If instead of IIf we use the code that Sean gave:

Dim value As Integer
If True Then
value = trueCounter.Nex t()
Else
value = falseCounter.Ne xt()
End If
Debug.WriteLine (value, "true")
If False Then
value = trueCounter.Nex t()
Else
value = falseCounter.Ne xt()
End If
Debug.WriteLine (value, "false")

We then get the same results as C#:

true: 1655911537
false: 534011718

Consider what happens if instead of calling Random.Next() you are calling a
GetNextSequence Number function, that simply increments a counter & return
it,

id = IIf(sequenceAlr eadyExists, theExistingSequ ence,
GetNextSequence Number())

For example you are creating records from some input source and if the
record doesn't have an ID you call GetNextSequence Number to assign a new
one. The above line in VB will cause gaps in the sequence number, as is
called for every "line" in the input source. Consider what happens if the
GetNextSequence Number() function is fairly expensive (performance, memory
pressure, network, other) to execute.

As I stated, in the case of constants like the OP & your example, using IIf
doesn't hurt, however I strongly recommend any one using IIf to understand
what it is actually doing!

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Phill W." <p-***********@o-p-e-n.a-c.u-k> wrote in message
news:dr******** **@yarrow.open. ac.uk...
| ".Net Sports" <ba********@cox .net> wrote in message
| news:11******** *************@g 43g2000cwa.goog legroups.com...
| >I need to convert some C# ?Conditionals over to vb.net
| >
| > string PurchaseType = (Convert.ToStri ng(drwData["DailyItem"]) ==
| > "True") ? "ID" : "PID";
|
| The direct equivalent of the "?" operator is Visual Basic's IIf function:
|
| Dim PurchaseType As String _
| = DirectCast( IIf( drwData.Item("D ailyItem").Equa ls( "True" ) _
| , "ID" _
| , "PID" _
| ), String )
|
| (line breaks for clarity(?) only)
|
| HTH,
| Phill W.
|
|
Jan 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2337
by: tdi | last post by:
Ok, stupid question for the day. I'm reading the interview with Steve Moret and he says: "Once a lot of scripts started going in we knew there was no way we could back out of using Python." I'm just getting into Python and am wondering if I'm missing something or this is just a semantic issue. Thanks. -Ted
3
12015
by: nan | last post by:
Hi All, I am trying to connect the Database which is installed in AS400 using DB2 Client Version 8 in Windows box. First i created the Catalog, then when i selected the connection type as ODBC, then i am getting
15
1530
by: .Net Sports | last post by:
I need to convert some C# ?Conditionals over to vb.net (most likely in vb.net using If..Then statements), but the C#2VBNETconverter by KamalPatel isn't converting them: string PurchaseType = (Convert.ToString(drwData) == "True") ? "ID" : "PID"; XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ? "Highlight" : "Selection");
19
3902
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.