473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 23 '05 #1
12 1174
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 23 '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 23 '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 23 '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 23 '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 23 '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 23 '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 23 '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 23 '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 23 '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 23 '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 23 '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

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
6
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of...
12
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...
2
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...
4
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...
1
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
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...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
5
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

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.