473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

basic c# problem

Hi guys,
I work mostly with vb.net and today only started doping some C#. Can
some translate the following vb code in C#
'--Here conn is connection object
If Not (conn Is Nothing) Then
'-----
End If
Nov 17 '05 #1
7 1105
if (conn != null)
{
...do something
}

null is the c# equivalent of VB Nothing
! is c# equivalent of VB Not

Hope that helps
Mark R Dawson
http://www.markdawson.org

"dot Net Pa Ji" wrote:
Hi guys,
I work mostly with vb.net and today only started doping some C#. Can
some translate the following vb code in C#
'--Here conn is connection object
If Not (conn Is Nothing) Then
'-----
End If

Nov 17 '05 #2
> If Not (conn Is Nothing) Then
'-----
End If

if (!(conn == null)){//so something
}

I would do it in VBNet as well as this bellow

If Not conn Is Nothing then
'do something
End if

Than it becomes more as Mark showed you.

I hope this helps,

Cor
Nov 17 '05 #3
> null is the c# equivalent of VB Nothing

While that is true for reference types, Nothing is valid for value
types as well in VB. I *believe* that the default value for a member
variable of any particular type is the value of VB.NET's Nothing for
that same type - so for boolean it's false, for int it's 0 etc.

Not relevant in this particular example, but worth noting.

Jon

Nov 17 '05 #4
Hi Robinhood.

For your information in VB.Net is a difference between = Nothing and Is
Nothing.

= Nothing means for a value type empty or default
Is Nothing means for a reference type no reference.

By instance a string can have both

Setting to nothing is for both =

(Robin Hood because he came from Nothingham)

:-)

Cor

"Jon Skeet [C# MVP]" <jo*******@gmail.com> schreef in bericht
news:11**********************@g47g2000cwa.googlegr oups.com...
null is the c# equivalent of VB Nothing


While that is true for reference types, Nothing is valid for value
types as well in VB. I *believe* that the default value for a member
variable of any particular type is the value of VB.NET's Nothing for
that same type - so for boolean it's false, for int it's 0 etc.

Not relevant in this particular example, but worth noting.

Jon

Nov 17 '05 #5
Thanks for the information.
---DNPJ
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ee**************@TK2MSFTNGP14.phx.gbl...
Hi Robinhood.

For your information in VB.Net is a difference between = Nothing and Is
Nothing.

= Nothing means for a value type empty or default
Is Nothing means for a reference type no reference.

By instance a string can have both

Setting to nothing is for both =

(Robin Hood because he came from Nothingham)

:-)

Cor

"Jon Skeet [C# MVP]" <jo*******@gmail.com> schreef in bericht
news:11**********************@g47g2000cwa.googlegr oups.com...
null is the c# equivalent of VB Nothing


While that is true for reference types, Nothing is valid for value
types as well in VB. I *believe* that the default value for a member
variable of any particular type is the value of VB.NET's Nothing for
that same type - so for boolean it's false, for int it's 0 etc.

Not relevant in this particular example, but worth noting.

Jon


Nov 17 '05 #6
Cor Ligthert [MVP] <no************@planet.nl> wrote:
For your information in VB.Net is a difference between = Nothing and Is
Nothing.

= Nothing means for a value type empty or default
Is Nothing means for a reference type no reference.

By instance a string can have both


What makes a string different from other reference types though? That
doesn't make sense to me. Is it just to make it more like VB6?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
On 2005-09-21, Jon Skeet C# MVP <sk***@pobox.com> wrote:
Cor Ligthert [MVP] <no************@planet.nl> wrote:
For your information in VB.Net is a difference between = Nothing and Is
Nothing.

= Nothing means for a value type empty or default
Is Nothing means for a reference type no reference.

By instance a string can have both


What makes a string different from other reference types though? That
doesn't make sense to me. Is it just to make it more like VB6?


Pretty much. It's to make string compares work in a way similar to the
way they did in VB6. String comparisons are special-cased in VB.Net not
just for the reference/value type/empty means nothing/ issue, but
because VB also needs to support things like the Option Compare
statement, and there's some automatic culture stuff that's handled
during string compares (I doubt that one in a thousand VB developers
could tell you what String = String actually does in any detail.).

In VB.Net, for a string s

If s Is Nothing
works identically to if(s == null)

If s = Nothing

is equivalent to if(s == null || s == String.Empty)

Note this isn't polymorphic, you can't do

Dim s As Object = "Hello World"

If s = Nothing

That won't compile, because "= Nothing" is not valid for reference
types, except for Strings.
Nov 17 '05 #8

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

Similar topics

0
by: Ray | last post by:
Date problem. Using non American date formats with Microsoft Access and Visual Basic. I set my regional settings to English(Ireland) or English(United Kingdom) and my date appears in Access as...
10
by: John | last post by:
I have a problem, it's not with any code I have because... there is no code. When I run a blank visual basic 6 form, it opens up just fine. When I add a text box, a caption, and a button... it...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
4
by: Barry | last post by:
The MS fix for IE broke how users access our site (if they patch their browsers), so I need a solution to get users logged onto our site transparently. Basically we used to log on to the site...
3
by: sefe dery | last post by:
hi ng, i try to create a asp.net 1.0 website on windows server 2003(Servername: ServerX) with iis 6.0. PROBLEM: The user should login with his windows credentials in basic.aspx and...
21
by: Al Christoph | last post by:
I posted this last week end in the MSDN forums. No luck there. Let's see what the experts here have to say:-)))) I have a rather convoluted project. The distributable will come in eight...
4
by: sqlguy | last post by:
Why do we have to contact MS for a problem that has been with this compiler from at least the beta of VS 20005. I am so sick and tired of the 30 - 40 clicks it takes to dismiss VS when there is a...
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
1
by: Ronm | last post by:
Hey Guys, I have a problem which has been driving me crazy for the last days. I'm working on a small project involving: Visual Basic Acces Database ASP and adobe/macromedia Flash,
3
visualbasic1111
by: visualbasic1111 | last post by:
hello, I am a Bsc III yr student and I am working on a project of visual basic for our college's Library Management. My problem is - After using ADO data control in visual basic, when I run my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.