|
Your object needs to expose properties that represent the collection of students and teachers, so you can do
foreach(student s in mySchool.Students)
You can't vary your enumerator by return type, you can't do that for any method.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
You cant do that, but you could provide two properties, say Students and Teachers,
that return a collection of students and teachers respectively.
Then you can do foreach (Student s in school.Students) {...}
Alternatively you can have both Student and Teacher inherit from the same base class
(say Person) and do:
foreach (Person p in school) {
Teacher t=p as Teacher;
if (t!=null) { do teacher stuff }
}
and similar for students.
Luc Pattyn
|
|
|
|
|
thanks
but i things it has a solation.i have two class named teacher and student in my school collection.can i override equle method of each class to determine in foreach.or can i used generic collections
|
|
|
|
|
Hi,
foreach insists on matching types, all elements your enumerator offers must match the
type you mention inside foreach (or be derived from it), hence my Person suggestion
(although object would do too !).
I am not absolutely sure about generics, but I expect exactly the same:
in a teacher collection you can put teachers;
in a persons collection you can put teachers and students, but enumerating them would
result in persons, hence teachers as well as students.
If you insist on having different types (Student and Teacher) in a single collection,
then I strongly suggest you use the "as" statements I showed before. It works
very well for me.
Luc Pattyn
|
|
|
|
|
hi.
Thanks for your suggestion.I think your solation is the best.
|
|
|
|
|
Hmm, I don't like this use of foreach at all. Why? I don't see school as collection of students and teachers - Students and teachers are in school.
You can have properties of type StudentCollection and TeacherCollection . Then do foreach(Student s in school.Students) .
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
|
|
|
|
|
Yes, that's the best way, look at DataTable, there are .Columns and .Rows, each can be enumerated.
|
|
|
|
|
I am using Bitmap imgBold = Bitmap.FromFile("bold.bmp"); to retrieve a bitmap file, but when I try to compile the program, it says that it can't directly convert an Image object into a Bitmap object. It appears as though the Bitmap.FromFile method returns an Image object... Is there a method of the Image class that will allow me to convert the Image into a Bitmap ? The reason I need it to be a bitmap is that I need to set the transparency color. You can't do this with an Image object.
-----
Note: *** Never give me an answer have anything to do with Visual Studio. I don't have this program, and it'll be that way for a long, long time. ***
|
|
|
|
|
Why don't you simply cast the Image to Bitmap?
Bitmap imgBold = (Bitmap)Bitmap.FromFile("bold.bmp");
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Try this:
Bitmap imgBold = (Bitmap) Bitmap.FromFile("bold.bmp");
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi Max,
you can use a stream instead of FromFile, just open a file stream and do:
Bitmap imgBold=new Bitmap(stream);
Luc Pattyn
|
|
|
|
|
You should never do a direct cast. Do this instead
Bitmap bm = Bitmap.FromFile("Blah") as Bitmap;
the 'as' operator will return null if the object is not a valid target for the cast. A cast will just blow up.
In this case, you know it will work, but it's just neater to get into the habit of doing it this way.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Christian Graus wrote: You should never do a direct cast. Do this instead
Bitmap bm = Bitmap.FromFile("Blah") as Bitmap;
the 'as' operator will return null if the object is not a valid target for the cast. A cast will just blow up.
Well, it's not always the best idea to postpone the error message as long as possible.
The code is more maintainable if an error occurs as close to the source as possible. If a cast doesn't work, it's easier to find the cause if the error message comes at the cast, than if it comes somewhere later in the code because of a reference mysteriously turning null.
Using as only serves a purpose if you actually check for null afterwards.
Also, in this case if the cast fails, you will lose the reference to the original object, which means that you won't be able to dispose it.
---
single minded; short sighted; long gone;
|
|
|
|
|
Guffa wrote: Using as only serves a purpose if you actually check for null afterwards.
Obviously, yes.
Guffa wrote: Also, in this case if the cast fails, you will lose the reference to the original object, which means that you won't be able to dispose it.
Oh, good point...
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Guffa wrote: Also, in this case if the cast fails, you will lose the reference to the original object, which means that you won't be able to dispose it.
Wouldn't I also loose the reference to the original object if the cast succeeds and therefore dispose it before doing this anyway.?
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Stefan Troschütz wrote: Wouldn't I also loose the reference to the original object if the cast succeeds
No. The type of the reference variable doesn't matter. A reference is a reference. If you want a reference to the object as the original, you can just cast the reference to the actual type.
---
single minded; short sighted; long gone;
|
|
|
|
|
Ah, I think I misunderstood your post. I thought with original object you were refering to the object that was referenced by the Bitmap variable before doing Bitmap bm = Bitmap.FromFile("Blah") as Bitmap but of course that is nonsense as the variable is declared in this line. So with original object you mean in this special case the Image object returned by FromFile , right?
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Stefan Troschütz wrote: So with original object you mean in this special case the Image object returned by FromFile, right?
Right.
---
single minded; short sighted; long gone;
|
|
|
|
|
becarfule that Bitmap class is sealed.so u cant create an object.just u should use Image class.
|
|
|
|
|
You are totally wrong and completley confused on what the OP is doing.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
You are totally wrong and completley confused on what the OP is doing.
what's wrong with me? Bitmap is a sealed class.can explain u me more?
|
|
|
|
|
sealed does not mean that you cant create an instance, of course you can do
"new Bitmap(...)". It does mean you cannot create a class that inherits from it.
Luc Pattyn
|
|
|
|
|
hi.
yes.Now i understand what a big mistake that i had make!
|
|
|
|
|
Hello,
Can I write a c++ code on my c# project?
I mean if I have an array (a) on c# and I want to make something with this array but on c++ code how can I do that?
I mean How can I mix the two languages togather?
SnaidiS(Semion)
|
|
|
|
|
Semion_N wrote: Can I write a c++ code on my c# project
No. A project can only be written in a single language, be it C++, C#, VB.NET, ... But, there's nothing stopping you from having multiple projects in your Solution, each written in a seperate language.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|