473,513 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read-Only Constructor

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.

Nov 22 '05 #1
12 1044
Sathyaish wrote:
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 22 '05 #2
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" wrote:
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.

Nov 22 '05 #3
"Sathyaish" <sa*******@gmail.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 22 '05 #4
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; }
}
}
Nov 22 '05 #5
Search on Private Constructor

http://www.fawcette.com/vsm/2003_02/...efault_pf.aspx

A private constructor is used when all methods of a class are shared and you
want to prevent someone from creating an instance of the class.

Class MyClass

Private Sub New()
' no code here
End Sub

Public Shared Sub DoSomething()
...
End Sub

End Class

Greg

"Sathyaish" <sa*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.

Nov 22 '05 #6
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!
***********************************************
"Sathyaish" <sa*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.

Nov 22 '05 #7
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]
"Sathyaish" <sa*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.

Nov 22 '05 #8
Cowboy (Gregory A. Beamer) <No************@comcast.netNoSpamM> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 22 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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
Nov 22 '05 #10
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
Nov 22 '05 #11
Greg Burns <bl*******@newsgroups.nospam> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.


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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 22 '05 #12
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.

Nov 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
11631
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I currently have as a test for using std::ifstream::read(). Is there anything wrong with the way I'm getting the file? #include <vector> #include...
2
3070
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing this by aggregating new content from all databases into a single indexed database and then saving a timestamp in the account database (for the...
4
3834
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a socket read after a certain delay. The logic is (I will provide a test program below): - create and connect socket
1
3980
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
0
4697
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made the client application establish a connection, and send data, which appears in plain, de-crypted text on the server - this works.
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7397
Oralloy
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. ...
0
7565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5103
isladogs
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...
0
4759
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...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.