Pass string to method that calls for object

M

mrgr8avill

Hello, all.

First off, I apologize for my ignorance. I am not used to working with non-variants, and it has been a LONG time since
I dealt with OOP.

I need to pass a string to a compiled method that requires an array of objects, like:

execute(string[],object[])

it was obviously created as an object because it was meant to accept input from the page form, but I want to send it
information from a datatable instead. Here's the relevant code:

---------------------

DataTable myTable = ExecuteDataTable("merge","SELECT * FROM merge");

string[] fieldNames = new string[myTable.Rows.Count];
object[] fieldValues = new string[myTable.Rows.Count];

for (int i = 0; i < myTable.Rows.Count; i++)
{
DataRow row = myTable.Rows[ i ];
// DataColumn col = myTable.Columns[];
fieldNames = row["Fieldname"];
fieldValues = row["FieldValue"]; //This is what needs to be an object instead of a string
}
--------------------


Here, of course, the compiler throws fits:

-------------------

Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

shan.aspx.cs(133,31): error CS0029: Cannot implicitly convert type 'object' to 'string'

---------------------

I vaguely remember creating objects from javascript, but only as a long shdowed dream. I really would appreciate any
help you might give in getting this to work, either by helping me convert the FieldValue to an object or any other
workaround.

Thanks again.

Shannon
 
G

Guest

Build the program again in Visual Studio, ad when you see the error message,
double-click it. Just prior to the highlighted code, insert the following:

(string)

This will implicitly cast it to the string you require, or I'm a Dutchman's
uncle!

HTH,

Arnie Van Huiesen
 
M

mrgr8avill

Thanks, Arnie, but two problems:

1) I don't have VS.NET (got out of the gig way back in VS6, and can't afford it just to keep my own stuff going), and

2) the class the method belong to is in third party compiled code - no access to the source.

So I will not be able to determine your family heritage for you :) But anything else you have would be highly
appreciated.

Thanks again

Shannon



Build the program again in Visual Studio, ad when you see the error message,
double-click it. Just prior to the highlighted code, insert the following:

(string)

This will implicitly cast it to the string you require, or I'm a Dutchman's
uncle!

HTH,

Arnie Van Huiesen

mrgr8avill said:
Hello, all.

First off, I apologize for my ignorance. I am not used to working with non-variants, and it has been a LONG time since
I dealt with OOP.

I need to pass a string to a compiled method that requires an array of objects, like:

execute(string[],object[])

it was obviously created as an object because it was meant to accept input from the page form, but I want to send it
information from a datatable instead. Here's the relevant code:

---------------------

DataTable myTable = ExecuteDataTable("merge","SELECT * FROM merge");

string[] fieldNames = new string[myTable.Rows.Count];
object[] fieldValues = new string[myTable.Rows.Count];

for (int i = 0; i < myTable.Rows.Count; i++)
{
DataRow row = myTable.Rows[ i ];
// DataColumn col = myTable.Columns[];
fieldNames = row["Fieldname"];
fieldValues = row["FieldValue"]; //This is what needs to be an object instead of a string
}
--------------------


Here, of course, the compiler throws fits:

-------------------

Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

shan.aspx.cs(133,31): error CS0029: Cannot implicitly convert type 'object' to 'string'

---------------------

I vaguely remember creating objects from javascript, but only as a long shdowed dream. I really would appreciate any
help you might give in getting this to work, either by helping me convert the FieldValue to an object or any other
workaround.

Thanks again.

Shannon
 
T

Tom Porterfield

mrgr8avill said:
I need to pass a string to a compiled method that requires an array of objects, like:

execute(string[],object[])

it was obviously created as an object because it was meant to accept input from the page form, but I want to send it
information from a datatable instead. Here's the relevant code:

DataTable myTable = ExecuteDataTable("merge","SELECT * FROM merge");

string[] fieldNames = new string[myTable.Rows.Count];
object[] fieldValues = new string[myTable.Rows.Count];

for (int i = 0; i < myTable.Rows.Count; i++)
{
DataRow row = myTable.Rows[ i ];
// DataColumn col = myTable.Columns[];
fieldNames = row["Fieldname"];
fieldValues = row["FieldValue"]; //This is what needs to be an object instead of a string
}

Here, of course, the compiler throws fits:
-------------------
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

shan.aspx.cs(133,31): error CS0029: Cannot implicitly convert type 'object' to 'string'
---------------------


The line that is giving the error is:

fieldNames = row["Fieldname"];

This is because when you access a column on a row that way the returned
type is object and you are trying to insert that object into an array of
string. You want to convert to string first. Options include casting
as Arnie stated. You can also use Convert.ToString(row["Fieldname"]).
 
M

mrgr8avill

Geeze -- see, I TOLD you I was ignorant. Good LORD!!!!!!!

Thank you very much for your help -- I am going to go soak my head in the toilet...

No WONDER it said object to string instead of string to object.

Glad I get mail every day, or I'd probably forget my own name.

Thanks again, Tom, I really appreciate it.

Shannon


mrgr8avill said:
I need to pass a string to a compiled method that requires an array of objects, like:

execute(string[],object[])

it was obviously created as an object because it was meant to accept input from the page form, but I want to send it
information from a datatable instead. Here's the relevant code:

DataTable myTable = ExecuteDataTable("merge","SELECT * FROM merge");

string[] fieldNames = new string[myTable.Rows.Count];
object[] fieldValues = new string[myTable.Rows.Count];

for (int i = 0; i < myTable.Rows.Count; i++)
{
DataRow row = myTable.Rows[ i ];
// DataColumn col = myTable.Columns[];
fieldNames = row["Fieldname"];
fieldValues = row["FieldValue"]; //This is what needs to be an object instead of a string
}

Here, of course, the compiler throws fits:
-------------------
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

shan.aspx.cs(133,31): error CS0029: Cannot implicitly convert type 'object' to 'string'
---------------------


The line that is giving the error is:

fieldNames = row["Fieldname"];

This is because when you access a column on a row that way the returned
type is object and you are trying to insert that object into an array of
string. You want to convert to string first. Options include casting
as Arnie stated. You can also use Convert.ToString(row["Fieldname"]).
 

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