473,385 Members | 1,942 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.

C# equivalent to DirectCast.

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.cboRelation ship.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
Nov 16 '05 #1
5 36780
HolyHarley <jc*****@TAISoftware.com> wrote:
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.
<snip>
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;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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

"HolyHarley" <jc*****@TAISoftware.com> wrote in message
news:51**************************@posting.google.c om...
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.cboRelation ship.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

Nov 16 '05 #3
Henk Verhoeven <henk.verhoeven@_nospam_bluecod.net.nonospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
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.

Nov 16 '05 #6

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

Similar topics

4
by: Andreas Klemt | last post by:
Hello, what has the better performance and what are you using? Dim myObj As Object = 70 a) Dim myInt As Integer = DirectCast(myObj, Integer) b) Dim myInt As Integer = Convert.ToInt32(myObj) ...
11
by: Tubs | last post by:
i am attempting to write something which can morph itself to whatever comes in and get the value property from it but i don't know what type it is until runtime. I am therefore trying to use...
13
by: Crirus | last post by:
Can I use DirectCast to convert a object to it's base or should I use CType? I have a instance of a class as Object. The run-time tipe is a derived of a class, but I need to refer to that instance...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
5
by: Michael Ramey | last post by:
Hello, There are quite a few ways to convert one object, say an integer to a string. Dim myStr as string dim myInt as integer = 123 myStr = cstr(myInt) myStr = myInt.toString()
6
by: Mark Nethercott | last post by:
I get the following failure when trying to access the builtin properties; An unhandled exception of type 'System.InvalidCastException' occurred in resultsoutput.dll Additional information:...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
1
by: iwdu15 | last post by:
can anyone explain the directcast code...ive tried using it and lookin it up but im lookin for an easy definition and how it works...ive tried using it before byut it throws errors saying it can...
4
by: John | last post by:
Hi What is the late binding equivalent of the below code? Many Thanks Regards Dim O As Outlook.Application
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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.