string variables in C#

  • Thread starter Thread starter C#leaner
  • Start date Start date
C

C#leaner

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);
}
 
C#leaner said:
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 - Think he is reviewing the MSPress book. It looks too familiar to me.
:-)



Jon Skeet said:
C#leaner said:
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,

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 said:
C#leaner said:
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.)
 
Cor Ligthert said:
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;

where words is an appropriate list or array of strings...
 
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.

C#leaner said:
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.)
 
Back
Top