PC Review


Reply
Thread Tools Rate Thread

cannot convert from type to type.

 
 
=?Utf-8?B?Q2hyaXM=?=
Guest
Posts: n/a
 
      20th Sep 2005
Hi,

I have the following procedure below:
I am getting 2 errors on compile:

Argument '2': cannot convert from 'System.Data.SqlClient.SqlDataReader' to
'ref System.Data.SqlClient.SqlDataReader'

Argument '3': cannot convert from 'System.Text.StringBuilder' to 'ref
System.Text.StringBuilder'

the PrintChildrenRecursive is a class in a referenced DLL that was written in
VB.NET.

this function takes a dataset and loops through it creating a javastring
using stringbuilder that creates a parent-child tree of html hyperlinks.

the printchildrecursive function accepts the Dr and Sbuilder ByReference.

private static void PrintFromRoot(SqlDataReader Dr, StringBuilder SBuilder)
{
PrintChildren PCR = new PrintChildren();

SBuilder.Append("[");
PCR.PrintChildrenRecursive(1, Dr, SBuilder, false);

SBuilder.Remove(1,2); //remove comma and bracket from beginning of string

SBuilder.Append("];");
}

I don't understand why conversion is occuring because they are already of
the correct type as already indicated by the error messages.

thanks

Chris
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      20th Sep 2005
Chris,

You are passing parameters by reference. In order to do so, you need to
use the ref keyword, like this:

obj.PrintFromRoot(ref sqlDataReader, ref stringBuilder);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Chris" <(E-Mail Removed)> wrote in message
news:793DB326-36D6-4619-95FF-(E-Mail Removed)...
> Hi,
>
> I have the following procedure below:
> I am getting 2 errors on compile:
>
> Argument '2': cannot convert from 'System.Data.SqlClient.SqlDataReader' to
> 'ref System.Data.SqlClient.SqlDataReader'
>
> Argument '3': cannot convert from 'System.Text.StringBuilder' to 'ref
> System.Text.StringBuilder'
>
> the PrintChildrenRecursive is a class in a referenced DLL that was written
> in
> VB.NET.
>
> this function takes a dataset and loops through it creating a javastring
> using stringbuilder that creates a parent-child tree of html hyperlinks.
>
> the printchildrecursive function accepts the Dr and Sbuilder ByReference.
>
> private static void PrintFromRoot(SqlDataReader Dr, StringBuilder
> SBuilder)
> {
> PrintChildren PCR = new PrintChildren();
>
> SBuilder.Append("[");
> PCR.PrintChildrenRecursive(1, Dr, SBuilder, false);
>
> SBuilder.Remove(1,2); //remove comma and bracket from beginning of string
>
> SBuilder.Append("];");
> }
>
> I don't understand why conversion is occuring because they are already of
> the correct type as already indicated by the error messages.
>
> thanks
>
> Chris



 
Reply With Quote
 
=?Utf-8?B?Q2hyaXM=?=
Guest
Posts: n/a
 
      20th Sep 2005
Nicholas,

thank you so much!

"Nicholas Paldino [.NET/C# MVP]" wrote:

> Chris,
>
> You are passing parameters by reference. In order to do so, you need to
> use the ref keyword, like this:
>
> obj.PrintFromRoot(ref sqlDataReader, ref stringBuilder);
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "Chris" <(E-Mail Removed)> wrote in message
> news:793DB326-36D6-4619-95FF-(E-Mail Removed)...
> > Hi,
> >
> > I have the following procedure below:
> > I am getting 2 errors on compile:
> >
> > Argument '2': cannot convert from 'System.Data.SqlClient.SqlDataReader' to
> > 'ref System.Data.SqlClient.SqlDataReader'
> >
> > Argument '3': cannot convert from 'System.Text.StringBuilder' to 'ref
> > System.Text.StringBuilder'
> >
> > the PrintChildrenRecursive is a class in a referenced DLL that was written
> > in
> > VB.NET.
> >
> > this function takes a dataset and loops through it creating a javastring
> > using stringbuilder that creates a parent-child tree of html hyperlinks.
> >
> > the printchildrecursive function accepts the Dr and Sbuilder ByReference.
> >
> > private static void PrintFromRoot(SqlDataReader Dr, StringBuilder
> > SBuilder)
> > {
> > PrintChildren PCR = new PrintChildren();
> >
> > SBuilder.Append("[");
> > PCR.PrintChildrenRecursive(1, Dr, SBuilder, false);
> >
> > SBuilder.Remove(1,2); //remove comma and bracket from beginning of string
> >
> > SBuilder.Append("];");
> > }
> >
> > I don't understand why conversion is occuring because they are already of
> > the correct type as already indicated by the error messages.
> >
> > thanks
> >
> > Chris

>
>
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      20th Sep 2005
Chris <(E-Mail Removed)> wrote:
> Nicholas,
>
> thank you so much!


Note, however, that you very rarely really want to pass a StringBuilder
or a SqlDataReader by reference. Seeing code which does is *usually* an
indication of a lack of understanding of reference types and parameter
passing.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Import Convert Data Type from Type 1 to Type 2 u473 Microsoft Excel Programming 3 21st Oct 2008 08:22 PM
How can I convert System.Type variables to some database data type? lichaoir Microsoft C# .NET 0 12th Mar 2008 03:35 AM
error C2665 :none of the number1 overloads can convert parameter number2 from type 'type' Joseph Lu Microsoft VC .NET 2 17th Jul 2006 01:35 PM
Convert.ChangeType with reference type ie Int32& - can I get the base type? phancey@2bytes.co.uk Microsoft Dot NET 1 8th Feb 2005 03:31 PM
Convert Xerox .xif file type to any image type Rose Weir Freeware 1 22nd Mar 2004 06:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:02 AM.