C# newby

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • maria

    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.G etAllItems(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.


  • John Murray

    #2
    Re: C# newby

    If I understand your question correctly, try:

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






    maria wrote:[color=blue]
    > 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.G etAllItems(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.
    >
    >[/color]

    Comment

    • maria

      #3
      Re: C# newby

      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.tIt ems" is a type ,which is not valid in the given context.


      "John Murray" <jmurray@pluck. com> wrote in message
      news:eVL1SleSGH A.336@TK2MSFTNG P12.phx.gbl...[color=blue]
      > If I understand your question correctly, try:
      >
      > obj.GetAllItems (ref NumberOfitems, ref ComObj.tItems)
      >
      >
      >
      >
      >
      >
      > maria wrote:[color=green]
      >> 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.G etAllItems(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.[/color][/color]


      Comment

      • Bruce Wood

        #4
        Re: C# newby

        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(anIt em);
        ....
        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.

        Comment

        • maria

          #5
          Re: C# newby

          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;tIte ms[] Items = obj.GetAllItems ();
          The errror is :

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



          "Bruce Wood" <brucewood@cana da.com> wrote in message
          news:1142632045 .171802.121580@ u72g2000cwu.goo glegroups.com.. .[color=blue]
          >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(anIt em);
          > ...
          > 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.
          >[/color]


          Comment

          • Bruce Wood

            #6
            Re: C# newby

            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?

            Comment

            • maria

              #7
              Re: C# newby

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

              "Bruce Wood" <brucewood@cana da.com> wrote in message
              news:1142878564 .163682.24460@v 46g2000cwv.goog legroups.com...[color=blue]
              > 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?
              >[/color]


              Comment

              • Bruce Wood

                #8
                Re: C# newby

                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.MessageBo x.Show(itemArra y[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.

                Comment

                Working...