Re: Problems with (Cast)
- From: "Daniel Groh" <newsgroupms@xxxxxxxxx>
- Date: Sat, 21 May 2005 20:56:28 -0300
it works with int too (of course if is a number)...why not as a char ? i'll
try to do as you wrote!
<david.dawkins@xxxxxxxxx> escreveu na mensagem
news:1116717956.921399.278780@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Daniel Groh wrote:
>> I'm facing this error:
>> Error Message:Specified cast is not valid.
>>
>> this line:
>> char chrTipoPessoa = (char)sqlCmd.ExecuteScalar();
>>
>> It should returns the letter "F" or "J"!
>> but with (string) it works normal...I already checked and there's no
> space!
>>
>> Why I'm getting this error when I try to cast as char ?
>
> If it's this method:
>
> [C#]
> public virtual object ExecuteScalar();
>
> then you're not being returned an object that casts to a char
> value. I wouldn't expect a single-character string to cast to
> a char, although I didn't check.
>
> Try something like this, and then use the debugger to check the
> type of obj if it still doesn't work:
>
> object obj = sqlCmd.ExecuteScalar();
> if (obj is char) {
> return (char) obj;
> }
> if (obj is string) {
> string s = obj as string;
> return s.Length == 1 ? s[0] : 0; // yeuch, but you get the idea
> }
> throw new Exception( "Unexpected return from SQL command: " +
> obj.ToString() );
>
> etc etc
>
> I hope this helps
>
> Dave D
>
.
- Follow-Ups:
- Re: Problems with (Cast)
- From: Jon Skeet [C# MVP]
- Re: Problems with (Cast)
- References:
- Problems with (Cast)
- From: Daniel Groh
- Re: Problems with (Cast)
- From: david . dawkins
- Problems with (Cast)
- Prev by Date: Re: Is strict adherence to Adapter Pattern really a good thing?
- Next by Date: Get size (in pixels) of a string
- Previous by thread: Re: Problems with (Cast)
- Next by thread: Re: Problems with (Cast)
- Index(es):
Relevant Pages
|