473,486 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1675
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1927
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
35
8288
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
18
3415
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
18
3042
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
3
19724
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
13
7524
by: Michael Griebe | last post by:
Simple question. I am optimizing some C++ code and I'd like to know which is faster (or if there is any difference at all) between using a switch statement or nested else-ifs. I'm partial to...
4
4973
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
12
12251
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
13
11765
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.