a string variable

B

Bernie V

Hi group,

I'm trying to do this:
String DatVandaag;
DatVandaag=DateTime.Today.AddDays(-2).ToString("dd/MM/yyyy");
LiteralHeat.Text = "<center><H5>Wekelijkse vinyl TOP 10
("+DatVandaag+")</center></H5><br><br>";
I get the following error: Use of unassigned local variable 'DatVandaag'

Can somebody help me what I do worng ?

Thx in advance !

grz
Bernie V
 
A

Alvin Bruney [MVP]

I'm not getting the error you described. Maybe you have left out some code.

Try this String DatVandaag = String.Empty;
 
C

Curt_C [MVP]

When you are typing in the line
LiteralHeat.Text = "......("+DatVandaag+")...";
When you get to DatVandaag hit the "." key... do you get the Intellisense
popup?

Also try combining and losing the DatVandaag variable all together...

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com


Bernie V said:
Hi Curt,

I changed it, but it get still the same error (Use of unassigned local
variable 'DatVandaag')

grz
Bernie V
 
M

Michael Earls

I get the feeling you're not showing us all the code. Can you paste the
entire method? It looks like you might have the DatVandaag variable
declared outside of a "using" or "try" block. You'll need to define the
variable in the same "scope" that you use it.
 
B

Bernie V

Hi Michael,

This is the entire code:


private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack){
MakeChartPage();
}
}
private void MakeChartPage()
{
string DatVandaag;
DateTime dt = DateTime.Now;
if ((int)dt.DayOfWeek != 5)
{
switch ((int)dt.DayOfWeek)
{
case 0:
DatVandaag=DateTime.Today.AddDays(-2).ToString("dd/MM/yyyy");
break;
case 1:
DatVandaag=DateTime.Today.AddDays(-3).ToString("dd/MM/yyyy");
break;
case 2:
DatVandaag=DateTime.Today.AddDays(-4).ToString("dd/MM/yyyy");
break;
case 3:
DatVandaag=DateTime.Today.AddDays(-5).ToString("dd/MM/yyyy");
break;
case 4:
DatVandaag=DateTime.Today.AddDays(-6).ToString("dd/MM/yyyy");
break;
case 6:
DatVandaag=DateTime.Today.AddDays(-1).ToString("dd/MM/yyyy");
break;
}
}
else
{
DatVandaag=DateTime.Today.ToString("dd/MM/yyyy");
}
switch (Request.QueryString.Get("type"))
{
case "w":
cd.DocumentSource=".\\xml\\wchart.xml";
cd.TransformSource=".\\stylesheet\\wchart.xsl";
LiteralHeat.Text = "<center><H5>Wekelijkse vinyl TOP 10
("+DatVandaag+")</center></H5><br><br>";
LiteralChart.Text = "Elke week vind je hier mijn vinyl TOP 10.<br>Deze TOP
10 is samengesteld uit vinyls die ik zelf bezit.<br>";
break;
case "a":
cd.DocumentSource=".\\xml\\achart.xml";
cd.TransformSource=".\\stylesheet\\achart.xsl";
LiteralHeat.Text = "<center><H5>Vinyl TOP 10
allertijden</center></H5><br><br>";
LiteralChart.Text = "Hier zijn ze dan, volgens mijn persoonlijke menig de
beste vinyls.<br>"+
"De meeste tracks komen uit de jaren '90. Zelf vind ik dat er toen in die
"+
" tijd songs van veel betere kwaliteit gemaakt werden.<br>";
break;
}
}

--
http://www.djberniev.be


Michael Earls said:
I get the feeling you're not showing us all the code. Can you paste the
entire method? It looks like you might have the DatVandaag variable
declared outside of a "using" or "try" block. You'll need to define the
variable in the same "scope" that you use it.
 
C

Curt_C [MVP]

It shouldn't be....it's not a Text input type.
After seeing your other post with the code though, step through it line by
line and follow the value set.

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com
 
