use local variables in a switch construction 
Author Message
 use local variables in a switch construction

hello,

in the following situation the compiler says "use of an unasigned local
variable"
but I need the file to be created in the switch constructor,not before!
what can I do?

FileStream FStream;

switch (Command)
{
    case "NEW FILE":
        FStream = new FileStream(FileName,FileMode.Create);
        break;
    case "CLOSE FILE":
        FStream.Close();        //here comes the Compiler error!!
        break;

Quote:
}



Mon, 14 Feb 2005 19:24:52 GMT  
 use local variables in a switch construction
If you are really certain that "CLOSE FILE" will always be called after "NEW
FILE" you can do this:

FileStream FStream = null;

However, be aware that if for some reason your code should switch into
"CLOSE FILE" before "NEW FILE" you will get a a NullReferenceException.

Best Regards,

John


Quote:
> hello,

> in the following situation the compiler says "use of an unasigned local
> variable"
> but I need the file to be created in the switch constructor,not before!
> what can I do?

> FileStream FStream;

> switch (Command)
> {
>     case "NEW FILE":
>         FStream = new FileStream(FileName,FileMode.Create);
>         break;
>     case "CLOSE FILE":
>         FStream.Close();        //here comes the Compiler error!!
>         break;
> }



Mon, 14 Feb 2005 19:35:07 GMT  
 use local variables in a switch construction
try...

FileStream FStream = null;

switch (Command)
{
    case "NEW FILE":

        System.Diagnostics.Debug.Assert( FStream == null, "Can't open the
file as we already have one open" );
        FStream = new FileStream(FileName,FileMode.Create);
        break;
    case "CLOSE FILE":
        System.Diagnostics.Debug.Assert( FStream != null, "Can't close file
that hasn't been opened" );
        FStream.Close();        //here comes the Compiler error!!
        FStream = null;
        break;

Quote:
}

I took the liberty of adding some asserts as well although you don't need
them to get around the compiler problem. These might be better as throws if
the client caller is external to your module.


Quote:
> hello,

> in the following situation the compiler says "use of an unasigned local
> variable"
> but I need the file to be created in the switch constructor,not before!
> what can I do?

> FileStream FStream;

> switch (Command)
> {
>     case "NEW FILE":
>         FStream = new FileStream(FileName,FileMode.Create);
>         break;
>     case "CLOSE FILE":
>         FStream.Close();        //here comes the Compiler error!!
>         break;
> }



Mon, 14 Feb 2005 19:36:28 GMT  
 use local variables in a switch construction
It's trying to protect you from yourself - I can't see your whole program,
but it's possible for Command to be "CLOSE FILE" without FStream being set.
If you KNOW this will never happen, you can get rid of the compiler message
by initializing FStream to null...

ie:

FileStream FStream = null;

at the top - but make sure your logic is right, or you'll get a runtime
exception trying to close a null stream ...


Quote:
> hello,

> in the following situation the compiler says "use of an unasigned local
> variable"
> but I need the file to be created in the switch constructor,not before!
> what can I do?

> FileStream FStream;

> switch (Command)
> {
>     case "NEW FILE":
>         FStream = new FileStream(FileName,FileMode.Create);
>         break;
>     case "CLOSE FILE":
>         FStream.Close();        //here comes the Compiler error!!
>         break;
> }



Mon, 14 Feb 2005 19:40:29 GMT  
 use local variables in a switch construction

Quote:
> FileStream FStream;

if you are really SURE that commands will not occur in wrong sequence, just
declare

FileStream FStream = null;

in general, compiler is right. such constructions are not quite safe,
because they strongly depend on application's logic.

Regards
Wiktor Zychla



Mon, 14 Feb 2005 19:41:06 GMT  
 use local variables in a switch construction
Thanks all,

I'm quite sure that this will not happen,
because the commands are sent from a network client, that I have written.
So the client knows the protocol, and don't send the Close command before
the New File command.

thanks



Quote:
> > FileStream FStream;

> if you are really SURE that commands will not occur in wrong sequence,
just
> declare

> FileStream FStream = null;

> in general, compiler is right. such constructions are not quite safe,
> because they strongly depend on application's logic.

> Regards
> Wiktor Zychla



Mon, 14 Feb 2005 19:46:11 GMT  
 use local variables in a switch construction

Quote:

> in the following situation the compiler says "use of an unasigned local
> variable"
> but I need the file to be created in the switch constructor,not before!
> what can I do?

> FileStream FStream;

> switch (Command)
> {
>     case "NEW FILE":
>         FStream = new FileStream(FileName,FileMode.Create);
>         break;
>     case "CLOSE FILE":
>         FStream.Close();        //here comes the Compiler error!!
>         break;
> }

Consider if the Command is "CLOSE FILE" - which file stream would you
want to be closed? FStream hasn't necessarily been assigned a value. You
could set it to null to start with, and you'll get an exception if you
try to close it without opening it first.

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Mon, 14 Feb 2005 20:14:41 GMT  
 use local variables in a switch construction

Quote:

> I'm quite sure that this will not happen,
> because the commands are sent from a network client, that I have written.
> So the client knows the protocol, and don't send the Close command before
> the New File command.

That's assuming that only your client connects to it, of course, and not
some other client which has been specifically written to crash your
server...

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Mon, 14 Feb 2005 20:19:07 GMT  
 use local variables in a switch construction
It leaves you open to a denial of service attack - if it matters...


Quote:
> Thanks all,

> I'm quite sure that this will not happen,
> because the commands are sent from a network client, that I have written.
> So the client knows the protocol, and don't send the Close command before
> the New File command.

> thanks



> > > FileStream FStream;

> > if you are really SURE that commands will not occur in wrong sequence,
> just
> > declare

> > FileStream FStream = null;

> > in general, compiler is right. such constructions are not quite safe,
> > because they strongly depend on application's logic.

> > Regards
> > Wiktor Zychla



Mon, 14 Feb 2005 20:29:15 GMT  
 use local variables in a switch construction
BTW - Back doors written in C# - whatever will they think of next... :)


Quote:
> Thanks all,

> I'm quite sure that this will not happen,
> because the commands are sent from a network client, that I have written.
> So the client knows the protocol, and don't send the Close command before
> the New File command.

> thanks



> > > FileStream FStream;

> > if you are really SURE that commands will not occur in wrong sequence,
> just
> > declare

> > FileStream FStream = null;

> > in general, compiler is right. such constructions are not quite safe,
> > because they strongly depend on application's logic.

> > Regards
> > Wiktor Zychla



Mon, 14 Feb 2005 20:32:46 GMT  
 use local variables in a switch construction
Jon & Jermey..... you don't know any of that... since you can't see all his
code. You have no idea what processing he is doing to the request before
reaching this snippet.


Quote:
> It leaves you open to a denial of service attack - if it matters...



> > Thanks all,

> > I'm quite sure that this will not happen,
> > because the commands are sent from a network client, that I have
written.
> > So the client knows the protocol, and don't send the Close command
before
> > the New File command.

> > thanks



> > > > FileStream FStream;

> > > if you are really SURE that commands will not occur in wrong sequence,
> > just
> > > declare

> > > FileStream FStream = null;

> > > in general, compiler is right. such constructions are not quite safe,
> > > because they strongly depend on application's logic.

> > > Regards
> > > Wiktor Zychla



Mon, 14 Feb 2005 20:32:45 GMT  
 use local variables in a switch construction

Quote:
> Jon & Jermey..... you don't know any of that... since you can't see all
his
> code. You have no idea what processing he is doing to the request before
> reaching this snippet.

Yes we do - Freak-2k said it himself... He's relying on the fact that...

Quote:
> > > ...the client knows the protocol, and don't send the Close command
before
> > > the New File command.



Mon, 14 Feb 2005 20:52:01 GMT  
 use local variables in a switch construction
Culpa Mea, my fault entirely... I apologize... I lost track of this thread a
loooong time ago, I think.... getting way to long....
What I find particulary interesting that, after my original response, 8
people posted the same thing... :)

Cheers,

John


Quote:
> > Jon & Jermey..... you don't know any of that... since you can't see all
> his
> > code. You have no idea what processing he is doing to the request before
> > reaching this snippet.

> Yes we do - Freak-2k said it himself... He's relying on the fact that...

> > > > ...the client knows the protocol, and don't send the Close command
> before
> > > > the New File command.



Mon, 14 Feb 2005 20:54:37 GMT  
 use local variables in a switch construction
We'll just call you "QuickDraw" from now :)  I assume the others, like me,
posted their answers before yours filtered through...


Quote:
> Culpa Mea, my fault entirely... I apologize... I lost track of this thread
a
> loooong time ago, I think.... getting way to long....
> What I find particulary interesting that, after my original response, 8
> people posted the same thing... :)

