Re: Is it good programming to set instance in class without using C-tor

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Tony Johansson wrote:
I have a class definition called MyClass see below.

I create an instance of this class MyClass
I also want this instance to be able to modify the test instance that exist in this class.

I can make the instance of this class MyClass to be able to access the instance test in two ways.

I can either pass an instance of this class MyClass in the c-tor and assign the instance to the test field.

I can also use a set property by calling the property. Here I set an instance to the test field.

In general I prefer both:
- no arg constructor + property setter
- constructor with args

But in your specific case (where the property is the same
type as the class itself) I think you can not pass
the arg in the constructor or you will get an infinite
source code (unless you have another way of creating the
test object).

Arne
.



Relevant Pages

  • Re: Is it good programming to set instance in class without using C-tor
    ... I create an instance of this class MyClass ... I can also use a set property by calling the property. ... no arg constructor + property setter ... the arg in the constructor or you will get an infinite ...
    (microsoft.public.dotnet.languages.csharp)
  • Trouble with non-default constructors
    ... I have a class that needs a non-default constructor, ... class MyClass ... int Value; ... But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to share data?
    ... > int getdata; ... Why is readdata() public? ... Your constructor copies the values ...
    (comp.programming)
  • Re: Object instantiation in C#
    ... >> I have a class MyClass that throws exceptions. ... > When an UNCAUGHT exception occurs in a constructor, ... > pointing to whatever it was pointing to before. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trouble with non-default constructors
    ... > I have a class that needs a non-default constructor, ... > class MyClass ... > void GetValue{return Value); ... > But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)