D

David D. McCrory

Set your initial declaration equal to null...

string DatVandaag = null;


Bernie V said:
Hi Michael,

This is the entire code:


private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack){
MakeChartPage();
}
}
private void MakeChartPage()
{
string DatVandaag;
DateTime dt = DateTime.Now;
if ((int)dt.DayOfWeek != 5)
{
switch ((int)dt.DayOfWeek)
{
case 0:
DatVandaag=DateTime.Today.AddDays(-2).ToString("dd/MM/yyyy");
break;
case 1:
DatVandaag=DateTime.Today.AddDays(-3).ToString("dd/MM/yyyy");
break;
case 2:
DatVandaag=DateTime.Today.AddDays(-4).ToString("dd/MM/yyyy");
break;
case 3:
DatVandaag=DateTime.Today.AddDays(-5).ToString("dd/MM/yyyy");
break;
case 4:
DatVandaag=DateTime.Today.AddDays(-6).ToString("dd/MM/yyyy");
break;
case 6:
DatVandaag=DateTime.Today.AddDays(-1).ToString("dd/MM/yyyy");
break;
}
}
else
{
DatVandaag=DateTime.Today.ToString("dd/MM/yyyy");
}
switch (Request.QueryString.Get("type"))
{
case "w":
cd.DocumentSource=".\\xml\\wchart.xml";
cd.TransformSource=".\\stylesheet\\wchart.xsl";
LiteralHeat.Text = "<center><H5>Wekelijkse vinyl TOP 10
("+DatVandaag+")</center></H5><br><br>";
LiteralChart.Text = "Elke week vind je hier mijn vinyl TOP 10.<br>Deze TOP
10 is samengesteld uit vinyls die ik zelf bezit.<br>";
break;
case "a":
cd.DocumentSource=".\\xml\\achart.xml";
cd.TransformSource=".\\stylesheet\\achart.xsl";
LiteralHeat.Text = "<center><H5>Vinyl TOP 10
allertijden</center></H5><br><br>";
LiteralChart.Text = "Hier zijn ze dan, volgens mijn persoonlijke menig de
beste vinyls.<br>"+
"De meeste tracks komen uit de jaren '90. Zelf vind ik dat er toen in die
"+
" tijd songs van veel betere kwaliteit gemaakt werden.<br>";
break;
}
}
 
M

Michael Earls

Good call.

You might also consider a default case on your switches to set it to some
value based on the fall-through logic.
 
H

Hans Kesting

See inline

Bernie V said:
Hi Michael,

This is the entire code:


private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack){
MakeChartPage();
}
}
private void MakeChartPage()
{
string DatVandaag;
DateTime dt = DateTime.Now;
if ((int)dt.DayOfWeek != 5)
{
switch ((int)dt.DayOfWeek)
{
case 0:
DatVandaag=DateTime.Today.AddDays(-2).ToString("dd/MM/yyyy");
break;
case 1:
DatVandaag=DateTime.Today.AddDays(-3).ToString("dd/MM/yyyy");
break;
case 2:
DatVandaag=DateTime.Today.AddDays(-4).ToString("dd/MM/yyyy");
break;
case 3:
DatVandaag=DateTime.Today.AddDays(-5).ToString("dd/MM/yyyy");
break;
case 4:
DatVandaag=DateTime.Today.AddDays(-6).ToString("dd/MM/yyyy");
break;
case 6:
DatVandaag=DateTime.Today.AddDays(-1).ToString("dd/MM/yyyy");
break;

At this point the compiler doesn't know that all possible cases have been
handled.
You as a developer know that (int)DayOfWeek returns a value 0-6, and you
also know that case 5 has been handled elsewhere. The compiler however
just notices "int" as return type and sees that not all possible values lead
to
an assignment to DatVandaag. That is why it is still flagged as "possibly
unassigned".

}
}
else
{
DatVandaag=DateTime.Today.ToString("dd/MM/yyyy");
} [snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top