> Cheers,

> John



> > > Jon & Jermey..... you don't know any of that... since you can't see
all
> > his
> > > code. You have no idea what processing he is doing to the request
before
> > > reaching this snippet.

> > Yes we do - Freak-2k said it himself... He's relying on the fact that...

> > > > > ...the client knows the protocol, and don't send the Close command
> > before
> > > > > the New File command.



Mon, 14 Feb 2005 21:11:04 GMT  
 use local variables in a switch construction
Jeremy, didn't mean to be obnoxious about it, of course the more answers the
better... and the length of this thread is great!
Regarding "QuickDraw"... I'd prefer if you called me "BitBlt".... no wait,
BitBlt is not called that in .NET.... but "DrawImage" sounds hookey ( god, I
am pathetic I know).

Sorry about all this... I'll go drown myself in the toilet now...

Forever yours,

John


Quote:
> We'll just call you "QuickDraw" from now :)  I assume the others, like me,
> posted their answers before yours filtered through...



> > Culpa Mea, my fault entirely... I apologize... I lost track of this
thread
> a
> > loooong time ago, I think.... getting way to long....
> > What I find particulary interesting that, after my original response, 8
> > people posted the same thing... :)

> > Cheers,

> > John



> > > > Jon & Jermey..... you don't know any of that... since you can't see
> all
> > > his
> > > > code. You have no idea what processing he is doing to the request
> before
> > > > reaching this snippet.

> > > Yes we do - Freak-2k said it himself... He's relying on the fact
that...

> > > > > > ...the client knows the protocol, and don't send the Close
command
> > > before
> > > > > > the New File command.



Mon, 14 Feb 2005 21:41:49 GMT  
 
 [ 21 post ]  Go to page: [1] [2]

 Relevant Pages 

1. How to convert local time to gmt using a local variable time zone per process/thread

2. switch local variables?

3. Construction of local static objects in a multithreaded environment

4. Convert local time to gmt time without using TimeZone Env variable

5. using the switch statement with string variables

6. static local variables vs global variables

7. About local variables and variable size???

8. construction/deletion of global variables in a com component

9. Mounting a local directory as a local drive using the win32sdk or the mfcs

10. Unassigned local variable- newbe question

11. Static VS Local Class Level variables and Methods

12. Use of unassigned local variable

 

 
Powered by phpBB® Forum Software