473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1670
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
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
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
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
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
3
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
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
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.