c_edward_b

I'm an absolute beginner, an occasional programmer with mostly non-C experience who downloaded C# Express yesterday and started trying to read lines from a text file. I think I'm confused about the "new" keyword and about what is the meaning of the stuff between the parentheses when creating an object.

The line below is from "Murach's C#" and creates a FileStream object so I can read data from a sample spectrum file. The role of "new" is confusing - does it accomplish "hiding" is it there to cause the object to appear in the heap rather than on the stack or what But other than wanting to understand "new" better, I'm pretty happy.

FileStream specfile = new FileStream(@"c:\spectra\sample.txt", FileMode.Open, FileAccess.Read));


However, Murach develops a different object in another example later on in the discussion, shown below. Now I'm really confused. The object does read files, so it works. But I think we want to create a StreamReader object, which is from a different class, so I expect a different set of arguments, but instead the only argument is the entire definition we used in the earlier example, including the "new" keyword. If I'm even right to think that "stuff between the parentheses" = "arguments", what is the meaning of this definition using as its only argument what had been an entire definition in the other example

StreamReader specinput = new StreamReader(new FileStream(@"c:\spectra\sample.txt", FileMode.Open, FileAccess.Read));

Bless your heart if you feel like helping the beginner a little!



Re: Visual C# Language "new"? Arguments when creating an object? (Help the beginner?)

Matthew Watson

"new" is how you create a new object (forget about the heap or stack, you don't need to think about that for the moment).

It goes: SomeType myVariable = new SomeType(..parameters...)

Where SomeType is the name of a type (such as FileStream or StreamReader) and ..parameters.. is a list of zero or more things used to initialise the new object.

The thing that is used to create a new object is called a "Constructor" (for obvious reasons Wink.

A type can have many Constructors, each of which takes different numbers and types of parameters.

In the case of FileStream and StreamReader: They both have several different constructors. One of the FileStream constructors happens to have exactly the same types of parameters as one of the StreamReader constructors, which you have noticed.

It is not really a coincidence, since both those types allow you to read from a file. There is overlap in the things that those types can do, but there are some differences too.





Re: Visual C# Language "new"? Arguments when creating an object? (Help the beginner?)

c_edward_b

Thanks immensely!

>One of the FileStream constructors happens to have exactly the same types of parameters...

So for example if both of these would have worked:

SomeFirstType myFirstVariable = new SomeFirstType(a,b,c);

SomeSecondType mySecondVariable = new SomeSecondType(a,b,c);

then that means this would also work instead:

SomeSecondType mySecondVariable = new SomeSecondType(new SomeFirstType(a,b,c));

Does this last line create one object or two

How does "new SomeFirstType(a,b,c)" substitute for "a,b,c"

And, I guessed maybe this would work, but it didn't, saying "a field initializer couldn't reference" something:

SomeFirstType myFirstVariable = new SomeFirstType(a,b,c);

SomeSecondType mySecondVariable = new SomeSecondType(myFirstVariable);

Thanks again!





Re: Visual C# Language "new"? Arguments when creating an object? (Help the beginner?)

jgalley

then that means this would also work instead:

SomeSecondType mySecondVariable = new SomeSecondType(new SomeFirstType(a,b,c));

This is not really true in general, though there might be some cases where it is true. It would only be true if there was a signature for the constructor of SomeSecondType that had SomeFirstType as a parameter.

Think of it this way...

==============

Building.House myHouse = new Building.House(Color.red) ; // initialize myHouse to be a "new red house"

Vehical.Car myCar = new Vehical.Car(Color.Red); // initialized myCar to be a "new red car"

Building.House myHouse = new Building.House(new Vehical.Car(Color.Red)); // initialize the variable myHouse to be a "new red car house"

==============

It would not likely be the case that a constructor for Building.House would take a parameter of type Car even though it might be the case that both Car and House have a constructor that was Color.





Re: Visual C# Language "new"? Arguments when creating an object? (Help the beginner?)

decyclone

The statement

FileStream specfile = new FileStream(@"c:\spectra\sample.txt", FileMode.Open, FileAccess.Read));

creates a variable of FileStream type (FileStream specfile, same as C), and the part after = creates a new object of FileStream type to store in the variable just created.

When creating an object using 'new' keyword, the arguments passed in the <TypeName>(..Args..) depends on the constructors the type (type means class here) supports.

You can study constructors here, here, and here.

MSDN reference is here, here and here.

The line

StreamReader specinput = new StreamReader(new FileStream(@"c:\spectra\sample.txt", FileMode.Open, FileAccess.Read));

is just a short form of

FileStream specfile = new FileStream(@"c:\spectra\sample.txt", FileMode.Open, FileAccess.Read));
StreamReader specinput = new StreamReader(specfile);

it's just saving from a line of code. Programmers love it and it's pretty common technique used.

Just read the references and enjoy C# programming.