471,570 Members | 1,007 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Switch Statement Question

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.Portable DocFormat);
break;
default:
break;
}

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

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200512/1
Dec 16 '05 #1
2 1599
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.

Dec 16 '05 #2
Bruce Wood wrote:
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.

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...sharp/200512/1
Dec 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Aristotelis E. Charalampakis | last post: by
35 posts views Thread by Thomas Matthews | last post: by
18 posts views Thread by Minti | last post: by
3 posts views Thread by pgraeve | last post: by
13 posts views Thread by Michael Griebe | last post: by
4 posts views Thread by priyanka | last post: by
12 posts views Thread by | last post: by
13 posts views Thread by Satya | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.