C# newby

M

maria

Hi all
I used to work with VB, and now I am trying C# and can't get it start for
example in VB
Dim Items() as tItems
dim NumberOfitems as long
obj.GetAllItems(NumberOfitems, Items())
for i=0 to NumberOfitems
y=Items(i).ID
x=Items(i).Name
next

in C#:
using ComObj;

ComObj.clsObj object = new ComObj.clsObj();

ComObj.clsObj obj = object;

ComObj.tItems Items= new tItems();
int NumberOfitems;

obj.GetAllItems(ref NumberOfitems, ref Items())

When I build it gives me an error in Error List:
1.Items is a 'variable' but it used like a 'method'.

If I will change function called this way:

obj.GetAllItems(ref NumberOfitems, ref (Array) Items())

the output error is :
1. can not convert type ComObj.tItems to System.Array.
2. Argument 2 must be passed with ref keyword
3. The best overloaded method match for ComObj.clsObj.GetAllItems(ref int,
ref System.Array) has some invalid arguments.

If anyone has an idea on how I should declare the Items() that it will be
recognized as array, or any idea how to handle this script please replay.
Any help is greatly appreciated.
 
J

John Murray

If I understand your question correctly, try:

obj.GetAllItems(ref NumberOfitems, ref ComObj.tItems)
 
M

maria

Thank you so much for you replay.
I was trying to do the way you said, but again the error message appear in
the ErrorList:
"ComObj.tItems" is a type ,which is not valid in the given context.
 
B

Bruce Wood

I don't read VB all that well, but I'll explain line by line and maybe
you can catch any errors.

Dim Items() as tItems

I'm guessing that what you're doing here is declaring "Items" to be an
array of tItems. In C#, you need to say this:

tItems[] Items;

which doesn't actually create an array, but declares Items as something
that could point to an array of tItems, when you do create one.

dim NumberOfitems as long

You got this right in C#; it would be something like this:

long NumberOfItems;

obj.GetAllItems(NumberOfitems, Items())

I'm guessing that in VB things are passed by reference by default?
Seems odd, but the rest of your example would require this. In C#,
since you haven't assigned values to either the array or the number of
items yet, you have to say this:

obj.GetAllItems(out NumberOfItems, out Items);

There's no need to put parentheses after "Items" to show that it's an
array. C# already knows that, since you declared it as one.
Furthermore, C# uses brackets [] not parentheses () to indicate
arrrays.

Now, this assumes that somewhere within GetAllItems you say:

Items = new tItems[n];

where "n" is the number of items you're going to return. Since arrays
can't change size, people often put their results in an ArrayList
first, then convert it to an array at the end of the method (in your
case at the end of GetAllItems) like this:

ArrayList result = new ArrayList();
....
result.Add(anItem);
....
Items = tItems[])result.ToArray(typeof(tItem));

Your loop in VB:

for i=0 to NumberOfitems
y=Items(i).ID
x=Items(i).Name
next

is very similar in C#:

for (int i = 0; i < NumberOfItems; i++)
{
y = Items.ID;
x = Items.Name;
}

Assuming, of course, that you've declared "y" and "x" somewhere.

However, if the point of the method is to fetch an array of items from
somewhere, I would change its signature like this:

tItems[] Items = obj.GetAllItems();

since an array knows its size, you can then do this:

for (int i = 0; i < Items.Length; i++)
{
}

and get the same effect as before.
 
M

maria

Hi Bruce.

Appreciate your help. Thanks for taking your time to answer me.

This tree lines still cause me a problem.

tItems[] Items;

long NumberOfItems;

obj.GetAllItems(ref NumberOfItems, ref Items);

As soon as I build them I get an error:

"Argument 2: Can not convert from ref tItems to ref System.Array"



If I will change the ref to out

The error is:

"1.Argument 1; must be passed with ref keyword.

2.Argument 2; can not convert from out Items to ref System.Array"



If I will try the third suggestion;tItems[] Items = obj.GetAllItems();
The errror is :

"1.No overload for method GetAllItems takes 0 arguments."
 
B

Bruce Wood

Well, you would have to change the definition of GetAllItems
accordingly. In other words, if you're going to change "ref" to "out",
then both the call to GetAllItems and the definition of GetAllItems
need to change. If you're going to use a return value, then both the
call to GetAllItems and the definition of GetAllItems need to change.

You never posted the code for GetAllItems... is it something you wrote
yourself, or something that was provided to you that you cannot change?
 
M

maria

Yes. This is com object written in VB6, to which I reference, which provides
me thefunctioanality to access the db, and I can not change it.
Do you know if I could use it?
 
B

Bruce Wood

If you can't change the GetAllItems code because it's in VB6, then you
have no choice but to do something like this:

System.Array itemArray;
long numberOfItems;

obj.GetAllItems(ref numberOfItems, ref itemArray);

Then the problem is how to get information out of the boring, vanilla
System.Array that is itemArray into a structure that is more useful to
you. To start with you should probably find out what object types the
method is returning in the array. I usually do that the "cheesy" way,
like this:

System.Windows.Forms.MessageBox.Show(itemArray[0].GetType());

Once you know what you have in your array, you can start to figure out
how to put it in a more useful structure.
 

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