Switch Statement Question

  • Thread starter Cathleen C via DotNetMonster.com
  • Start date
C

Cathleen C via DotNetMonster.com

I'm a semi-beginner with c# and am having a problem effectively implementing
a switch statement. I've created an asp.net app that runs a report depending
on which item was selected from a drop down box from a previous page. I
then have an Export button to convert the resultant report into pdf. I have
it working but wanted to stream line the code.

The oStream variable is set with the switch statement. After the switch I
attempt to use the oStream variable but receive and error message that its
unassigned. If I move the code to with each case it works fine. Its as if
the variable is local to the switch and I can't use it outside the statement.
This is probably super-simple to fix, I'm just not sure how to do it. Can
someone tell me where I'm going wrong?


private void cmdExport_Click(object sender, System.EventArgs e)
{
string report = Request.QueryString["report"];

MemoryStream oStream; // Using System.IO

switch(report)
{
case "con004":
oStream = (MemoryStream) rptCon004.ExportToStream(CrystalDecisions.
Shared.ExportFormatType.PortableDocFormat);
break;
case "con012":
oStream = (MemoryStream) rptCon012.ExportToStream(CrystalDecisions.
Shared.ExportFormatType.PortableDocFormat);
break;
case "con020":
oStream = (MemoryStream) rptCon020.ExportToStream(CrystalDecisions.
Shared.ExportFormatType.PortableDocFormat);
break;
case "routeHome":
oStream = (MemoryStream) rptRouteHome.ExportToStream(CrystalDecisions.
Shared.ExportFormatType.PortableDocFormat);
break;
case "emergencyContact":
oStream = (MemoryStream) rptEmergencyContact.ExportToStream
(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
break;
default:
break;
}

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(oStream.ToArray());
Response.End();
}
 
B

Bruce Wood

You don't assign anything to oStream in the "default" case, and so
there is a possible path through the switch that will result in an
unassigned oStream. That's what the compiler is complaining about:
_every possible_ path through a switch (or if) must assign something to
an unassigned variable in order that it be considered "assigned" once
the statement finishes.
 
C

Cathleen C via DotNetMonster.com

Bruce said:
You don't assign anything to oStream in the "default" case, and so
there is a possible path through the switch that will result in an
unassigned oStream. That's what the compiler is complaining about:
_every possible_ path through a switch (or if) must assign something to
an unassigned variable in order that it be considered "assigned" once
the statement finishes.


Ah, I knew it would be something simple! Thanks, Bruce! I'll give that a
try.
 

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

Top