473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Collection

I'm going from VB to C#.

I have example in VB:
Dim myParams As New Collection
Dim par 1 as SqlParameter

par1 = New SqlParameter("@mediaId", SqlDbType.NVarChar, 10)
par1.Value = lstMedia.Items(lstMedia.SelectedIndex).Value
myParams.Add(par1)

I would like to do the similar in C#:

SqlParameterCollection[] myParams=new SqlParameterCollection[3];
SqlParameter par1;
par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1); - - there is no ADD method ?

Any solution?

Regards,Simon
Nov 17 '05 #1
6 3706
So you want an array of collections? Any particular reason why?

SqlParamaterCollection does not have a publically available constructor so
am I correct in assuming that this is why you are using an array of the
collections?

"simon" wrote:
I'm going from VB to C#.

I have example in VB:
Dim myParams As New Collection
Dim par 1 as SqlParameter

par1 = New SqlParameter("@mediaId", SqlDbType.NVarChar, 10)
par1.Value = lstMedia.Items(lstMedia.SelectedIndex).Value
myParams.Add(par1)

I would like to do the similar in C#:

SqlParameterCollection[] myParams=new SqlParameterCollection[3];
SqlParameter par1;
par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1); - - there is no ADD method ?

Any solution?

Regards,Simon

Nov 17 '05 #2
SqlParameterCollection[] myParams=new SqlParameterCollection[3];

A fixed array doesn't have a .Add method because it is "fixed".

You can do:

par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
replace
myParams.Add(par1); - - there is no ADD method ?
by
myParams[i] = par1; // where i is from the index (0 to 2 ) of yours fixed
array.

The best solution.

Replace:
SqlParameterCollection[] myParams=new SqlParameterCollection[3];
by
SqlParameterCollection myParams=new ArrayList();
then you can use
myParams.Add(par1); - now there is ADD method!!!
Gustavo.

"Brian Delahunty" <Br************@discussions.microsoft.com> wrote in
message news:F8**********************************@microsof t.com...
So you want an array of collections? Any particular reason why?

SqlParamaterCollection does not have a publically available constructor so
am I correct in assuming that this is why you are using an array of the
collections?

"simon" wrote:
I'm going from VB to C#.

I have example in VB:
Dim myParams As New Collection
Dim par 1 as SqlParameter

par1 = New SqlParameter("@mediaId", SqlDbType.NVarChar, 10)
par1.Value = lstMedia.Items(lstMedia.SelectedIndex).Value
myParams.Add(par1)

I would like to do the similar in C#:

SqlParameterCollection[] myParams=new SqlParameterCollection[3];
SqlParameter par1;
par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1); - - there is no ADD method ?

Any solution?

Regards,Simon

Nov 17 '05 #3
this is a more elegant solution:

try this and let me know if it works.

public class MyComparer2 : IComparer
{
private SortedList mSortedList;

public SortedList SortedList
{
get{return mSortedList;}
set{mSortedList = value;}
}

int IComparer.Compare(Object x, Object y)
{
int keyLeft = (int) x;
int keyRight = (int) y;
return((new CaseInsensitiveComparer()).Compare(mSortedList[y],
mSortedList[x]));
}
}

public void test()
{
MyComparer2 comparer = new MyComparer2();
SortedList sortedList = new SortedList(comparer);
comparer.SortedList = sortedList;

sortedList.Add(1,3);
sortedList.Add(2,4);
sortedList.Add(3,2);
sortedList.Add(4,5);
sortedList.Add(5,1);

//Here we look for keys....
Console.WriteLine(sortedList[3]); // the return object is "2";

//The SortedList was dinamically sorted by the second column when the items
are loaded.
foreach(int i in sortedList.Keys)
Console.WriteLine("Index:" + sortedList.IndexOfKey(i) + " Key:" + i + "
Value:" + sortedList[i]);

//It should shows
//Index:1 Key: 5 Value: 1
//Index:2 Key: 3 Value: 2
//Index:3 Key: 1 Value: 3
//Index:4 Key: 2 Value: 4
//Index:5 Key: 4 Value: 5
}

Gustavo

"Franco, Gustavo" <gustavo_franco[REMOVEIT]@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP15.phx.gbl...
SqlParameterCollection[] myParams=new SqlParameterCollection[3];

A fixed array doesn't have a .Add method because it is "fixed".

You can do:

par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
replace
myParams.Add(par1); - - there is no ADD method ?
by
myParams[i] = par1; // where i is from the index (0 to 2 ) of yours fixed
array.

The best solution.

Replace:
SqlParameterCollection[] myParams=new SqlParameterCollection[3];
by
SqlParameterCollection myParams=new ArrayList();
then you can use
myParams.Add(par1); - now there is ADD method!!!
Gustavo.

"Brian Delahunty" <Br************@discussions.microsoft.com> wrote in
message news:F8**********************************@microsof t.com...
So you want an array of collections? Any particular reason why?

SqlParamaterCollection does not have a publically available constructor
so
am I correct in assuming that this is why you are using an array of the
collections?

"simon" wrote:
I'm going from VB to C#.

I have example in VB:
Dim myParams As New Collection
Dim par 1 as SqlParameter

par1 = New SqlParameter("@mediaId", SqlDbType.NVarChar, 10)
par1.Value = lstMedia.Items(lstMedia.SelectedIndex).Value
myParams.Add(par1)

I would like to do the similar in C#:

SqlParameterCollection[] myParams=new SqlParameterCollection[3];
SqlParameter par1;
par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1); - - there is no ADD method ?

