I'm trying to change the way a treeview works just a bit, i'm pretty new
to C# and now i'm running into overloading. I tried the following code
and it's yelling at me saying that no overload method takes 0 arguments.
#region tViewNodeCollec tion
public class tViewNodeCollec tion : TreeNodeCollect ion
{
private tView _owner;
private tViewNode _parent;
/// <summary>
/// Create a collection within a tViewItem
/// </summary>
/// <param name="parent"></param>
public tViewNodeCollec tion(tViewNode parent)
{
_parent = parent;
}
/// <summary>
/// Create a new instance of tViewNodeCollec tion
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollec tion(tView owner)
{
_owner = owner;
}
/// <summary>
/// Create a new instance of tViewNodeCollec tion
/// </summary>
/// <param name="owner"></param>
public tViewNodeCollec tion(tView owner, tViewNode parent)
{
_owner = owner;
_parent = parent;
}
}
#endregion
so then I tried a series of different :this(something ) but that started
scaring me since I really don't understand overloading - Could someone
shed some light on this for me? 10 2679
An overloaded method is a method that differs from all methods of the same
name in so far as it has a different number or types of its arguments. A
different return type does not constitute an overload. If you get an error
stating that no overload has 0 arguments then you are calling a method with
no arguments and you have no overloaded method that takes zero arguments.
Either instantiate the class with a constructor call with arguments or
provide an explicit constructor with zero arguments. In C# if you provide an
overloaded constructor you no longer get a default constructor (zero
arguments) and need to provide one. I think this is your problem.
Thomas P. Skinner [MVP]
"Benny Raymond" <be***@pocketro cks.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. .. I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload method takes 0 arguments.
#region tViewNodeCollec tion public class tViewNodeCollec tion : TreeNodeCollect ion { private tView _owner; private tViewNode _parent; /// <summary> /// Create a collection within a tViewItem /// </summary> /// <param name="parent"></param> public tViewNodeCollec tion(tViewNode parent) { _parent = parent; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner) { _owner = owner; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner, tViewNode parent) { _owner = owner; _parent = parent; } } #endregion
so then I tried a series of different :this(something ) but that started scaring me since I really don't understand overloading - Could someone shed some light on this for me?
I believe that the constructor of the TreeViewCollect ion has been set to
private so effectively it can't be inherited.
The error message is a bit misleading though (it suggests that there is a
public constructor that does not have zero args - and hence in that case you
would be able to call it from your derived class) .
see for instance... http://www.groupsrv.com/dotnet/viewtopic.php?t=16350
Hope this helps
Br,
Mark.
P.S.
Benny, although this is really something that is open to your/ your company
preference, when naming types you should (IMHO) not prefix them with
"t" -that is really a Delphi thing (but if it works for you then please
ignore my advice).
P.P.S.
You mention that you do not understand overloading, well in essence creating
an overload means that you are adding an additional function call to a class
which has the same name that is distinguished by it's type signature.
For instance
public void SayHello()
{SayHello();} //just calling overload method to save code in class
public void SayHello(string name)
{ string display = "Hello";
if name.Length > 0
display += name;
Console.WriteLi ne(display);
}
Which will mean that the Class would provide two method calls..
1 that allows you to SayHello without any arguments and the other to allow
you to SayHello with a string argument.
***I think you are also getting a little bit confused with overloading and
overriding - they are two DIFFERENT concepts. You should ideally read up on
both since they are crucial to good OOP***
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message
news:uK******** ******@TK2MSFTN GP15.phx.gbl... An overloaded method is a method that differs from all methods of the same name in so far as it has a different number or types of its arguments. A different return type does not constitute an overload. If you get an error stating that no overload has 0 arguments then you are calling a method with no arguments and you have no overloaded method that takes zero arguments. Either instantiate the class with a constructor call with arguments or provide an explicit constructor with zero arguments. In C# if you provide an overloaded constructor you no longer get a default constructor (zero arguments) and need to provide one. I think this is your problem.
Thomas P. Skinner [MVP]
"Benny Raymond" <be***@pocketro cks.com> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. .. I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload method takes 0 arguments.
#region tViewNodeCollec tion public class tViewNodeCollec tion : TreeNodeCollect ion { private tView _owner; private tViewNode _parent; /// <summary> /// Create a collection within a tViewItem /// </summary> /// <param name="parent"></param> public tViewNodeCollec tion(tViewNode parent) { _parent = parent; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner) { _owner = owner; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner, tViewNode parent) { _owner = owner; _parent = parent; } } #endregion
so then I tried a series of different :this(something ) but that started scaring me since I really don't understand overloading - Could someone shed some light on this for me?
A private constructor with zero args doesn't prevent inheritance from that
class. Basically the private constructor with zero args is normally used to
force the programming to instantiate the class with an overloaded
constructor. The derived class would need to explicitly invoke the
overloaded constructor in the base class.
The TreeNodeCollect ion does seem a bit strange in that no constructors are
documented. That would mean the class must be constructed via a public
static method. However I can't find one documented. It appears that the
TreeNodeCollect ion gets instantiated by the TreeView class by some subtle
method. In this case I agree that one can't inherit from TreeNodeCollect ion.
BTW, TreeNodeCollect ion is NOT documented as sealed. The result is the same
I guess. Interesting.
Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message
news:eK******** ******@TK2MSFTN GP14.phx.gbl... I believe that the constructor of the TreeViewCollect ion has been set to private so effectively it can't be inherited. The error message is a bit misleading though (it suggests that there is a public constructor that does not have zero args - and hence in that case you would be able to call it from your derived class) .
see for instance... http://www.groupsrv.com/dotnet/viewtopic.php?t=16350
Hope this helps
Br,
Mark.
P.S. Benny, although this is really something that is open to your/ your company preference, when naming types you should (IMHO) not prefix them with "t" -that is really a Delphi thing (but if it works for you then please ignore my advice).
P.P.S. You mention that you do not understand overloading, well in essence creating an overload means that you are adding an additional function call to a class which has the same name that is distinguished by it's type signature. For instance
public void SayHello() {SayHello();} //just calling overload method to save code in class
public void SayHello(string name) { string display = "Hello"; if name.Length > 0 display += name; Console.WriteLi ne(display); }
Which will mean that the Class would provide two method calls.. 1 that allows you to SayHello without any arguments and the other to allow you to SayHello with a string argument.
***I think you are also getting a little bit confused with overloading and overriding - they are two DIFFERENT concepts. You should ideally read up on both since they are crucial to good OOP*** "Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:uK******** ******@TK2MSFTN GP15.phx.gbl... An overloaded method is a method that differs from all methods of the same name in so far as it has a different number or types of its arguments. A different return type does not constitute an overload. If you get an error stating that no overload has 0 arguments then you are calling a method with no arguments and you have no overloaded method that takes zero arguments. Either instantiate the class with a constructor call with arguments or provide an explicit constructor with zero arguments. In C# if you provide an overloaded constructor you no longer get a default constructor (zero arguments) and need to provide one. I think this is your problem.
Thomas P. Skinner [MVP]
"Benny Raymond" <be***@pocketro cks.com> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. .. I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload method takes 0 arguments.
#region tViewNodeCollec tion public class tViewNodeCollec tion : TreeNodeCollect ion { private tView _owner; private tViewNode _parent; /// <summary> /// Create a collection within a tViewItem /// </summary> /// <param name="parent"></param> public tViewNodeCollec tion(tViewNode parent) { _parent = parent; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner) { _owner = owner; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner, tViewNode parent) { _owner = owner; _parent = parent; } } #endregion
so then I tried a series of different :this(something ) but that started scaring me since I really don't understand overloading - Could someone shed some light on this for me?
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in
news:uF******** ******@TK2MSFTN GP15.phx.gbl: A private constructor with zero args doesn't prevent inheritance from that class. Basically the private constructor with zero args is normally used to force the programming to instantiate the class with an overloaded constructor. The derived class would need to explicitly invoke the overloaded constructor in the base class.
The TreeNodeCollect ion does seem a bit strange in that no constructors are documented. That would mean the class must be constructed via a public static method. However I can't find one documented. It appears that the TreeNodeCollect ion gets instantiated by the TreeView class by some subtle method. In this case I agree that one can't inherit from TreeNodeCollect ion. BTW, TreeNodeCollect ion is NOT documented as sealed. The result is the same I guess. Interesting.
Thomas,
Using Lutz Roeder's Reflector utility, it appears that
TreeNodeCollect ion's only constructor is scoped as internal. The
types w/i System.Windows. Forms.dll can use it like any other type,
but us poor serfs on the "outside" can't construct our own instances
of TreeNodeCollect ion :-(.
Chris.
-------------
C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
Chris,
Now why didn't I think of that? This makes complete sense. I was thinking
maybe an undocumented static method or something, but internal is so much
simpler. Thanks for this info.
Thomas P. Skinner [MVP]
"Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16... "Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in news:uF******** ******@TK2MSFTN GP15.phx.gbl:
A private constructor with zero args doesn't prevent inheritance from that class. Basically the private constructor with zero args is normally used to force the programming to instantiate the class with an overloaded constructor. The derived class would need to explicitly invoke the overloaded constructor in the base class.
The TreeNodeCollect ion does seem a bit strange in that no constructors are documented. That would mean the class must be constructed via a public static method. However I can't find one documented. It appears that the TreeNodeCollect ion gets instantiated by the TreeView class by some subtle method. In this case I agree that one can't inherit from TreeNodeCollect ion. BTW, TreeNodeCollect ion is NOT documented as sealed. The result is the same I guess. Interesting.
Thomas,
Using Lutz Roeder's Reflector utility, it appears that TreeNodeCollect ion's only constructor is scoped as internal. The types w/i System.Windows. Forms.dll can use it like any other type, but us poor serfs on the "outside" can't construct our own instances of TreeNodeCollect ion :-(.
Chris. ------------- C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
Hi Thomas please see replies....
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message
news:uF******** ******@TK2MSFTN GP15.phx.gbl... A private constructor with zero args doesn't prevent inheritance from that class. Basically the private constructor with zero args is normally used to force the programming to instantiate the class with an overloaded constructor. The derived class would need to explicitly invoke the overloaded constructor in the base class.
Incorrect - A private constructor *will* prevent inheritance *if* it is
the only constructor available in the class (which I suggested in the
original post). Your statement is correct only *if* there is an available
constructor visible to the code outside the class (public) or to the
inheriting class (protected - if both classes in same assembly). One reason
you might want to do this is to enforce strict object creation through an
object factory method. For instance the following class cannot be
inherited...
public class Parent
{
string name;
private Parent(string s) //only constructor: is private and prevents
inheritance since any inheriting classes cannot see the base constructor
{
name = s;
}
public static Parent CreateParent(st ring s)
{
if (s == null || s.Length == 0)
{
throw new Exception("Inva lid string specified when building new
object");
}
return new Parent(s);
}
}
You are obviously correct is stating another reason why a private default
constructor is used -to enforce construction through an overloaded
constructor *although* this is strictly not necessary since a default
constructor is only used if :-
i. No constructor is specified in a class
ii. If the default constructor has been explicitly declared - which is the
default behaviour of Visual Studio code generator.
For instance the following code can only be created using the constructor
with a string params...
public class Class2
{
//the lack of default constructor effectively means that the class can
only
//be instanciated using the constructor with a string parameter.
public Class2(string s)
{
}
}
The TreeNodeCollect ion does seem a bit strange in that no constructors are documented. That would mean the class must be constructed via a public static method. However I can't find one documented. It appears that the TreeNodeCollect ion gets instantiated by the TreeView class by some subtle method. In this case I agree that one can't inherit from TreeNodeCollect ion. BTW, TreeNodeCollect ion is NOT documented as sealed. The result is the same I guess. Interesting.
Thankfully Chris has got to the bottom of this one. Interesting subject
this. Like you I noticed the lack of constructor documentation for this
class. I really wish Microsoft would document exactly the internals of the
class libraries -but I guess you cant have everything.
Best Regards,
Mark Broadbent.
Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message news:eK******** ******@TK2MSFTN GP14.phx.gbl...I believe that the constructor of the TreeViewCollect ion has been set to private so effectively it can't be inherited. The error message is a bit misleading though (it suggests that there is a public constructor that does not have zero args - and hence in that case you would be able to call it from your derived class) .
see for instance... http://www.groupsrv.com/dotnet/viewtopic.php?t=16350
Hope this helps
Br,
Mark.
P.S. Benny, although this is really something that is open to your/ your company preference, when naming types you should (IMHO) not prefix them with "t" -that is really a Delphi thing (but if it works for you then please ignore my advice).
P.P.S. You mention that you do not understand overloading, well in essence creating an overload means that you are adding an additional function call to a class which has the same name that is distinguished by it's type signature. For instance
public void SayHello() {SayHello();} //just calling overload method to save code in class
public void SayHello(string name) { string display = "Hello"; if name.Length > 0 display += name; Console.WriteLi ne(display); }
Which will mean that the Class would provide two method calls.. 1 that allows you to SayHello without any arguments and the other to allow you to SayHello with a string argument.
***I think you are also getting a little bit confused with overloading and overriding - they are two DIFFERENT concepts. You should ideally read up on both since they are crucial to good OOP*** "Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:uK******** ******@TK2MSFTN GP15.phx.gbl... An overloaded method is a method that differs from all methods of the same name in so far as it has a different number or types of its arguments. A different return type does not constitute an overload. If you get an error stating that no overload has 0 arguments then you are calling a method with no arguments and you have no overloaded method that takes zero arguments. Either instantiate the class with a constructor call with arguments or provide an explicit constructor with zero arguments. In C# if you provide an overloaded constructor you no longer get a default constructor (zero arguments) and need to provide one. I think this is your problem.
Thomas P. Skinner [MVP]
"Benny Raymond" <be***@pocketro cks.com> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. .. I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload method takes 0 arguments.
#region tViewNodeCollec tion public class tViewNodeCollec tion : TreeNodeCollect ion { private tView _owner; private tViewNode _parent; /// <summary> /// Create a collection within a tViewItem /// </summary> /// <param name="parent"></param> public tViewNodeCollec tion(tViewNode parent) { _parent = parent; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner) { _owner = owner; } /// <summary> /// Create a new instance of tViewNodeCollec tion /// </summary> /// <param name="owner"></param> public tViewNodeCollec tion(tView owner, tViewNode parent) { _owner = owner; _parent = parent; } } #endregion
so then I tried a series of different :this(something ) but that started scaring me since I really don't understand overloading - Could someone shed some light on this for me?
Yes, but that's not what I said. Read it again at bit more carefully before
criticizing.. I was responding to a conjecture that a private default
constructor always prevents inheritance. It doesn't if there are public
overloaded constructors as you well point out. I never said a class with
ONLY a private default constructor can be inherited.
Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message
news:Or******** *****@TK2MSFTNG P11.phx.gbl... Hi Thomas please see replies....
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:uF******** ******@TK2MSFTN GP15.phx.gbl...A private constructor with zero args doesn't prevent inheritance from that class. Basically the private constructor with zero args is normally used to force the programming to instantiate the class with an overloaded constructor . The derived class would need to explicitly invoke the overloaded constructor in the base class.
Incorrect - A private constructor *will* prevent inheritance *if* it is the only constructor available in the class (which I suggested in the original post). Your statement is correct only *if* there is an available constructor visible to the code outside the class (public) or to the inheriting class (protected - if both classes in same assembly). One reason you might want to do this is to enforce strict object creation through an object factory method. For instance the following class cannot be inherited... public class Parent { string name; private Parent(string s) //only constructor: is private and prevents inheritance since any inheriting classes cannot see the base constructor { name = s; }
public static Parent CreateParent(st ring s) { if (s == null || s.Length == 0) { throw new Exception("Inva lid string specified when building new object"); } return new Parent(s); } }
You are obviously correct is stating another reason why a private default constructor is used -to enforce construction through an overloaded constructor *although* this is strictly not necessary since a default constructor is only used if :- i. No constructor is specified in a class ii. If the default constructor has been explicitly declared - which is the default behaviour of Visual Studio code generator. For instance the following code can only be created using the constructor with a string params...
public class Class2 { //the lack of default constructor effectively means that the class can only //be instanciated using the constructor with a string parameter. public Class2(string s) { } }
The TreeNodeCollect ion does seem a bit strange in that no constructors are documented. That would mean the class must be constructed via a public static method. However I can't find one documented. It appears that the TreeNodeCollect ion gets instantiated by the TreeView class by some subtle method. In this case I agree that one can't inherit from TreeNodeCollect ion. BTW, TreeNodeCollect ion is NOT documented as sealed. The result is the same I guess. Interesting.
Thankfully Chris has got to the bottom of this one. Interesting subject this. Like you I noticed the lack of constructor documentation for this class. I really wish Microsoft would document exactly the internals of the class libraries -but I guess you cant have everything.
Best Regards,
Mark Broadbent. Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message news:eK******** ******@TK2MSFTN GP14.phx.gbl...I believe that the constructor of the TreeViewCollect ion has been set to private so effectively it can't be inherited. The error message is a bit misleading though (it suggests that there is a public constructor that does not have zero args - and hence in that case you would be able to call it from your derived class) .
see for instance... http://www.groupsrv.com/dotnet/viewtopic.php?t=16350
Hope this helps
Br,
Mark.
P.S. Benny, although this is really something that is open to your/ your company preference, when naming types you should (IMHO) not prefix them with "t" -that is really a Delphi thing (but if it works for you then please ignore my advice).
P.P.S. You mention that you do not understand overloading, well in essence creating an overload means that you are adding an additional function call to a class which has the same name that is distinguished by it's type signature. For instance
public void SayHello() {SayHello();} //just calling overload method to save code in class
public void SayHello(string name) { string display = "Hello"; if name.Length > 0 display += name; Console.WriteLi ne(display); }
Which will mean that the Class would provide two method calls.. 1 that allows you to SayHello without any arguments and the other to allow you to SayHello with a string argument.
***I think you are also getting a little bit confused with overloading and overriding - they are two DIFFERENT concepts. You should ideally read up on both since they are crucial to good OOP*** "Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:uK******** ******@TK2MSFTN GP15.phx.gbl... An overloaded method is a method that differs from all methods of the same name in so far as it has a different number or types of its arguments. A different return type does not constitute an overload. If you get an error stating that no overload has 0 arguments then you are calling a method with no arguments and you have no overloaded method that takes zero arguments. Either instantiate the class with a constructor call with arguments or provide an explicit constructor with zero arguments. In C# if you provide an overloaded constructor you no longer get a default constructor (zero arguments) and need to provide one. I think this is your problem.
Thomas P. Skinner [MVP]
"Benny Raymond" <be***@pocketro cks.com> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. .. > I'm trying to change the way a treeview works just a bit, i'm pretty > new to C# and now i'm running into overloading. I tried the > following code and it's yelling at me saying that no overload method > takes 0 arguments. > > #region tViewNodeCollec tion > public class tViewNodeCollec tion : TreeNodeCollect ion > { > private tView _owner; > private tViewNode _parent; > /// <summary> > /// Create a collection within a tViewItem > /// </summary> > /// <param name="parent"></param> > public tViewNodeCollec tion(tViewNode parent) > { > _parent = parent; > } > /// <summary> > /// Create a new instance of tViewNodeCollec tion > /// </summary> > /// <param name="owner"></param> > public tViewNodeCollec tion(tView owner) > { > _owner = owner; > } > /// <summary> > /// Create a new instance of tViewNodeCollec tion > /// </summary> > /// <param name="owner"></param> > public tViewNodeCollec tion(tView owner, tViewNode parent) > { > _owner = owner; > _parent = parent; > } > } > #endregion > > > so then I tried a series of different :this(something ) but that > started scaring me since I really don't understand overloading - Could > someone shed some light on this for me?
1. I think you will find that you did implicitly say exactly that ...."A
private constructor with zero args doesn't prevent inheritance from that
class"
-since you were attempting to correct my original statement -which by the
way mentions NOTHING about overloaded constructor in the base class and in
addition nowhere in my statement do I say ALWAYS prevents -just in that one
instance.
2. I was not critizing, I was correcting (or for the sake of
Benny...clarify ing your misleading statement). So before you start flaming
me please think about what you are saying and take time to read the posts
properly.
3. Your MVP doesnot entitle you to be correct on every occasion so get over
it and move on!
NUFF SAID!
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. Yes, but that's not what I said. Read it again at bit more carefully before criticizing.. I was responding to a conjecture that a private default constructor always prevents inheritance. It doesn't if there are public overloaded constructors as you well point out. I never said a class with ONLY a private default constructor can be inherited.
Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message news:Or******** *****@TK2MSFTNG P11.phx.gbl... Hi Thomas please see replies....
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:uF******** ******@TK2MSFTN GP15.phx.gbl...A private constructor with zero args doesn't prevent inheritance from that class. Basically the private constructor with zero args is normally used to force the programming to instantiate the class with an overloaded constructo r. The derived class would need to explicitly invoke the overloaded constructor in the base class.
Incorrect - A private constructor *will* prevent inheritance *if* it is the only constructor available in the class (which I suggested in the original post). Your statement is correct only *if* there is an available constructor visible to the code outside the class (public) or to the inheriting class (protected - if both classes in same assembly). One reason you might want to do this is to enforce strict object creation through an object factory method. For instance the following class cannot be inherited... public class Parent { string name; private Parent(string s) //only constructor: is private and prevents inheritance since any inheriting classes cannot see the base constructor { name = s; }
public static Parent CreateParent(st ring s) { if (s == null || s.Length == 0) { throw new Exception("Inva lid string specified when building new object"); } return new Parent(s); } }
You are obviously correct is stating another reason why a private default constructor is used -to enforce construction through an overloaded constructor *although* this is strictly not necessary since a default constructor is only used if :- i. No constructor is specified in a class ii. If the default constructor has been explicitly declared - which is the default behaviour of Visual Studio code generator. For instance the following code can only be created using the constructor with a string params...
public class Class2 { //the lack of default constructor effectively means that the class can only //be instanciated using the constructor with a string parameter. public Class2(string s) { } }
The TreeNodeCollect ion does seem a bit strange in that no constructors are documented. That would mean the class must be constructed via a public static method. However I can't find one documented. It appears that the TreeNodeCollect ion gets instantiated by the TreeView class by some subtle method. In this case I agree that one can't inherit from TreeNodeCollect ion. BTW, TreeNodeCollect ion is NOT documented as sealed. The result is the same I guess. Interesting.
Thankfully Chris has got to the bottom of this one. Interesting subject this. Like you I noticed the lack of constructor documentation for this class. I really wish Microsoft would document exactly the internals of the class libraries -but I guess you cant have everything.
Best Regards,
Mark Broadbent. Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message news:eK******** ******@TK2MSFTN GP14.phx.gbl... I believe that the constructor of the TreeViewCollect ion has been set to private so effectively it can't be inherited. The error message is a bit misleading though (it suggests that there is a public constructor that does not have zero args - and hence in that case you would be able to call it from your derived class) .
see for instance... http://www.groupsrv.com/dotnet/viewtopic.php?t=16350
Hope this helps
Br,
Mark.
P.S. Benny, although this is really something that is open to your/ your company preference, when naming types you should (IMHO) not prefix them with "t" -that is really a Delphi thing (but if it works for you then please ignore my advice).
P.P.S. You mention that you do not understand overloading, well in essence creating an overload means that you are adding an additional function call to a class which has the same name that is distinguished by it's type signature. For instance
public void SayHello() {SayHello();} //just calling overload method to save code in class
public void SayHello(string name) { string display = "Hello"; if name.Length > 0 display += name; Console.WriteLi ne(display); }
Which will mean that the Class would provide two method calls.. 1 that allows you to SayHello without any arguments and the other to allow you to SayHello with a string argument.
***I think you are also getting a little bit confused with overloading and overriding - they are two DIFFERENT concepts. You should ideally read up on both since they are crucial to good OOP*** "Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:uK******** ******@TK2MSFTN GP15.phx.gbl... > An overloaded method is a method that differs from all methods of the > same name in so far as it has a different number or types of its > arguments. A different return type does not constitute an overload. If > you get an error stating that no overload has 0 arguments then you are > calling a method with no arguments and you have no overloaded method > that takes zero arguments. Either instantiate the class with a > constructor call with arguments or provide an explicit constructor > with zero arguments. In C# if you provide an overloaded constructor > you no longer get a default constructor (zero arguments) and need to > provide one. I think this is your problem. > > Thomas P. Skinner [MVP] > > "Benny Raymond" <be***@pocketro cks.com> wrote in message > news:%2******** ********@tk2msf tngp13.phx.gbl. .. >> I'm trying to change the way a treeview works just a bit, i'm pretty >> new to C# and now i'm running into overloading. I tried the >> following code and it's yelling at me saying that no overload method >> takes 0 arguments. >> >> #region tViewNodeCollec tion >> public class tViewNodeCollec tion : TreeNodeCollect ion >> { >> private tView _owner; >> private tViewNode _parent; >> /// <summary> >> /// Create a collection within a tViewItem >> /// </summary> >> /// <param name="parent"></param> >> public tViewNodeCollec tion(tViewNode parent) >> { >> _parent = parent; >> } >> /// <summary> >> /// Create a new instance of tViewNodeCollec tion >> /// </summary> >> /// <param name="owner"></param> >> public tViewNodeCollec tion(tView owner) >> { >> _owner = owner; >> } >> /// <summary> >> /// Create a new instance of tViewNodeCollec tion >> /// </summary> >> /// <param name="owner"></param> >> public tViewNodeCollec tion(tView owner, tViewNode parent) >> { >> _owner = owner; >> _parent = parent; >> } >> } >> #endregion >> >> >> so then I tried a series of different :this(something ) but that >> started scaring me since I really don't understand overloading - >> Could someone shed some light on this for me? > >
Mark,
I apologize if I interpreted your original statement incorrectly. However I
believe the implication was that a private default constructor always
prevents inheritance even if you did not mention overloaded constructors or
the word always. My mistake. I think you were also a bit quick to jump on my
most recent statement. Nothing I said was incorrect as stated. I only made a
mistake on what I said because you misinterpreted it.
I will ignore comment 3 and get on with things.
Thomas P. Skinner [MVP]
Mark Broadbent" <no****@nospam. com> wrote in message
news:e1******** ******@tk2msftn gp13.phx.gbl... 1. I think you will find that you did implicitly say exactly that ...."A private constructor with zero args doesn't prevent inheritance from that class" -since you were attempting to correct my original statement -which by the way mentions NOTHING about overloaded constructor in the base class and in addition nowhere in my statement do I say ALWAYS prevents -just in that one instance.
2. I was not critizing, I was correcting (or for the sake of Benny...clarify ing your misleading statement). So before you start flaming me please think about what you are saying and take time to read the posts properly.
3. Your MVP doesnot entitle you to be correct on every occasion so get over it and move on!
NUFF SAID!
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. Yes, but that's not what I said. Read it again at bit more carefully before criticizing.. I was responding to a conjecture that a private default constructor always prevents inheritance. It doesn't if there are public overloaded constructors as you well point out. I never said a class with ONLY a private default constructor can be inherited.
Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message news:Or******** *****@TK2MSFTNG P11.phx.gbl... Hi Thomas please see replies....
"Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message news:uF******** ******@TK2MSFTN GP15.phx.gbl... A private constructor with zero args doesn't prevent inheritance from that class. Basically the private constructor with zero args is normally used to force the programming to instantiate the class with an overloade d constructor. The derived class would need to explicitly invoke the overloaded constructor in the base class.
Incorrect - A private constructor *will* prevent inheritance *if* it is the only constructor available in the class (which I suggested in the original post). Your statement is correct only *if* there is an available constructor visible to the code outside the class (public) or to the inheriting class (protected - if both classes in same assembly). One reason you might want to do this is to enforce strict object creation through an object factory method. For instance the following class cannot be inherited... public class Parent { string name; private Parent(string s) //only constructor: is private and prevents inheritance since any inheriting classes cannot see the base constructor { name = s; }
public static Parent CreateParent(st ring s) { if (s == null || s.Length == 0) { throw new Exception("Inva lid string specified when building new object"); } return new Parent(s); } }
You are obviously correct is stating another reason why a private default constructor is used -to enforce construction through an overloaded constructor *although* this is strictly not necessary since a default constructor is only used if :- i. No constructor is specified in a class ii. If the default constructor has been explicitly declared - which is the default behaviour of Visual Studio code generator. For instance the following code can only be created using the constructor with a string params...
public class Class2 { //the lack of default constructor effectively means that the class can only //be instanciated using the constructor with a string parameter. public Class2(string s) { } }
The TreeNodeCollect ion does seem a bit strange in that no constructors are documented. That would mean the class must be constructed via a public static method. However I can't find one documented. It appears that the TreeNodeCollect ion gets instantiated by the TreeView class by some subtle method. In this case I agree that one can't inherit from TreeNodeCollect ion. BTW, TreeNodeCollect ion is NOT documented as sealed. The result is the same I guess. Interesting.
Thankfully Chris has got to the bottom of this one. Interesting subject this. Like you I noticed the lack of constructor documentation for this class. I really wish Microsoft would document exactly the internals of the class libraries -but I guess you cant have everything.
Best Regards,
Mark Broadbent. Thomas P. Skinner [MVP]
"Mark Broadbent" <no****@nospam. com> wrote in message news:eK******** ******@TK2MSFTN GP14.phx.gbl... >I believe that the constructor of the TreeViewCollect ion has been set >to private so effectively it can't be inherited. > The error message is a bit misleading though (it suggests that there > is a public constructor that does not have zero args - and hence in > that case you would be able to call it from your derived class) . > > see for instance... > http://www.groupsrv.com/dotnet/viewtopic.php?t=16350 > > Hope this helps > > Br, > > Mark. > > P.S. > Benny, although this is really something that is open to your/ your > company preference, when naming types you should (IMHO) not prefix > them with "t" -that is really a Delphi thing (but if it works for you > then please ignore my advice). > > P.P.S. > You mention that you do not understand overloading, well in essence > creating an overload means that you are adding an additional function > call to a class which has the same name that is distinguished by it's > type signature. > For instance > > public void SayHello() > {SayHello();} //just calling overload method to save code in > class > > public void SayHello(string name) > { string display = "Hello"; > if name.Length > 0 > display += name; > Console.WriteLi ne(display); > } > > Which will mean that the Class would provide two method calls.. > 1 that allows you to SayHello without any arguments and the other to > allow you to SayHello with a string argument. > > ***I think you are also getting a little bit confused with overloading > and overriding - they are two DIFFERENT concepts. You should ideally > read up on both since they are crucial to good OOP*** > > > > "Thomas P. Skinner [MVP]" <to*@bu.edu> wrote in message > news:uK******** ******@TK2MSFTN GP15.phx.gbl... >> An overloaded method is a method that differs from all methods of the >> same name in so far as it has a different number or types of its >> arguments. A different return type does not constitute an overload. >> If you get an error stating that no overload has 0 arguments then you >> are calling a method with no arguments and you have no overloaded >> method that takes zero arguments. Either instantiate the class with a >> constructor call with arguments or provide an explicit constructor >> with zero arguments. In C# if you provide an overloaded constructor >> you no longer get a default constructor (zero arguments) and need to >> provide one. I think this is your problem. >> >> Thomas P. Skinner [MVP] >> >> "Benny Raymond" <be***@pocketro cks.com> wrote in message >> news:%2******** ********@tk2msf tngp13.phx.gbl. .. >>> I'm trying to change the way a treeview works just a bit, i'm pretty >>> new to C# and now i'm running into overloading. I tried the >>> following code and it's yelling at me saying that no overload method >>> takes 0 arguments. >>> >>> #region tViewNodeCollec tion >>> public class tViewNodeCollec tion : TreeNodeCollect ion >>> { >>> private tView _owner; >>> private tViewNode _parent; >>> /// <summary> >>> /// Create a collection within a tViewItem >>> /// </summary> >>> /// <param name="parent"></param> >>> public tViewNodeCollec tion(tViewNode parent) >>> { >>> _parent = parent; >>> } >>> /// <summary> >>> /// Create a new instance of tViewNodeCollec tion >>> /// </summary> >>> /// <param name="owner"></param> >>> public tViewNodeCollec tion(tView owner) >>> { >>> _owner = owner; >>> } >>> /// <summary> >>> /// Create a new instance of tViewNodeCollec tion >>> /// </summary> >>> /// <param name="owner"></param> >>> public tViewNodeCollec tion(tView owner, tViewNode parent) >>> { >>> _owner = owner; >>> _parent = parent; >>> } >>> } >>> #endregion >>> >>> >>> so then I tried a series of different :this(something ) but that >>> started scaring me since I really don't understand overloading - >>> Could someone shed some light on this for me? >> >> > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alex VanderWoude |
last post by:
Okay, now I know how to directly reference a particular overload of a
method in a cref. However, now I have the "opposite" problem. I want to
directly reference the general summary page for all the overloaded
methods. So far I've been able to do this by not mentioning any
parameters in the method name, but in one case it seems to be failing and
picking one of the overloads instead.
For example, consider the following two overloads:...
|
by: Vladimir Granitsky |
last post by:
Hi guys,
Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!?
Am I right that C# compiler does wrong overload resolution ?
I've used parameters of type object and string here, just to illustrate the problem. Really I have a bit more deep inheritance graph, and the things get more interesting if the strongly typed overload is like override public void...
|
by: Dave Raskin |
last post by:
public class Base
{
}
public class Derived : Base
{
}
public class Service
{
|
by: MattB |
last post by:
Hello. I'm pretty ignorant when it comes to asp.net but I'm working to
change that.
In another thread I was asking about using ReadXML to grab an ADO record set
I have in a string and make it usable. Eric offered a promising solution:
"The myDataSet.ReadXml method also has an overload that accepts an argument
derived from TextReader. Pass it a StringReader that wraps around the string
containing the XML you want to load and you're all...
|
by: Tony |
last post by:
I have an operator== overload that compares two items and returns a new
class as the result of the comparison (instead of the normal bool)
I then get an ambiguous operater compile error when I attempt to check to
see if the object is null:
"The call is ambiguous between the following methods or properties:
'TestObject.operator ==(TestObject, string)' and 'TestObject.operator
==(TestObject, TestObject)"
Does anyone have any idea how to...
| |
by: Shea Martin |
last post by:
I have a struct like so:
struct MyStruct
{
public:
void Value( int newValue ) { mValue = newValue; }
int Value() const { return mValue; }
private:
int mValue;
|
by: Soli3d |
last post by:
Is there a way to Inherit from a Method? I don't want to have to repeat
the code in my method over and over for the overloaded versions. If I need
to make changes I don't want to have to reapeat them in each overloaded
version of my method.
Better way to do this?
Thank you,
Paul
|
by: Christof Nordiek |
last post by:
Given the following code
Is there any way to call one of the Bar methods.
using System;
class Program
{
static void Main()
{
Foo<string, stringfoo = new Foo<string,string>();
|
by: epanda |
last post by:
Hi,
I would like to merge two class which have got 90% of same source
code. But I don't know what to do.
class A {
int putLightOn () {
switch(status)
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |