What's up with C#?

G

Guest

I'm a VB.net programmer currently battling to build new apps in C#, and MAN!
It's been a bit frustrating, to say the least.

I've done quite a bit of progamming with Java and C back in the day, so I
can deal with the case sensitiviness and semicolens, but I'm finding certain
aspects of the language to be a real pain.

Well, to get to the point, here's a problem that I'd appreciate it if
someone could help me...

Note: dsResponse is a dataset being populated by an xml file.

In code, I have the following:
//This works:
if (dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0).ToString()
== "Fail")

//This doesn't work
if (dsResponse.ReservationResponse.Rows[0].ItemArray["Status"] == "Fail")
error: Cannot implicitly convert type 'string' to 'int'

In the command window:
//this works
dsResponse.ReservationResponse.Rows[0].ItemArray["Status"]

//this DOESN'T:
dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0).ToString()
Error:
'dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0.ToString' does
not exist

What I would like to accomplish is to be able to use the NAME of the field,
and not the index value, to retrieve the value of that item.

WHAT GIVES?

tia
 
J

Jon Skeet [C# MVP]

tuuky said:
I'm a VB.net programmer currently battling to build new apps in C#, and MAN!
It's been a bit frustrating, to say the least.

I've done quite a bit of progamming with Java and C back in the day, so I
can deal with the case sensitiviness and semicolens, but I'm finding certain
aspects of the language to be a real pain.

Well, to get to the point, here's a problem that I'd appreciate it if
someone could help me...

Note: dsResponse is a dataset being populated by an xml file.

In code, I have the following:
//This works:
if (dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0).ToString()
== "Fail")

//This doesn't work
if (dsResponse.ReservationResponse.Rows[0].ItemArray["Status"] == "Fail")
error: Cannot implicitly convert type 'string' to 'int'

Indeed, because you can't index an array (ItemArray) by string. I don't
know why you're using the ItemArray property anyway though - why not
just use the indexer on the row itself?
In the command window:
//this works
dsResponse.ReservationResponse.Rows[0].ItemArray["Status"]

That sounds very odd. Are you sure you don't mean

dsResponse.ReservationResponse.Rows[0]["Status"]

?
//this DOESN'T:
dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0).ToString()
Error:
'dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0.ToString' does
not exist

What I would like to accomplish is to be able to use the NAME of the field,
and not the index value, to retrieve the value of that item.

So use

if (dsResponse.ReservationResponse.Rows[0]["Status"]=="Fail")
{
....
}

No problem.
 
G

Guest

Thanks a lot, Jon.

Part of the growing pains, I guess.

Jon Skeet said:
tuuky said:
I'm a VB.net programmer currently battling to build new apps in C#, and MAN!
It's been a bit frustrating, to say the least.

I've done quite a bit of progamming with Java and C back in the day, so I
can deal with the case sensitiviness and semicolens, but I'm finding certain
aspects of the language to be a real pain.

Well, to get to the point, here's a problem that I'd appreciate it if
someone could help me...

Note: dsResponse is a dataset being populated by an xml file.

In code, I have the following:
//This works:
if (dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0).ToString()
== "Fail")

//This doesn't work
if (dsResponse.ReservationResponse.Rows[0].ItemArray["Status"] == "Fail")
error: Cannot implicitly convert type 'string' to 'int'

Indeed, because you can't index an array (ItemArray) by string. I don't
know why you're using the ItemArray property anyway though - why not
just use the indexer on the row itself?
In the command window:
//this works
dsResponse.ReservationResponse.Rows[0].ItemArray["Status"]

That sounds very odd. Are you sure you don't mean

dsResponse.ReservationResponse.Rows[0]["Status"]

?
//this DOESN'T:
dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0).ToString()
Error:
'dsResponse.ReservationResponse.Rows[0].ItemArray.GetValue(0.ToString' does
not exist

What I would like to accomplish is to be able to use the NAME of the field,
and not the index value, to retrieve the value of that item.

So use

if (dsResponse.ReservationResponse.Rows[0]["Status"]=="Fail")
{
....
}

No problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top