Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 Automatic "()" insertion for C# generic methods
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Nels_P_Olsen
Senior Member

USA
47 Posts

Posted - Oct 08 2009 :  12:41:03 PM  Show Profile  Reply with Quote
When selecting a generic method from the suggestion list, VAX still appends "()" to the selected method name and positions the caret between the parenthesis. It needs to insert "<>" and position the caret after the opening angle bracket.

I'm not sure if it should also append "()" after "<>". It could if VAX can be smart enough to pop up the same parameters tooltip you get when you type an opening parenthesis for a method invocation, whenever the caret/cursor is moved to after the opening parenthesis.

- Nels

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Oct 09 2009 :  5:01:30 PM  Show Profile  Reply with Quote
My C# is quite weak, so I did a quick web search for C# generic methods, and have ended up with this code, that compiles perfectly happily in VS2005:

class test_general
{
    void callingTemplateMethod()
    {
        int[] intArray = { 1, 2, 3, 4, 5, 6 };
        double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };

        PrintArray(intArray); // pass an int array argument
        PrintArray(doubleArray); // pass a double array argument
    }

    void PrintArray<E>(E[] inputArray)
    {
        foreach (E element in inputArray)
            Console.Write(element + " ");

        Console.WriteLine("\\n");
    }
}

there are no angle brackets on the call to the generic method "PrintArray", and it does not seem to matter. The code sample is from here:

http://www.deitel.com/articles/csharp_tutorials/20051111/GenericMethodImplementation.html

no idea if it is a good site or not though.

zen is the art of being at one with the two'ness
Go to Top of Page

Nobodo
Tomato Guru

126 Posts

Posted - Oct 13 2009 :  2:18:02 PM  Show Profile  Reply with Quote
Here is an example:

using System;
using System.Collections.Generic;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> myList = new List<int>();
            myList.Add(15);
            myList.Add(22);
            myList.Add(23);
            
            foreach (int myInt in myList)
            {
                Console.WriteLine(myInt * 2);
            }
            Console.Read();
        }
    }
}


I don't see what Nels describes, however. In fact (in VS2005), VA does automatically insert <int> at the end of
List<int> myList = new List

but doesn't put the () automatically at the end of
List<int> myList = new List<int>

Edited by - Nobodo on Oct 13 2009 2:23:04 PM
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Oct 13 2009 :  2:28:19 PM  Show Profile  Reply with Quote
You do not have any angle brackets on any function calls in your example either. I am a bit confused. Why should VA add angle brackets to a function call if they are not required?

zen is the art of being at one with the two'ness
Go to Top of Page

Nobodo
Tomato Guru

126 Posts

Posted - Oct 13 2009 :  2:42:19 PM  Show Profile  Reply with Quote
I am not seeing anything wrong, just was giving an example that included the angle brackets in a call. The instantiation has them:
List<int> myList = new List<int>();


Once you type the
List<int> myList = new

part, VA has what is needed to figure out how to suggest the rest. Your example didn't have anything generic related for VA to auto-suggest, so that's why I gave an example.
Go to Top of Page

Nels_P_Olsen
Senior Member

USA
47 Posts

Posted - Oct 15 2009 :  09:29:17 AM  Show Profile  Reply with Quote
Feline, your example is able to infer the generic type argument from the type of the method parameter. This is not always possible or even desirable. Try using a method that takes a generic type argument that is not used as the type of one of the method parameters, e.g. Convert.ChangeType<T>(object) which returns an instance of type T.

Also, try this where there are both generic and non-generic overloads for a method name, and you choose the generic method from Intellisense.

- Nels
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Oct 22 2009 :  5:30:14 PM  Show Profile  Reply with Quote
This makes a bit more sense. The question now becomes, when should VA insert the angle brackets? You probably don't want this to happen for all generic functions, as the code samples here show.

Would it be sensible to only insert the angle brackets when the template type is not used as a parameter type? Or is this to simplistic?

zen is the art of being at one with the two'ness
Go to Top of Page

Nels_P_Olsen
Senior Member

USA
47 Posts

Posted - Oct 23 2009 :  10:05:24 AM  Show Profile  Reply with Quote
You could have an checkbox option in VA setup, something along the lines of "Insert <> before () for explicit generic type arguments". Our shop at least never writes code that uses implicit generic type argument resolution.

- Nels
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19022 Posts

Posted - Oct 26 2009 :  10:26:12 AM  Show Profile  Reply with Quote
This makes sense. We are considering adding the angle brackets for C++ generic items, so I have added a note about all of this to the case, for C# code:

case=460

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000