Click here to Skip to main content
15,910,083 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
structure is a value type and can't have null values then why it behaves different in vb and c#?
XML
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim c As System.Drawing.Color
        If c <> Nothing Then
            MessageBox.Show(c.Name)
        Else
            MessageBox.Show("null")
        End If
    End Sub
    Public Enum Align
        Left
        Center
        Right
    End Enum
    Public Alignment As Align
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        If Alignment <> Nothing Then
            MessageBox.Show("true")
        Else
            MessageBox.Show("false")
        End If
    End Sub


C#
private void button1_Click(object sender, EventArgs e)
        {
            System.Drawing.Color c = default(System.Drawing.Color);
            if (c != null)
            {
                MessageBox.Show(c.Name);
            }
            else
            {
                MessageBox.Show("null");
            }
        }
        public enum Align
        {
            Left,
            Center,
            Right
        }

        public Align Alignment;

        private void button2_Click(object sender, EventArgs e)
        {
            if (Alignment != null)
            {
                MessageBox.Show("true");
            }
            else
            {
                MessageBox.Show("false");
            }
        }
Posted

1 solution

Why structure behaves differently in vb and c#?

It doesn't. What you're seeing in your code is the difference between null in C# and Nothing in VB.NET.

null is a non-reference and only that. Structures and Enums are value types, not reference types, so you can't assign null to them.

Nothing is the default value of whatever you assign it to. For reference types, it's the same as null. For value types, it's their default value, so 0 in general. Structures and Enums have a default value, so you can assign Nothing to them.

So, Nothing in VB.NET is basically the null and default(T) of C# in one.
 
Share this answer
 
v5
Comments
Sid_Joshi 28-Apr-15 2:56am    
then what condition i should use in c# for
if (c != null) and
if (Alignment != null)?
Sascha Lefèvre 28-Apr-15 3:02am    
if(c != default(System.Drawing.Color))

if(Alignment != default(Align))

But for Enums I wouldn't recommend that. I would rather define the 0-value-enum-member as a kind of "invalid"-member:
public enum Align
{
Invalid,
Left,
Center,
Right
}
..and then test like this: if(Alignment != Align.Invalid)
Sid_Joshi 28-Apr-15 3:44am    
Thank you
I changed my code if(!pv_ForeColor.IsEmpty)
and if (_Alignment != TableViewerCell.Text_Align.Left)
according to your solution..

Thank You Again....
Sascha Lefèvre 28-Apr-15 3:45am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900