Overload Methods

Overload Methods

I have decided to post issues that solicit feedback from you and in the process touch upon the cool aspects of Class Designer. I hope you find it engaging.

The Class Designer sits on top of the VC Code Model. The VC Code Model poses certain restrictions while creating overloaded functions. VC Code Model would not allow a function to be added to a type, if it already contains a function with the same signature. Say we want to add an overload that differs in the last parameter type. We start with the first parameter and keep adding. When you add the penultimate one VC Code Model wouldn’t let you continue. Because adding the last but one parameter causes a conflict with a function that already exists.

Following are some problems we face and solutions we have. Your opinions are invaluable.

Adding a parameter to a method needs a default type

class Bar

{

public:

// Rest of the class!

};

Cool!

The user has opened the VC project and wants to know about the class Bar. The Bar class is huge and has tons of methods. The user is interested only in browsing the “public” methods of Bar.

The user right clicks on the header file “Bar.h” in the Solution Explorer and chooses “View in Diagram”. The Class Designer will be launched and it will display all types declared in the file “Bar.h”. The user selects the Bar class on the diagram and expands it so that it displays all members. To see members grouped by access, the user would right click on the designer and chooses “Group by Access”. All the members in the Bar class will be grouped as public, private, protected etc. The user could collapse the compartments (private and protected) that are not interesting. The user also notices that when a type is selected there is a Class Details Window (a grid control) showing more details about members in the type.

Issue?

Through the Class Details Window the user wants to add a method Foo that takes a parameter. First the user adds a method named Foo. Then the user adds a parameter to the method and names it “myParam”. You have to realize that we are trying to synchronize the designer to the code editor. Every action the user doeswill, if applicable, translates to code being generated. In the above case a method Foo got added in code and now we have to add a parameter in code. To add a parameter we need the type. Since the user hasn’t entered a type we have to generate a default type.

What should be the default type?

Our Solution –

The default type will be “__ProvideType_N”. Where “N” will replaced by an integer value that will be incremented for every new parameter. Note that the default type will be generated to code.

Resulting Code -

class Bar

{

public:

void Foo(__ProvideType_1 myParam)

{

}

// Rest of the class!

};

Adding a method causes overload conflict

class Bar

{

public:

void Foo()

{

}

// Rest of the class!

};

class MyBar : Bar

{

};

Cool!

Assume that in the Class Designer the user has the MyBar class displaed. The user wants to view the Bar class in the designer. The user can right click on the MyBar class and select “Show Base Classes”. The Bar class will be displayed on the designer and an inheritance relationship will be displayed between MyBar and Bar.

Issue?

Say the user wants to add an overload for Foo that takes a parameter in the Bar class. First the user adds a method named Foo through the Class Details Window. As soon as the method is added we try to generate code for it through the VC Code Model. This conflicts with the existing Foo method.

Our Solution –

Before adding the new method we will search in the current class to see if there would be a conflict. If there will be a conflict then we will do the following on the VC Code Model…

1.Create a temp method with the name __Foo

2.Add a parameter of type “__ProvideTypeToResolveConflict_N” and name of the parameter will be “Param_N”, where “N” is an incrementing integer value.

3.Rename the __Foo method to Foo

4.Select the parameter we added

The user will only notice that a method “Foo” was added as requested but in addition a parameter has been added to resolve overload conflict with an existing method in the class.

Resulting code –

class Bar

{

public:

void Foo()

{

}

void Foo(__ProvideTypeToResolveConflict_1 Param_1)

{

}

// Rest of the class!

};

class MyBar : Bar

{

};

Modifying a method causes overload conflict

class Bar

{

public:

void Foo(int i)

{

}

void Foo(char c)

{

}

// Rest of the class!

};

class MyBarContainer

{

Bar* myBar;

}

Cool!

The user has the type MyBarContainer on the Class Designer. To see the contents of the Bar class, the user will right click on the member myBar and chooses “Show As Association”. The Class Designer will display the Bar class on the diagram and will establish an association relationship between MyBarContainer and Bar.

Issue?

The user wants to change the signature of “void Foo(char c)” to “void Foo(int c, double d)”. To achieve that, the user decides to first change the type of the first parameter “c” to “int”. If we translate this action to VC Code Model it will throw an exception since modifying the type on “c” to “int” conflicts with an already existing method.

Our Solution –

Before deleting/modifying the parameters on the VC Code Model, we figure out the resulting signature

1.Search the current class and check if there would be any conflicts with the calculated signature

2.If the user intended change would create a conflict then we will insert a parameter “Param_N” of type “__ProvideTypeToResolveConflict_N” as the first parameter to the method that is being modified.

3.The parameter we added will be selected

Resulting Code –

class Bar

{

public:

void Foo(int i)

{

}

void Foo(__ProvideTypeToResolveConflict _1 Param_1, int c)

{

}

// Rest of the class!

};

class MyBarContainer

{

Bar* myBar;

}

It would be great if you could let me know if the user experience for creating overloaded functions is good enough or if you would prefer some modifications or prefer some other way of doing this.