Click here to Skip to main content
15,927,060 members
Home / Discussions / C#
   

C#

 
GeneralRe: Icon in Toolbox Pin
Nick Parker20-Aug-04 8:02
protectorNick Parker20-Aug-04 8:02 
GeneralProblem verifying a signed XML document Pin
pelos20-Aug-04 0:06
pelos20-Aug-04 0:06 
GeneralGenerating Product Key... Pin
IrishSonic19-Aug-04 22:54
IrishSonic19-Aug-04 22:54 
GeneralRe: Generating Product Key... Pin
Aaron Eldreth20-Aug-04 3:31
Aaron Eldreth20-Aug-04 3:31 
GeneralRe: Generating Product Key... Pin
Brian Delahunty20-Aug-04 5:32
Brian Delahunty20-Aug-04 5:32 
GeneralRe: Generating Product Key... Pin
Anonymous21-Aug-04 15:31
Anonymous21-Aug-04 15:31 
QuestionHow to customize SaveFileDialog (?_?) Pin
sachinkalse19-Aug-04 22:09
sachinkalse19-Aug-04 22:09 
GeneralPosition in a StreamReader Pin
jochenz19-Aug-04 21:42
jochenz19-Aug-04 21:42 
Hi all!

I'm trying to write a method that reads an object. If the object can't be read, the stream should return to it's original state. This seems to be easily achievable by getting the stream's position before attempting to read and to set is back to the same position if reading would fail. However, this does not work out:

public void ReadFromStream(StreamReader stream) 
{
	// before attempting to read: save current stream position
	long safePos = stream.BaseStream.Position;

	// read first line
	string line = stream.ReadLine();

	// extract keyword and name
	string[] keyAndName = line.Split(new char[]{' '});

	// if keyword invalid
	if(keyAndName[0].Trim() != keyWord) 
	{
		// move stream back to original position
		stream.BaseStream.Position = safePos;

		// a debug check to see whether it actually moves back to the original position
		// NO IT DOESN'T: the message shown is the line following 'line' and is not 'line'
		System.Windows.Forms.MessageBox.Show(stream.ReadLine());

		// invalid keyword --> throw exception and stream should be left in same state as when this method was entered
		throw new Exception();
	}

// further reading of the object...
}


Should I return the stream C++-style? I wouldn't think so since a stream is a reference type in C#...

I don't have a clue why this fails. FYI: the value of safePos and stream.BaseStream.Position always remain the same, in my case 16384.

Thanks for any help!

Jochen
GeneralRe: Position in a StreamReader Pin
Werdna20-Aug-04 13:18
Werdna20-Aug-04 13:18 
GeneralRe: Position in a StreamReader Pin
jochenz20-Aug-04 22:07
jochenz20-Aug-04 22:07 
GeneralEnterprise level database operatibility Pin
Salil Khedkar19-Aug-04 21:35
Salil Khedkar19-Aug-04 21:35 
QuestionWhat is the code that I can open the CD driver by? Pin
mhmo19-Aug-04 19:59
mhmo19-Aug-04 19:59 
AnswerRe: What is the code that I can open the CD driver by? Pin
Stefan Troschuetz20-Aug-04 2:50
Stefan Troschuetz20-Aug-04 2:50 
Generalfine tune form's paint event handler Pin
CNU19-Aug-04 19:21
CNU19-Aug-04 19:21 
GeneralRe: fine tune form's paint event handler Pin
sreejith ss nair19-Aug-04 19:59
sreejith ss nair19-Aug-04 19:59 
GeneralRe: fine tune form's paint event handler Pin
CNU19-Aug-04 20:30
CNU19-Aug-04 20:30 
GeneralRe: fine tune form's paint event handler Pin
sreejith ss nair19-Aug-04 21:24
sreejith ss nair19-Aug-04 21:24 
GeneralSplit me Pin
sreejith ss nair19-Aug-04 19:00
sreejith ss nair19-Aug-04 19:00 
GeneralC# MDB Connect Pin
jzb19-Aug-04 18:32
jzb19-Aug-04 18:32 
GeneralRe: C# MDB Connect Pin
Nemanja Trifunovic20-Aug-04 3:35
Nemanja Trifunovic20-Aug-04 3:35 
GeneralASP timeout Pin
jzb19-Aug-04 16:07
jzb19-Aug-04 16:07 
Generalhiding MDI Child forms Pin
Member 128471219-Aug-04 15:56
Member 128471219-Aug-04 15:56 
GeneralRe: hiding MDI Child forms Pin
LongRange.Shooter20-Aug-04 3:08
LongRange.Shooter20-Aug-04 3:08 
GeneralASP.NET and Graphics .. Pin
OMalleyW19-Aug-04 13:50
OMalleyW19-Aug-04 13:50 
GeneralRe: ASP.NET and Graphics .. Pin
Christian Graus19-Aug-04 13:56
protectorChristian Graus19-Aug-04 13:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.