M.Ali.Raza

Hi folks, I want to ask that what may be best practice to overcome following situation

I have a class named BaseException with three following constructors

public MyException(Exception p_exException, params string [] SzMsgs ) : this ( p_exException,null, SzMsgs)

{

}

public MyException(Exception p_exException, string p_szUserName):

this ( p_exException, p_szUserName, null)

{

}

public MyException(Exception p_exException, string p_szUserName, params string [] p_szMsg)

{

//some implementation

}

Upon compiling the application it gives the following erro.

The call is ambiguous between the following methods or properties: 'MyTestApp.MyException.MyException(System.Exception, params string[])' and 'MyTestApp.MyException.MyException(System.Exception, string, params string[])'

I have tried introducing an extra variable bool nill in first constructor

public MyException(Exception p_exException, bool nill, params string[] SzMsgs): this(p_exException, null, SzMsgs)

{

}

that may be of (any) type to overcome the ambiguity but it seems the worst solution of this so, what may be the possible solution of this in anyone mind.

P.S I can not change the order of the 2nd Constructor's parameters as some libraries which have been in use are deployed and may use this signature.

Thanx in advance on reading all this.. Smile




Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

Karioth

Indeed, the first constructor is suitable for every of situations. It can accept no strings, one string or more. Just check SzMsgs.Length to divide it.




Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

M.Ali.Raza

thanx for reading and the post.
In First constructor it uses the User Name and second one is to utilize the Message Strings, both have different meaning which differ from scenario to scenario. some time i want to send one Message String and want to call function which accepts Params array then what will be the situation, and how it can be achieved..

Any ways thanx 4 ur time..!





Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

Sean Fowler

Yes it's one of those annoying limitations of the language.

You say you can't change the signature of the second constructor. Well you could reverse the order of the parameters in the first constructor. It's not nice but it's a little nicer than adding a dummy parameter.

Sean






Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

boban.s

Swithing the place of Exception and UserName argument can solve the problem:

public MyException(Exception p_exException, params string[] SzMsgs) :

this(null, p_exException, SzMsgs)

{

}

public MyException(string p_szUserName, Exception p_exException):

this(p_szUserName, p_exException, null)

{

}

public MyException(string p_szUserName, Exception p_exException, params string[] p_szMsg)

{

//some implementation

}






Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

M.Ali.Raza

as i explained in P.S that i can not do this as second overload is been used by my application almost thousand time which is already deployed. the first one is new which i want to include in the class.

Thanx for ur time.





Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

boban.s

The problem is in 'params' parameter. Maybe is to late to change it, but why to use 'params string[]' and not List<string>. Then you will not have problems.






Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

Mamush

the problem is Clear so you have to work around it.

here us what i did

I defined a small class for UserName with tha abilty of implict casting to string

Code Snippet

using System;

using System.Collections.Generic;

using System.Text;

namespace Test

{

class MyException

{

public MyException(Exception p_exException, params string[] SzMsgs)

: this(p_exException, null, SzMsgs)

{

}

public MyException(Exception p_exException, string p_szUserName)

:

this(p_exException, (UserName)p_szUserName, null)

{

}

public MyException(Exception p_exException, UserName p_szUserName, params string[] p_szMsg)

{

//some implementation

}

}

class UserName

{

UserName(String user)

{

name = user;

}

private String name;

public String Name

{

get { return name; }

set { name = value; }

}

static public implicit operator UserName(string value)

{

return new UserName(value);

}

}

}

This will work and will not affect the deployed version

Hope it helps and let me know what you think

Cheers






Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

Sean Fowler

How about this. It only changes the paremeter order for the third constructor.

Code Snippet

public class MyException

{

public MyException(Exception p_exException, params string[] SzMsgs)

: this(null, p_exException, SzMsgs)

{

}

public MyException(Exception p_exException, string p_szUserName)

: this(p_szUserName, p_exException, null)

{

}

public MyException(string p_szUserName, Exception p_exException, params string[] p_szMsg)

{

//some implementation

}

}

Sean






Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

M.Ali.Raza

Sean as i said i cant change 2nd and 3rd constructors as lots of code has been written against their signature. I think in my situation Boban.s 2nd answer seem pretty much suitable.
But thanx a lot once again 4 ur time n efforts.





Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

M.Ali.Raza

Mamush this might be the best situation if third constructor


public MyException(Exception p_exException, UserName p_szUserName, params string[] p_szMsg)

{

//some implementation

}

was not in use but as this is the one already in use and deployed assemblies already using this in thousand of places so I think i should use Generic list for interducing the new constructor.
Thanx 4 the time n efforts.
- Ali





Re: Visual C# General What is the best practice to overcome ambiguous functions calls?

Sean Fowler

Ahh ok, actually you only said you couldn't change the second constructor, but if the third one's already in use too then fair enough.

To make it a little more flexible for possible future requirements you could change your first constructor to accept an "ExceptionRequest" object. This would be your own object that has properties of an exception, a username and a list of messages.

Of course you can create constructors for this new ExceptionRequest object, but it's also possible to create the object and then set the properties. This frees you from the constraints of constructor signatures.

Sean