473,326 Members | 2,012 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,326 software developers and data experts.

C# newby

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.
Mar 17 '06 #1
7 4187
If I understand your question correctly, try:

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


maria wrote:
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.

Mar 17 '06 #2
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.
"John Murray" <jm*****@pluck.com> wrote in message
news:eV*************@TK2MSFTNGP12.phx.gbl...
If I understand your question correctly, try:

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


maria wrote:
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.

Mar 17 '06 #3
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[i].ID;
x = Items[i].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.

Mar 17 '06 #4
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."

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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[i].ID;
x = Items[i].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.

Mar 20 '06 #5
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?

Mar 20 '06 #6
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?

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
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?

Mar 20 '06 #7
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.

Mar 26 '06 #8

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

Similar topics

9
by: Damien | last post by:
I have just built a simple stopwatch application, but when i f5 to get things goings i get this message, An unhandled exception of type 'System.ArithmeticException' occurred in...
0
by: Pete | last post by:
Hi All, A total Newby with, possibly, a daft question? However, until I can get a reasonable explanation I am disinclined towards going further. Here goes: I recently downloaded the latest...
0
by: marco | last post by:
I'm trying to parse a xml bookmarkpage with php. I found a very useful example script about how you can parse a xml document with php. The scriptworks really smooth. The xml test document (See...
8
by: Ask | last post by:
G'day All, Just thought I'd drop in and say hi. I'm new to Python, but old to software development. Python is one of the languages used in my new job, so I've just bought a book, read it, and...
10
by: Fred Nelson | last post by:
Hi: I have programmed in VB.NET for about a year and I'm in the process of learing C#. I'm really stuck on this question - and I know it's a "newby" question: In VB.NET I have several...
4
by: Fred Nelson | last post by:
I have an applicatioin that I'm writing that uses a "case" file that contains over 350 columns and more may be added in the future. I would like to create a dataset with all the column names and...
4
by: Fred Nelson | last post by:
Hi: I'm developing a web application that needs to have five values, each retrieved from cookies on many pages. If I have five "Request.Cookies" commands together does this cause five "round...
2
by: Fred Nelson | last post by:
Hi: I'm working on a VS2005 web application and I have what is probabably a "newby" question. In VS2003 I could drag a textbox/button/etc on to a form and position it with the mouse. I...
2
by: johnnyG | last post by:
Greetings, I'm studying for the 70-330 Exam using the MS Press book by Tony Northrup and there are 2 side-by-side examples of using the SHA1CryptoServiceProvider to create a hash value from a...
10
by: Charles Russell | last post by:
Why does this work from the python prompt, but fail from a script? How does one make it work from a script? #! /usr/bin/python import glob # following line works from python prompt; why not in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.