473,406 Members | 2,220 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,406 software developers and data experts.

string variables in C#

In the following code, I am getting a compile error saying that use of
unassigned local variable d.
Can anyone help?
----------------------------------------------------------------------------------------------

string d;
int i=1;
while (i<13)
{
i++;

if (i==1)
d="first";

if (i==2)
d="second";

Console.WriteLine("\nOn the [0] day of Christmas", d);
}

Sep 27 '06 #1
6 4274
I saw my error [0] which needs to be {0}

C#leaner wrote:
In the following code, I am getting a compile error saying that use of
unassigned local variable d.
Can anyone help?
----------------------------------------------------------------------------------------------

string d;
int i=1;
while (i<13)
{
i++;

if (i==1)
d="first";

if (i==2)
d="second";

Console.WriteLine("\nOn the [0] day of Christmas", d);
}
Sep 27 '06 #2
C#leaner <am**********@yahoo.comwrote:
I saw my error [0] which needs to be {0}
That's got nothing to do with the use of the unassigned variable.

The compiler can't detect wheter d will be assigned a value in every
case. If you do:

if (i==1)
{
d = "first";
}
else if (i==2)
{
d = "second";
}
// snip other cases
else
{
d = "other";
}

then it'll be fine.

(I assume you realise that using an array would be the more elegant way
of doing this, and are just working through some code at the moment.)

--
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
Sep 27 '06 #3
Jon - Think he is reviewing the MSPress book. It looks too familiar to me.
:-)

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
C#leaner <am**********@yahoo.comwrote:
>I saw my error [0] which needs to be {0}

That's got nothing to do with the use of the unassigned variable.

The compiler can't detect wheter d will be assigned a value in every
case. If you do:

if (i==1)
{
d = "first";
}
else if (i==2)
{
d = "second";
}
// snip other cases
else
{
d = "other";
}

then it'll be fine.

(I assume you realise that using an array would be the more elegant way
of doing this, and are just working through some code at the moment.)

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

Sep 27 '06 #4
Jon,

And than he can use the d variable global inside your method as clear is
intended in the question?

This is in my idea much easier in VB.Net and therefore is the question from
C#learner probably.

I could solve this only with

string d = "";

In the shortest notation I know while this is in VB.Net

dim d as string

Which means an address declaration inside a method and not an assignment.

However maybe I oversee something and than I will be as well lucky with your
answer.

Cor
"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP************************@msnews.microsoft.c om...
C#leaner <am**********@yahoo.comwrote:
>I saw my error [0] which needs to be {0}

That's got nothing to do with the use of the unassigned variable.

The compiler can't detect wheter d will be assigned a value in every
case. If you do:

if (i==1)
{
d = "first";
}
else if (i==2)
{
d = "second";
}
// snip other cases
else
{
d = "other";
}

then it'll be fine.

(I assume you realise that using an array would be the more elegant way
of doing this, and are just working through some code at the moment.)

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

Sep 28 '06 #5
Cor Ligthert [MVP] <no************@planet.nlwrote:
And than he can use the d variable global inside your method as clear is
intended in the question?
Well, he could use the "d" variable anywhere else it's in scope,
because it's definitely assigned at that point - every branch the code
could take (regardless of i's value) assigns to d.
This is in my idea much easier in VB.Net and therefore is the question from
C#learner probably.

I could solve this only with

string d = "";

In the shortest notation I know while this is in VB.Net

dim d as string
That's still longer than the C# version :)
Which means an address declaration inside a method and not an assignment.
At that point, depending on the version/settings you will either get a
warning (the default with VB.NET in .NET 2.0) or you'll risk using the
variable before it's been assigned to without anything telling you (in
..NET 1.1). I prefer to be told about potential programming issues,
myself.
However maybe I oversee something and than I will be as well lucky with your
answer.
Well, my if/else answer would certainly work, but it's not as elegant
as:

string d = words[i];

where words is an appropriate list or array of strings...

--
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
Sep 28 '06 #6
Yeah, I realized he unassigned variable too, after fixing {0}.
Intersetign that if I use if else, I wouldn't need to initialize the
varaibale at decalraing stage.

Btw, I was required to use if structure for this assignments in C#
class. Thanks though.
Jon wrote:
C#leaner <am**********@yahoo.comwrote:
I saw my error [0] which needs to be {0}

That's got nothing to do with the use of the unassigned variable.

The compiler can't detect wheter d will be assigned a value in every
case. If you do:

if (i==1)
{
d = "first";
}
else if (i==2)
{
d = "second";
}
// snip other cases
else
{
d = "other";
}

then it'll be fine.

(I assume you realise that using an array would be the more elegant way
of doing this, and are just working through some code at the moment.)

--
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
Sep 29 '06 #7

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

Similar topics

12
by: Eli Daniel | last post by:
Hi, I'm new to Python. Can you please tell me if the following is possible. My users are using other scripting launguage to write scripts. They are used to write somthing like (keeping it...
14
by: brad | last post by:
I've got a multithreaded application using std::string in Linux. Performance is not very good so I ran Quantify(tm) to look at what is happening. Most of the time my app was calling...
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
10
by: ksrajalakshmi | last post by:
Hai , in a textbox am having text as "(20/100)+pay1*pay2" .it's a formula. and stored in a particular variable. string strformula="(20/100)+pay1*pay2" ; i've to substitute the value of the...
5
by: Chris H | last post by:
Okay, I am trying to us a varialble with a list of number values as my data for an array, yet when I do this it doesnt return any values from the array, i was able to temporarily fix the proble by...
7
by: Andy C Matthews | last post by:
Hi there, I'm building an Access database and using VBA to generate Microsoft Word mailings for customers. It's all going fine so far. However, a variables named ParcelID, a ten-digit string such...
2
by: comp.lang.php | last post by:
I am trying to replace within the HTML string $html the following: With Where I'm replacing "action=move_image" with "action=<?= $_REQUEST ?>"
11
by: RipperT | last post by:
Don't know if this group covers web apps, but here goes. In VS 2005, I am trying to get variables to hold thier values during postback from the server. I convert a text box's user-keyed value to an...
1
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...

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.