Any solution?

Regards,Simon


Nov 17 '05 #4
Sorry wrong Post

Gustavo

"Franco, Gustavo" <gustavo_franco[REMOVEIT]@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
this is a more elegant solution:

try this and let me know if it works.

public class MyComparer2 : IComparer
{
private SortedList mSortedList;

public SortedList SortedList
{
get{return mSortedList;}
set{mSortedList = value;}
}

int IComparer.Compare(Object x, Object y)
{
int keyLeft = (int) x;
int keyRight = (int) y;
return((new CaseInsensitiveComparer()).Compare(mSortedList[y],
mSortedList[x]));
}
}

public void test()
{
MyComparer2 comparer = new MyComparer2();
SortedList sortedList = new SortedList(comparer);
comparer.SortedList = sortedList;

sortedList.Add(1,3);
sortedList.Add(2,4);
sortedList.Add(3,2);
sortedList.Add(4,5);
sortedList.Add(5,1);

//Here we look for keys....
Console.WriteLine(sortedList[3]); // the return object is "2";

//The SortedList was dinamically sorted by the second column when the
items are loaded.
foreach(int i in sortedList.Keys)
Console.WriteLine("Index:" + sortedList.IndexOfKey(i) + " Key:" + i + "
Value:" + sortedList[i]);

//It should shows
//Index:1 Key: 5 Value: 1
//Index:2 Key: 3 Value: 2
//Index:3 Key: 1 Value: 3
//Index:4 Key: 2 Value: 4
//Index:5 Key: 4 Value: 5
}

Gustavo

"Franco, Gustavo" <gustavo_franco[REMOVEIT]@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP15.phx.gbl...
SqlParameterCollection[] myParams=new SqlParameterCollection[3];

A fixed array doesn't have a .Add method because it is "fixed".

You can do:

par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
replace
myParams.Add(par1); - - there is no ADD method ?
by
myParams[i] = par1; // where i is from the index (0 to 2 ) of yours
fixed array.

The best solution.

Replace:
SqlParameterCollection[] myParams=new SqlParameterCollection[3];
by
SqlParameterCollection myParams=new ArrayList();
then you can use
myParams.Add(par1); - now there is ADD method!!!
Gustavo.

"Brian Delahunty" <Br************@discussions.microsoft.com> wrote in
message news:F8**********************************@microsof t.com...
So you want an array of collections? Any particular reason why?

SqlParamaterCollection does not have a publically available constructor
so
am I correct in assuming that this is why you are using an array of the
collections?

"simon" wrote:

I'm going from VB to C#.

I have example in VB:
Dim myParams As New Collection
Dim par 1 as SqlParameter

par1 = New SqlParameter("@mediaId", SqlDbType.NVarChar, 10)
par1.Value = lstMedia.Items(lstMedia.SelectedIndex).Value
myParams.Add(par1)

I would like to do the similar in C#:

SqlParameterCollection[] myParams=new SqlParameterCollection[3];
SqlParameter par1;
par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1); - - there is no ADD method ?

Any solution?

Regards,Simon



Nov 17 '05 #5
Thank you Franco,

If I replace like you said

SqlParameterCollection myParams=new ArrayList();

I get an error:

"Cannot implicitly convert type 'System.Collections.ArrayList' to
'System.Data.SqlClient.SqlParameterCollection' "

Why?

But it works if I do:

ArrayList() myParams=new ArrayList();

Regards,Simon

"Franco, Gustavo" <gustavo_franco[REMOVEIT]@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP15.phx.gbl...
SqlParameterCollection[] myParams=new SqlParameterCollection[3];

