473,503 Members | 6,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 36784
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
7253
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
7745
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
1877
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
10812
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
5039
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
2107
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
2699
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
4091
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
1405
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
7183
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
7060
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
7437
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...
0
5551
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,...
0
4661
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...
0
3154
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...
0
3144
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
717
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
363
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...

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.