Method Group?? Newbie

26 views
Skip to first unread message

UKuser

unread,
Nov 15, 2009, 3:39:19 PM11/15/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi Folks,

As a PHP developer, I've been playing with C# and am wondering why
this code produces an error:

protected void Page_Load(object sender, EventArgs e)
{
string test = this.outputTest();

OdbcConnection myConn = mySqlConnect("localhost", "test",
"test", "licensi_lwsite");
DataTable myData = SelectData("Select * FROM information",
myConn);

Dictionary<object, object> mylist = new Dictionary<object,
object>();

for (int i=0;i<myData.Rows.Count;i++)
{
mylist.Add = myData.Rows[i].ToString();
}

}

I get the following error:
Cannot assign to 'Add' because it is a 'method group'

I'm basically trying to create my own dictionary which is something
I've done in PHP very easily, but is proving more of a challenge in
C#.

Also can you end up in this situation:

Dictionary<object,objectX>
where objectX is another dictionary, or it could be a string or
integer? In PHP arrays can contain any values including arrays
(dictionarys), but I'm interested to see how flexible C# is?

Thanks

A

Cerebrus

unread,
Nov 16, 2009, 12:44:26 AM11/16/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Quite simple, so I'm sure you figured it out after you asked here.
Nevertheless, the answer follows:

There are two problems with your code sample:
~ The Add is a method, not a property so you cannot assign to it. The
method call expects parameters, namely the key and value of the item
to add to the Dictionary.
~ You seem to be (trying to) add column values but are actually adding
the result of ToString() on each DataRow in the table. For instance,
in the following table

---
1.1 1.2 1.3
2.1 2.2 2.3
3.1 3.2 3.3
---

.. the code (if it worked) would add "System.Data.DataRow" three
times. (or something like that... I haven't tried this.)

What you would need to do is first identify the column which will
serve as the Key and that which will be the value of each Dictionary
item. Then use something like follows :

---
foreach (DataRow dr in myData.Rows)
{
myList.Add(dr["KeyColumn"], dr["ValueColumn"]);
}
---

HTH.

UKuser

unread,
Nov 16, 2009, 6:13:50 AM11/16/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I was missing the obvious!! But I didn't notice it actually - just
"playing" with code really so glad to find the solution.

Thanks

A
Reply all
Reply to author
Forward
0 new messages