473,320 Members | 1,936 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,320 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 4268
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.