473,386 Members | 1,958 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,386 software developers and data experts.

Setting readonly variables

I've applied readonly variables to my code where its seemed useful to do so,
such as
readonly string m_name;

public MyClass(string name)
{ m_name = name); }

Where I have had multiple constructors for a class I have had to apply all
the settings to one constructor or set each variable in different
constructors:

public MyClass(string name) : this(name, "")
{ }
public MyClass(string name, string address)
{ m_name = name;
m_address = address; }

However is it possible to set multiple constructors through a method in the
class called by the constructor? Something like:

public MyClass(string name) : this(name, "")
{ }
public MyClass(string name, string address)
{ Initalize(); }
private void Initialize()
{
m_name = name;
m_address = address;
}

Nov 17 '05 #1
3 1316
You know what's funny? It would have taken you roughly the same amount of
time to type that code into a cs file and try it. :¬)

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

"J Mon" <J Mo*@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
I've applied readonly variables to my code where its seemed useful to do so, such as
readonly string m_name;

public MyClass(string name)
{ m_name = name); }

Where I have had multiple constructors for a class I have had to apply all
the settings to one constructor or set each variable in different
constructors:

public MyClass(string name) : this(name, "")
{ }
public MyClass(string name, string address)
{ m_name = name;
m_address = address; }

However is it possible to set multiple constructors through a method in the class called by the constructor? Something like:

public MyClass(string name) : this(name, "")
{ }
public MyClass(string name, string address)
{ Initalize(); }
private void Initialize()
{
m_name = name;
m_address = address;
}

Nov 17 '05 #2
That's just it the compiler won't let me do this. I was wondering if there
is a way around the limitation?

"Tim Haughton" wrote:
You know what's funny? It would have taken you roughly the same amount of
time to type that code into a cs file and try it. :¬)

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

"J Mon" <J Mo*@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
I've applied readonly variables to my code where its seemed useful to do

so,
such as
readonly string m_name;

public MyClass(string name)
{ m_name = name); }

Where I have had multiple constructors for a class I have had to apply all
the settings to one constructor or set each variable in different
constructors:

public MyClass(string name) : this(name, "")
{ }
public MyClass(string name, string address)
{ m_name = name;
m_address = address; }

However is it possible to set multiple constructors through a method in

the
class called by the constructor? Something like:

public MyClass(string name) : this(name, "")
{ }
public MyClass(string name, string address)
{ Initalize(); }
private void Initialize()
{
m_name = name;
m_address = address;
}


Nov 17 '05 #3
> However is it possible to set multiple constructors through a method in the
class called by the constructor?


readonly fields can only be set inline (i.e on the line where they are
declared) or inside a constructor, so I think you are stuck putting them in
one constructor that the other constructors call.
Nov 17 '05 #4

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

Similar topics

5
by: blue | last post by:
I know, I know, global variables are bad bad bad. I have some readonly variables that I assign right away and they will never change (hence the "readonly" part). I need to access these variables...
19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
0
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
2
by: Martin Gregersen | last post by:
Hi Looking for a way to bind a readonly property on a textbox to a parameter, not having to cycle through all controls, but rather setting <textbox id="myTextbox" readonly="<%# SetReadOnly...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
11
by: John Dalberg | last post by:
I would like to declare variables that can't be modified (like readonly) however I want to declare them in a single class so I can always refer to the same class whenever code uses them. However I...
8
by: Weeble | last post by:
I was surprised to discover today that local variables can't be "readonly". (They can be "const", but that's only for compile-time constants, not something that might be calculated at run-time.) I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.