C# equivalent to DirectCast.

  • Thread starter Thread starter HolyHarley
  • Start date Start date
H

HolyHarley

I have been working in VB.net and have been using the combobox to
store data I need when the user selects an item. To do this, I have
been creating a class of some data.

Private Class Relationship

Public _code As String
Public _description As String
Public _default As Boolean

Public Sub New(ByVal sCode As String, ByVal sDescription As
String, ByVal bDefault As Boolean)

Me._code = sCode.Trim & ""
Me._description = sDescription.Trim & ""
Me._default = bDefault

End Sub

Public ReadOnly Property ListEntry() As String
Get
Return Me._code & " -- " & Me._description
End Get
End Property

End Class

I then load the combobox with data.

Private Sub LoadRelationship()

Try

Dim dr As SqlDataReader = _dropDown.GetRelationship

Me.cboRelationship.DisplayMember = "ListEntry"

While dr.Read

Me.cboRelationship.Items.Add(New
Relationship(dr.Item("code"), dr.Item("description"),
dr.Item("initial")))

End While

Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Information +
MsgBoxStyle.OKOnly, "frmXXClientAddEdit.LoadRelationship Error...")
End Try

End Sub


Then when the user select an item, I cam access the code, description
or initial using the following:

If Me.cboRelationship.SelectedIndex <> -1 Then
.RelationCode = DirectCast(Me.cboRelationship.Items(Me.cboRelationship.SelectedIndex),
Relationship)._code
Else
.RelationCode = ""
End If

I found this very useful, especially when you expand the data stored
in the combobox.

In C# I have been able to get to the point where the data is stored in
the combobox, but I have not been able to get the data out. I will
need to access both the ID (int) field and the Description (string)
values, so the valuemember does not appear to cut it.

Appreciate any help.

Thanks.

Jeff
 
HolyHarley said:
I have been working in VB.net and have been using the combobox to
store data I need when the user selects an item. To do this, I have
been creating a class of some data.

In C# I have been able to get to the point where the data is stored in
the combobox, but I have not been able to get the data out. I will
need to access both the ID (int) field and the Description (string)
values, so the valuemember does not appear to cut it.

I haven't checked the spec, but I *suspect* that DirectCast in VB.NET
is equivalent to just casting in C#:

Description d = (Description) cboRelationship.SelectedItem;
 
Hi

I will use your VB example for the combo. I assume your question is about
the casting

If (this.cboRelationship.SelectedIndex <> -1 ){
this.RelationCode =
((Relationship)this.cboRelationship.SelectedItem)._code;

//or you can do this
this.RelationCode = (this.cboRelationship.SelectedItem as
Relationship)._code;

} else { this.RelationCode = ""; }

Henk
 
Henk Verhoeven said:
I will use your VB example for the combo. I assume your question is about
the casting

If (this.cboRelationship.SelectedIndex <> -1 ){
this.RelationCode =
((Relationship)this.cboRelationship.SelectedItem)._code;

//or you can do this
this.RelationCode = (this.cboRelationship.SelectedItem as
Relationship)._code;

If you're not going to test the result against null, it's usually
clearer just to cast, IMO:

((Relationship)cboRelationship.SelectedItem)._code

I only use "as" if there's some doubt as to whether or not the value is
either null or some other type.
 
Hank and Jon,
Thanks so much for your help. That is precisely what I was looking for.

For the most part, transitioning to C# has been pretty easy. Some of
the details are not always simple.

Thanks again,

Jeff
 
Jeffrey, feel free to download our Instant C# Demo Edition
(www.instantcsharp.com). You'll be able to get any C# equivalents
just by typing the code snippet into the converter (without paying
anything!).

We also provide support for our Demo Edition, so if you have any
questions we'll answer them.
 
Back
Top