Read-Only Constructor

  • Thread starter Thread starter Sathyaish
  • Start date Start date
S

Sathyaish

So, is there something called a Read-Only constructor in the .NET
framework? Someone mentioned the term to me and I've been trying to
make sense out of it. I didn't get a link on the Web even.
 
Sathyaish said:
So, is there something called a Read-Only constructor in the .NET
framework? Someone mentioned the term to me and I've been trying to
make sense out of it. I didn't get a link on the Web even.

I think you need to check with the source of that term because it
doesn't make any sense at all.

A property can be read only in the sense that you can't write:

obj.PropName = newValue;

and get it to compile.

A constructor on the other hand is a method so it is called, not read or
written to.
 
Perhaps you're refering to the readonly keyword that can be used to make
fields readonly except from the constructor:
class MyClass {
public readonly int myAge; // can only be assigned to by the constructor
public MyClass(int age) {
myAge = age;
}
}
 
Sathyaish said:
So, is there something called a Read-Only constructor in the .NET
framework? Someone mentioned the term to me and I've been trying to
make sense out of it. I didn't get a link on the Web even.

I suggest to provide some more details on what you want to archieve.
 
Hey Sathyaish

The other guys are right, there's no such term. You may be referring to a
private constructor which only the class can use. This is typically used in
the singleton pattern such as:

class MySingleton
{
private static readonly MySingleton _singleCopy = new MySingleton();

private MySingleton()
{ // Some init here }

public MySingleton Instance
{
get { return _singleCopy; }
}
}
 
Let's see what we know about constructors:

1. They can be scoped at a variety of levels. There is the public
constructor, which is the most common. You can also make the constructor
private, which is useful for the Singleton pattern.

2. They can be static (Shared in VB.NET) or instance. In other words, you
can set up a constructor to set properties that apply to all instances of
the object.

3. They can be overloaded, including the possibility of different scopes on
each one.

4. They can be chained, where one calls another, to avoid doing the same
work in multiple places.

I am not sure what a "read only" constructor means, however.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***********************************************
Think Outside the Box!
***********************************************
 
I guess that the following is ment properties only to be set by the
constructor

so we have data that must be set in the class however the data can not be
changed after the class is created


like this :

Friend Class ClsDemo
Private _datapath As String

Friend ReadOnly Property Datapath()

Get

Return _datapath

End Get

End Property

Public Sub New(ByVal dbpath As String)

_datapath = dbpath

End Sub

End Class

regards

Michel Posseth [MCP]
 
Cowboy (Gregory A. Beamer) said:
2. They can be static (Shared in VB.NET) or instance. In other words, you
can set up a constructor to set properties that apply to all instances of
the object.

static doesn't mean the properties apply to all instances of the
object. It means they apply to the type itself - there don't need to be
*any* instances created to use them, and indeed if you try to use a
static property as if it *were* owned by an instance, you'll get a
compiler error.
 
Jon Skeet said:
static doesn't mean the properties apply to all instances of the
object. It means they apply to the type itself - there don't need to be
*any* instances created to use them, and indeed if you try to use a
static property as if it *were* owned by an instance, you'll get a
compiler error.

Wasn't that compiler error previoulsy only available to C# and not VB? I
think VS 2005 now catches it though.

Greg
 
Greg Burns said:
Wasn't that compiler error previoulsy only available to C# and not VB? I
think VS 2005 now catches it though.

Greg

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Main()
Class1.DoSomething() ' ok in VS 2003 & 2005

Dim myClass1 As New Class1
myClass1.DoSomething() ' ok in VS 2003, but won't compile in 2005

End Sub

End Class

Public Class Class1
Public Shared Sub DoSomething()
' foobar
End Sub
End Class
 
Greg Burns said:
Wasn't that compiler error previoulsy only available to C# and not VB? I
think VS 2005 now catches it though.

I was only actually talking about C# (hence mentioning static rather
than static/Shared) but I'm sorry I wasn't clearer.

Yes, it's a pity that you can access static members in VB.NET as if
they were instance members. Java has that flaw too - fortunately
Eclipse let's me make it a warning or error, just as VS 2005 does for
VB :)
 
Sorry if the OP was a bit vague. I did mention that the word did not
make sense to me and that I'd read it somewhere. Here's the background:

I got an email from a consultant for an opening with a company and
knowledge of a few programming concepts was the pre-requisite for the
preliminary interview. One of the points in the bulleted list of
concepts was _Read-Only Constructors_.

I discounted the suspicion that it could be a typo, or something. Yet,
I wanted to check with others, and hence the OP.

I do know about conversion constructors, copy constructors,
parameterized and parameterless constructors, public and private
constructors and static constructors. I also know about read-only
fields. I also know about the singleton pattern.

I wasn't trying to achieve anything in particular except decipher the
contents of an email I'd recieved. My confusion was precisely to do
with what I'd written - having never heard of the term Read-Only
Constructor.

Thanks.
 
Back
Top