Possibility of returning derived type from inherited operator

Hello forum,
I am wondering the possibility of returning a derived type from an inherited operator. A test case would include two class definitions such as:
Public Class Base
Dim _a
Public ReadOnly Property A As Integer
Get
Return _a
End Get
End Property
Public Shared Operator +(ByVal base1 As Base, _
ByVal base2 As Base) As Base
Return New Base(base1.A + base2.A)
End Operator
Public Sub New(ByVal a As Integer)
_a = a
End Sub
End Class
Public Class Derived
Inherits Base
Public Sub New(ByVal a As Integer)
MyBase.New(a)
End Sub
End Class
and implementation such as:
Dim derivedA As Derived = New Derived(1)
Dim derivedB As Derived = New Derived(1)
Dim derivedResult As Derived = derivedA + derivedB
'would like result as derived, not base!
This test case returns the error that the derivedResult cannot by cast to the base type (as this is how the operator is defined). I am wondering: is there a way to change the operator definition so that the derived type is returned from the operation? There
could be many derived types and avoiding overloading the method in each type definition could reduce the amount of code.
Many thanks.

I'm just Frank, not "Mr. Smith". ;-)
I suspect that Reed is onto it with generics. He's quite familiar with it and in a question I posted a few years ago - about derived classes - he answered it using generics.
Let's see what you or he comes up with. :)
Still lost in code, just at a little higher level.
Haha, I was like "who is Mr. Smith.... oh yeah, that's Frank's last name".  ;)
@James3210:  same here... it's just Reed.  Wearing a shirt with a collar is about as formal as I get.  :)
Anyway, back to the subject at hand, I think the generic method in conjunction with the operator overload is the way to go.  Since you'll have to write the operator on each class anyway, at least you can copy-paste the operator code and then just change
the type used for "T":
Public Class FooBase
Private anInteger As Integer
Protected Shared Function Add(Of T As {FooBase, New})(source As T, target As T) As FooBase
Return New T() With {.anInteger = source.anInteger + target.anInteger}
End Function
Public Shared Operator +(source As FooBase, target As FooBase) As FooBase
Return FooBase.Add(Of FooBase)(source, target)
End Operator
End Class
Public Class DerivedFoo
Inherits FooBase
Public Overloads Shared Operator +(source As DerivedFoo, target As DerivedFoo) As DerivedFoo
Return FooBase.Add(Of DerivedFoo)(source, target)
End Operator
End Class
Public Class DerivedFooTwo
Inherits FooBase
Public Overloads Shared Operator +(source As DerivedFooTwo, target As DerivedFooTwo) As DerivedFooTwo
Return FooBase.Add(Of DerivedFooTwo)(source, target)
End Operator
End Class
In this example the shared Add() method is internal to the FooBase objects, but you could make the access modifier public if you wanted to expose the method for direct use.
-EDIT-
Sorry, I forgot you wanted to return the object type from the operator, not the internal result.  I've updated the code to return a new object instance.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