A fixed array doesn't have a .Add method because it is "fixed".

You can do:

par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
replace
myParams.Add(par1); - - there is no ADD method ?
by
myParams[i] = par1; // where i is from the index (0 to 2 ) of yours fixed
array.

The best solution.

Replace:
SqlParameterCollection[] myParams=new SqlParameterCollection[3];
by
SqlParameterCollection myParams=new ArrayList();
then you can use
myParams.Add(par1); - now there is ADD method!!!
Gustavo.

"Brian Delahunty" <Br************@discussions.microsoft.com> wrote in
message news:F8**********************************@microsof t.com...
So you want an array of collections? Any particular reason why?

SqlParamaterCollection does not have a publically available constructor
so
am I correct in assuming that this is why you are using an array of the
collections?

"simon" wrote:
I'm going from VB to C#.

I have example in VB:
Dim myParams As New Collection
Dim par 1 as SqlParameter

par1 = New SqlParameter("@mediaId", SqlDbType.NVarChar, 10)
par1.Value = lstMedia.Items(lstMedia.SelectedIndex).Value
myParams.Add(par1)

I would like to do the similar in C#:

SqlParameterCollection[] myParams=new SqlParameterCollection[3];
SqlParameter par1;
par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1); - - there is no ADD method ?

Any solution?

Regards,Simon


Nov 17 '05 #6
Sorry it was a typo error.

I replied the email without analyze the code.

You have a problem there.

You are creating a fixed array with dimension 3 of collections
(SqlParameterCollection[] myParams=new SqlParameterCollection[3]). I guess
you did that because SqlParameterCollection doesn't have a default
constructors, but that doesn't mean you are creating an instance to
SqlParameterCollection, it just creates 3 pointers or references to
SqlParameterCollection objects;

Basically, I see you can't use SqlParameterCollection and it is used for
..net framework to return to you a collection of parameters from same another
method.

ArrayList myParams=new ArrayList();
SqlParameter par1;

par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1);

This is the code that you are looking for.

Why this doesn't work?
SqlParameterCollection myParams=new ArrayList();

SqlParameterCollection and ArrayList are two totally different classes, so
ArrayList can't be "cast" to SqlParameterCollection.

It is like having a square and a circle both having the same surface and
trying to fit the square inside the circle.

Gustavo

"simon" <si*********@stud-moderna.si> wrote in message
news:uz*************@TK2MSFTNGP09.phx.gbl...
Thank you Franco,

If I replace like you said

SqlParameterCollection myParams=new ArrayList();

I get an error:

"Cannot implicitly convert type 'System.Collections.ArrayList' to
'System.Data.SqlClient.SqlParameterCollection' "

Why?

But it works if I do:

ArrayList() myParams=new ArrayList();

Regards,Simon

"Franco, Gustavo" <gustavo_franco[REMOVEIT]@hotmail.com> wrote in message
news:Or****************@TK2MSFTNGP15.phx.gbl...
SqlParameterCollection[] myParams=new SqlParameterCollection[3];

A fixed array doesn't have a .Add method because it is "fixed".

You can do:

par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
replace
myParams.Add(par1); - - there is no ADD method ?
by
myParams[i] = par1; // where i is from the index (0 to 2 ) of yours
fixed array.

The best solution.

Replace:
SqlParameterCollection[] myParams=new SqlParameterCollection[3];
by
SqlParameterCollection myParams=new ArrayList();
then you can use
myParams.Add(par1); - now there is ADD method!!!
Gustavo.

"Brian Delahunty" <Br************@discussions.microsoft.com> wrote in
message news:F8**********************************@microsof t.com...
So you want an array of collections? Any particular reason why?

SqlParamaterCollection does not have a publically available constructor
so
am I correct in assuming that this is why you are using an array of the
collections?

"simon" wrote:

I'm going from VB to C#.

I have example in VB:
Dim myParams As New Collection
Dim par 1 as SqlParameter

par1 = New SqlParameter("@mediaId", SqlDbType.NVarChar, 10)
par1.Value = lstMedia.Items(lstMedia.SelectedIndex).Value
myParams.Add(par1)

I would like to do the similar in C#:

SqlParameterCollection[] myParams=new SqlParameterCollection[3];
SqlParameter par1;
par1 = new SqlParameter("@mediaId", SqlDbType.NVarChar, 10);
par1.Value = lstMedia.Items[lstMedia.SelectedIndex].Value;
myParams.Add(par1); - - there is no ADD method ?

Any solution?

Regards,Simon



Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
11
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.