Collection

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

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
 
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?
 
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 = 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 said:
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 said:
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
 
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);

//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 said:
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 = 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 said:
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 said:
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
 
Sorry wrong Post

Gustavo

Franco said:
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);

//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 said:
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 = 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 said:
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?

:

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

 
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 said:
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 = 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 said:
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 said:
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
 
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 said:
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 said:
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 = 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 said:
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?

:

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

 
Back
Top