Similar Messages

  • How to return oracle type from select statment

    I have the following oracle[b] types
    create or replace type course_obj as object
         name varchar2(20),
         start_date date,
         instructor varchar2(20)
    create or replace type course_arr
    is varray(10000) of course_obj;
    create or replace type student_obj as object
    first_name varchar2(50),
    last_name varchar2(50),
    student_number number,
    courses course_arr
    create or replace type student_arr
    is varray(10000) of student_obj;
    package
    CREATE OR REPLACE PACKAGE testing
    AS
    FUNCTION get_student_info ( student_number in number ) RETURN student_arr PIPELINED;
    END;
    CREATE OR REPLACE PACKAGE BODY testing
    AS
    FUNCTION get_student_info ( student_number in number ) RETURN student_arr PIPELINED IS
    course1 course_obj;
    course2 course_obj;
    courses course_arr := course_arr();
    student student_obj;
    BEGIN
    course1 := course_obj('Computer 101', sysdate, 'John Anderson');
    course2 := course_obj('Computer 102', sysdate, 'Jo Doe');
    courses.extend;
    courses(1) := course1;
    courses.extend;
    courses(2) := course2;
    student := student_obj('Jack', 'Smith', 781509790, courses);
    PIPE ROW(student);
    END;
    END;
    When I issue the following select SQL
    select student_number, courses from table(testing.get_student_info(1))
    The result I get is
    student
    number courses
    781509790 ((Computer 101, 9/7/2006 5:13:11 PM, John Anderson), (Computer 102, 9/7/2006 5:13:11 PM, Jo Doe), , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , )
    How can I return the result as
    student
    number name start date instructor
    781509790 Computer 101 9/7/2006 5:13:11 PM John Anderson
    781509790 Computer 102 9/7/2006 5:13:11 PM Joe Doe

    Hi,
    Instead of creating varray, create a table type of objects for both courses as well as students like below.
    create or replace type course_obj as object
    name varchar2(20),
    start_date date,
    instructor varchar2(20)
    create or replace type course_arr
    as TABLE OF course_obj;
    create or replace type student_obj as object
    first_name varchar2(50),
    last_name varchar2(50),
    student_number number,
    courses course_arr
    create or replace type student_arr as TABLE OF student_obj
    Then create a pipelined function like below,
    CREATE OR REPLACE FUNCTION get_student_info ( student_number in number )
    RETURN student_arr PIPELINED IS
    student_arr1 student_arr:=student_arr();
    course_arr1 course_arr:=course_arr();
    i number:=0;
    BEGIN
    i:=i+1;
    course_arr1.extend;
    student_arr1.extend;
    course_arr1(course_arr1.count) := course_obj('maths',sysdate,'vv2');
    student_arr1(student_arr1.count) := student_obj('raja','ram',23232,course_arr1);
    PIPE ROW(student_arr1(i));
    course_arr1(course_arr1.count) := course_obj('computer',sysdate,'vv');
    student_arr1(student_arr1.count) := student_obj('raj','raam',223232,course_arr1);
    PIPE ROW(student_arr1(i));
    END;
    You'll get the result like below,
    FIRST_NAME LAST_NAME STUDENT_NUMBER
    COURSES(NAME, START_DATE, INSTRUCTOR)
    raja ram 23232
    COURSE_ARR(COURSE_OBJ('maths', '08-SEP-06', 'vv2'))
    raj raam 223232
    COURSE_ARR(COURSE_OBJ('computer', '08-SEP-06', 'vv'))
    With regards
    Ram

  • Possible to return List T from a method based on Class T parameter?

    Hi all,
    I would like to implement a method as below:
    private List<T> createList(Class<T> cls) {
    }This method would then be possible to call this way:
    List<?> myDynamicGenericList = createList(someClass);Where someClass could be BigDecimal.class (should be known at compile time I suppose, cannot use reference to just "Class")
    I would then like to be able to get type check when adding elements in myDynamicGenericList:
    myDynamicGenericList .add(someBigDecimal); // Ok!
    myDynamicGenericList .add(someString); // Compilation error!Is this possible to achieve?
    With best regards/
    AC
    Message was edited by:
    Alecor9

      public static void main(String args[])
        List<BigDecimal> myList = createList(BigDecimal.class);
        myList.add(new BigDecimal("45.00"));
        myList.add(new BigDecimal("82.95"));
        for (BigDecimal bd : myList)
          System.out.println("decimal["+bd.toString()+"]");
      static <T> List<T>  createList(Class<T> someClass)
        List<T> someList = new ArrayList<T>();
        return someList;
      }Or you could do this:
      static <T extends Number> List<T>  createList(Class<T> someClass)
        List<T> someList = new ArrayList<T>();
        return someList;
      }

  • How to return object  type from external c procedure ?

    Hello all,
    I'm trying for the first time to return an object type as the return value of an external stored procedure (in C ).
    I don't have any issue to use an object type parameter for the function but not to return one to the caller.
    each time I try I get "ORA-03113: end-of-file on communication channel" and the connection is dropped.
    Thanks for any help,
    Roye Avidor
    here is the code :
    => object type
    SQL>create or replace type address as object ( age number, salary float );
    => address typ file
    CASE=SAME
    TYPE address as address
    => building the dependences structures.
    $>ott userid=scott/tiger intype=address.type code=c hfile=address.h
    => the package description
    create or replace package userTypeDefined_PKG
    is
    function getAddr( addr address ) return address;
    end;
    create or replace package body userTypeDefined_PKG
    is
    function getAddr( addr address ) return address
    AS LANGUAGE C
    NAME "addr"
    LIBRARY userTypeDefinedLib
    WITH CONTEXT
    PARAMETERS
    ( CONTEXT,
    addr,
    addr INDICATOR STRUCT ,
    return address,
    return INDICATOR STRUCT
    end;
    => The C code for the library in
    address* addr(OCIExtProcContext ctx, address address_obj, address_ind address_obj_ind, address_ind ret_ind){
    unsigned int tt;
    OCIEnv *envh;
    OCIError *errh;
    OCISvcCtx *svch;
    sword err;
    address* ret = NULL;
    int inum = 69;
    float fnum = 12.34;
    /* get OCI Environment */
    err = OCIExtProcGetEnv(ctx, &envh, &svch, &errh) ;
    /* allocate space for return sturcture*/
    ret = (address *)OCIExtProcAllocCallMemory(ctx, sizeof(address));
    /* set the AGE value */
    if ( OCINumberFromInt ( errh, &inum, sizeof(inum), OCI_NUMBER_SIGNED, &(ret->AGE) ) == OCI_ERROR )
    OCIExtProcRaiseExcp(ctx,(int)1476); // raise 1476 is fail
    /* set the SALARY value */
    if ( OCINumberFromReal ( errh, &fnum, sizeof(fnum), &(ret->SALARY) ) == OCI_ERROR )
    OCIExtProcRaiseExcp(ctx,(int)1477);// raise 1477 is fail
    // set the indicators for the structure as not null
    ret_ind->atomic = OCIIND_NOTNULL;
    ret_ind->AGE = OCI_IND_NOTNULL;
    ret_ind->SALARY = OCI_IND_NOTNULL;
    return (ret);
    }

    The return indicator should be declared as double pointer, and need to be allocated in the function body.

  • Is it possible to return to CR from PS without converting to a smart object?

    I saw somewhere that the was a menu in Filters to return to CR, but I don't see it in CS6.

    In CS6 and earlier you need to make the image a smart object in Camera Raw.
    Holding the Shift key changes the "Open Image" to "Open Smart Object.
    Once you are in PS you can double click the SO and return to Camera Raw.
    But the smart object needed to be created by Camera Raw for this to work.
    Converting a regular layer to a SO in Photoshop won't work. It will just open the image again in another PS window.

  • Radio Buttons - returning individual values from an exclusion group possible?

    Using LC Designer 7.1
    Does anyone know if it is possible to return individual values from an exclusion group of radio buttons?  My xml data file gives one value for the entire group, e.g.,
    2
    ...where the second radio button was selected.  But I'd prefer an output something like this...
      0
      1
      0
    etc...
    Is something in this format possible?

    You might be better off to use checkboxes and script them to act like radio buttons (as an exclusion group). That way they'd each have an on/off value.
    Regards,
    Dave

  • Returning two types

    given the fact that you can only return one type from each method, how is it possible to do this
         static double getValidRadius (String errorMessage)
              double radius, area;
              boolean radiusOk;
              String errorMessage;
              radius = getRadius();
              validateRadius (radiusOK);
              while radiusOk = false
                   errorMessage = ("error!, the radius must be 5-50 inclusive");
                   System.out.println (errorMessage);
                   radius = getRadius();
                   radiusOk = validateRadius(radius);
         }

    i am confused...
    What exactely do you want do to with your method?
    you pass an String do your method, ok, but you never use it, you deklare the same variable inside the method, which is not possible (you have two variables with the same name?).
    you dont return anything, but the methods name is getValidRadius with the return type double.
    What do you want to do with the line validateRadius( radiusOK ); is this a function call?
    At least it should stand while (radiusOK == false) { and not while radiusOK = false which looks a little bit like VBscribt or something like that. Did you programm in VB before Java?

  • Returning complex attributes from active sync resouce adapter

    In the resource adapter schema mapping, the available attribute types are string, integer, boolean, and encrypted. It seems possible to return other types, such as a map or list of maps. Is this reasonable?
    The use case is a database table containing "broken down" phone numbers. That is, separate columns for area code, phone number, extension, number type, etc.
    One alternative is for the resource adapter to return each component as an attribute, but that is awkward if phone numbers are multivalued.
    Another alternative is for the resource adapter to concatenate the components into a string.
    Or what I am considering is returning the phone numbers in the form of a map or list of maps. The forms or workflows can then do what they want with it.
    Is this a bad idea?

    I've given this issue some more thought and concluded that returning each column as an attribute, possibly multivalued, is the easiest solution. I'll define rules that forms and workflows can use to conveniently get the data they want.
    If I returned a structure or list of structures, I would want to define similar rules for forms and workflows to use. There would be more work developing the adapter, though, making this a less desirable approach.

  • Extract derived data from HFM

    Hi All!
    Is it possible to extract derived values from HFM using LKM HFM Data to SQL?
    By 'derived' I mean gray colored values in HFM data forms. For example, data from currency exchange rate accounts.
    Thanks a lot in advance!
    Edited by: Alex Lasker on 05.04.2010 7:13

    Did you ever get an answer to your question about extracting (not copying) derived values? We are running into the same issue when trying to load HFM data into Planning/Essbase. Since none of those derived values exist in the data extract our Planning totals don't tie-out between it and HFM.

  • How do one can upload a file (a PDF, doc etc) while filling a web form through chrome or safari? It is possible to upload a photo from the camera role, but other file types can not be uploaded.

    How do one can upload a file (a PDF, doc etc) while filling a web form through chrome or safari? It is possible to upload a photo from the camera role, but other file types can not be uploaded.

    For a variety of reasons, mostly related to security, the iOS operating system limits what can be done with respect to file uploading and downloading. But whenever you encounter a limitation like this always think, "There must be an app for this."
    Check the apps James Ward suggests.

  • Returning an array type from a local method in Web Dynpro Java application

    Hi,
    In my project, we have a requirement to display 18 rolling months along with the year, starting from current month.
    How I am going to approach is that I will get the system date and get the current month and send the month and year value to a local method which will return 18 rolling months along with the year.
    But, when I tried to create a new method there is no option to return an array type. It was greyed out.
    So, we can not return an array type from a method from Web Dynpro for Java application?
    If so, what is the alternative and how am I going to achieve it?
    I will appreciate your help!
    Regards
    Ram

    HI
    You can create new methods in
      //@@begin others
      private ArrayList MyMethod(){
           // ** Put your code here
           return new ArrayList();
      //@@end
    Other option are create a context node with cardinality 0...n with one or more attributes, and in your method create the needed registers into this node. To read this values, you only need to read your context node.
    Best regards
    Edited by: Xavier Aranda on Dec 2, 2010 9:41 AM

  • Is it possible to return an array of objects from a web service?

    I have been trying to do this for a while now, and I have come to the conclusion that it may be impossible. To demonstate what I want to do I enclose a simple java file [1]. I have deployed this with Axis 2 and I enclose the responce [2], it is obciously not wat I want.
    Is it porrible to do this? If so, how?
    Thanks for any help,
    [1]
    package org.impress;
    public class SampleObject {
         public SampleElement[] noParameters(){
              SampleElement[] retArray = new SampleElement[2];
              retArray[0] = new SampleElement();
              retArray[0].name = "one";
              retArray[0].value = "alpha";
              retArray[1] = new SampleElement();
              retArray[1].name = "two";
              retArray[1].value = "beta";
              return retArray;
         public class SampleElement {
              public String name;
              public String value;
    }[2]
    <ns:noParametersResponse>
         <ns:return type="org.impress.SampleObject$SampleElement"/>
         <ns:return type="org.impress.SampleObject$SampleElement"/>
    </ns:noParametersResponse>

    Hi
    Can anybody help me with the code of how to return a resultset from a web service. i have put the resultset data's in an object array and tried to return it, but in the client side no data comes ,,, i mean it is printed as null.... and plz tell me where to specify the return type of a object in the wsdl file....
    thanks..

  • Condition Type to be excluded whil creating Return sales Order from invoice

    Hi Frnds,
    I have the following requirement.
    Sales order order is created and its delivered and invoiced too. This sales order has some freight pricing condition types (RCD1, RCD2...)included since its shipped to customer.
    When the customer wants to return the goods, there is a return sales order is created with reference to the invoice created above. Now, the RCD1 and RCD2... should not get copied to return sales order.
    How do i achieve this? Please let me know any user exit, BAdi or Enhancement spot where i can add code to avoide these condition types from being picked up.
    SAP system is ECC 6.0
    Regards.,
    Karthick C

    It can be achieved by implementing a custom requirement routine. If you go to the the transaction VOFM, you will find "requirement" in MENU. You select 'Pricing' on drilling-down it. Following document may help you in creating requirement routine..
    http://saptechsolutions.com/pdf/VOFMCopyRequirementRoutines.pdf
    All requirement routines has similar pattern of writing... Your code may look like as follows:
    FORM KOBED_901.
    sy-subrc = 4.            "By deault condition is false.
    If NOT XKOMK-AUART = 'RE'.                   "RE is returing order type.
       sy-subrc = 0.        "This means For other than returning orders all others condition is true.
    endif.
    ENDFORM
    Once you activate routine, ask functional consultant to attach this to the Condition RCD1 and RCD2. Then, You'll not get RCD1 and RCD2 for Return sales Order.
    Please check and confirm and revert back if you want further details..
    Edited by: Pikle Audumbar on Oct 17, 2009 1:09 AM

  • Is it possible to pass some type of parameter/unique id FROM PDF?

    hi there,
    I will try to explain this as best as I can but please bear with me.
    I have Adobe Acrobat X Pro.
    We have drawings linked to each other in pdf.
    When you open a drawing (say, a layout of a house), my boss wants to be able to click on say, a door, and have all the information on that door pop up (size, manufacturer, when it was shipped, etc). The information log is stored in Excel. I know how to hyperlink to open an excel file FROM pdf, but cannot figure out how to open a specific sheet in Excel. So here is my question:
    1. How do I link to a specific sheet in Excel so it opens when I click on a link in the pdf file?
    Having said that, we are going to have around 1500 items and I don't want to have to create 1500 sheets (if that's even possible) to open the details for each one. So here is question #2:
    2.  Is it possible to pass some type of parameter to excel (or even Access) to know what item was clicked on the pdf file so I can write a macro/code in Excel to just fill in the details for that item? (Hence just needing one sheet instead of 1500?).
    Suggestions/path forwards are welcome.
    I hope this was clear and I thank you in advance.
    Thanks,
    Jessica

    There really isn't a way to do that. It would be possible to export an Excel sheet to a tab-delimited (or CSV, XML) file which could optionally be attached to the PDF. JavaScript inside the PDF could read the data file and extract the information for an item so it could be displayed somehow.

  • Transfer of stock from Returns stock type to Unrestricted/Blocked

    Hi,Friends
    How to transfer of stock from Returns stock type to Unrestricted/Blocked
    what movement type will trigger by transfering this stock,
    Thanks & Regards
    Krishna

    Hi,
    use MB1B Mvt Type 453 returns to unrestricted
    Unrestriced to Blocked stock use - 344
    Thanks & Regards,
    kiran

Maybe you are looking for

  • Case Statement in Answers Filter

    Hi, I'm trying to build a query in Answers that filters the data returned based on the current month number. If the current month is 1 then we want to show all 12 months, otherwise we want to show months less than the current month. If I use the foll

  • Cell phone charging problem

    Hi, My Nokia 6234 is about 2-3 years old.  And in the last few months I've started noticing a problem.  When I fully charge my phone and it's at work, it loses charge in about a day... even when I don't use it much at all, and I don't use bluetooth,

  • Cssd does not start in non-RAC environment Thus we can not bring up ASM

    Non-RAC environment ASM version = 11.1.0.7 HP-UX Itanium 11.23 After power outage, CSSD does not start on non-RAC environment Running as root "/sbin/init.d/init.cssd start" does not start cssd Oracle support tried "$ASM_HOME/bin/localconfig delete" a

  • Procedure call from SQL*Plus in Korn Shell Script

    I am trying to excute a procedure from sqlplus, truncate_audit_table. I doesn't execute. If I do it manually it executes. I have tried multiple ways, as below. This is inside a Korn Shell scripts that creates the procedure and counts the aud$ table.

  • Removing messeges in RW

    Hi Experts,          some unwanted scenerios in my dev system has filled the RW-> Messege Monitering -> Adapter Engine-> with thousands of system errors  / waiting and holding messeges! I think I need to clean them up. I hav already STOPPED the commu