Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

initialising strings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 27 Mar 2006 10:25 GMT
Hi,
In the IDE is there any way to turn off the compiler warning about strings
being used before being initialised.
Given that a string is an immutable object why bother initialising it before
assigning a value to it?
Thanks
Bob
Nick Hounsome - 27 Mar 2006 11:10 GMT
> Hi,
> In the IDE is there any way to turn off the compiler warning about strings
[quoted text clipped - 4 lines]
> Thanks
> Bob

A string is immutable but a variable of type string is a reference to a
string and can be changed to a completely different (immutable) string:

string a = "immutable 1";
a = "immutable 2"; // OK
a[2] = 'z';  // error - immutable.

What you are thinking of is a "readonly" string.
Jon Skeet [C# MVP] - 27 Mar 2006 11:23 GMT
> In the IDE is there any way to turn off the compiler warning about strings
> being used before being initialised.
> Given that a string is an immutable object why bother initialising it before
> assigning a value to it?

It sounds like you don't quite understand the error message properly.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Then we can try to explain the message.

Signature

Jon Skeet - <skeet@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

Bob - 27 Mar 2006 22:02 GMT
Hi Jon,
Good Call. I went back and re-examined the code and there was a path where a
string could have been used with a null value. I shall check the rest.
Thanks
Bob

> > In the IDE is there any way to turn off the compiler warning about strings
> > being used before being initialised.
[quoted text clipped - 10 lines]
>
> Then we can try to explain the message.
Jon Skeet [C# MVP] - 28 Mar 2006 06:15 GMT
> Good Call. I went back and re-examined the code and there was a path where a
> string could have been used with a null value. I shall check the rest.

Right - the problem isn't that it could have been used with a null
value. The problem is that local variables don't *have* a value
(logically at least) until they've been definitely assigned.

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.