473,386 Members | 2,042 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,386 software developers and data experts.

About foreach statement.

Hi,

I found these doesn't work:

private void button1_Click(object sender, EventArgs e)
{
string[] tmpstr = new string[] { "david", "bill", "mike" };

foreach (string tmp in tmpstr)
{
listBox1.Items.Add(typeof(tmp));//here raise an error
}
}

It says "cannot found class or namespace 'tmp'", I think it is because the
compiler cannot ensure the variable is "null".

What can I do?

Thank you.
--
send by david zha0
May 6 '07 #1
6 2259
David zha0 <zh*****@163.comwrote:
I found these doesn't work:

private void button1_Click(object sender, EventArgs e)
{
string[] tmpstr = new string[] { "david", "bill", "mike" };

foreach (string tmp in tmpstr)
{
listBox1.Items.Add(typeof(tmp));//here raise an error
}
}

It says "cannot found class or namespace 'tmp'", I think it is because the
compiler cannot ensure the variable is "null".
No, it's because the typeof operator should have a type name in
brackets - typeof(int) for example - not an expression like a variable.

What exactly are you trying to do here anyway?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 6 '07 #2
Thank you.

Seems I've asked a foolish question, haha.
I am studying C# now, and I just want to write something to
test the reflection.
I want to write a small program with plug-in support.
Here's part of the code:

/*The variable "myPluginInfo" follow is a List<of structure PluginInfo

struct PluginInfo
{
string path;

public PluginInfo(string path)
{
this.path=path;
}

public string Path
{
get
{
return path;
}
}
}
*/

//all the code follow is in ListBox1.MouseDoubleClick event handler:

Assembly tmpPlugin =
Assembly.Load(myPluginInfo[listBox1.SelectedIndex].Path);
//go through the assembly and find the "EntryPoint" class
foreach (Type tmptype in tmpPlugin.GetTypes())
{
if (tmptype.Name == "EntryPoint")
{
//go through the class again and find our Main
method.
ConstructorInfo tmpcoutinfo =
typeof(tmptype).GetConstructor(BindingFlags.Public | BindingFlags.Static,
null, Type.EmptyTypes, null);

try
{

if (tmpcoutinfo != null)
{
tmpcoutinfo.Invoke(new object[0]);
myevent.AddEventHandler(tmptype, new
EventHandler(Plugin_Exit));
#warning Some exception handler should
be add here.
}
else
{
throw new ApplicationException("No such
method!");
}
}
catch (ApplicationException m)
{
if (m.Message == "No such method!")
{
MessageBox.Show("No such Method!");
break;
}
//and if we can't handle it, just pass it.
throw new ApplicationException("Unknown
excetpion", m);
}
}
}

And by the way, is this (I mean the Reflection) the usual way when we want
to write a program with plug-in support?

Thank you very mush for your help.

"Jon Skeet [C# MVP]" <sk***@pobox.com>
??????:MP********************@msnews.microsoft.com ...
David zha0 <zh*****@163.comwrote:
>I found these doesn't work:

private void button1_Click(object sender, EventArgs e)
{
string[] tmpstr = new string[] { "david", "bill", "mike" };

foreach (string tmp in tmpstr)
{
listBox1.Items.Add(typeof(tmp));//here raise an error
}
}

It says "cannot found class or namespace 'tmp'", I think it is because
the
compiler cannot ensure the variable is "null".

No, it's because the typeof operator should have a type name in
brackets - typeof(int) for example - not an expression like a variable.

What exactly are you trying to do here anyway?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 6 '07 #3
David zha0 <zh*****@163.comwrote:
Thank you.

Seems I've asked a foolish question, haha.
I am studying C# now, and I just want to write something to
test the reflection.
I want to write a small program with plug-in support.
Here's part of the code:

/*The variable "myPluginInfo" follow is a List<of structure PluginInfo
Any reason for using a structure here rather than a class? I can't see
that it'll make much difference, to be honest, but I tend to use a
class unless I *really* want value type semantics.

<snip>

This bit of code is fairly confusing:

if (tmpcoutinfo != null)
{
tmpcoutinfo.Invoke(new object[0]);
myevent.AddEventHandler(tmptype,
new EventHandler(Plugin_Exit));
}

Why are you creating a new instance of the plugin type, but effectively
throwing it away afterwards? In other words, why aren't you using the
return value of ConstructorInfo.Invoke?
And by the way, is this (I mean the Reflection) the usual way when we want
to write a program with plug-in support?
Yes, reflection tends to be used to create instances of plug-ins, but
those plug-ins normally then implement a common interface which is used
to communicate with them. That's a nicer way (IMO) of finding out which
types in an assembly are plugins, rather than looking for a particular
type name.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 6 '07 #4
Thanks a lot for your reply.

Any reason for using a structure here rather than a class?

Someone said that a struct is faster than a class when .NET allocate
memories to it.
And I think maybe I will need more informations about the plug-in in the
future.
So I use a struct instead of a class.
Is this right to think like this?
And these code:
if (tmpcoutinfo != null)
{
tmpcoutinfo.Invoke(new object[0]);
myevent.AddEventHandler(tmptype,
new EventHandler(Plugin_Exit));
}

I don't know how the messages thansmit between the host application and the
plug-in, so I declare a event to recive the message.

You said "That's a nicer way (IMO) of finding out which
types in an assembly are plugins, rather than looking for a particular
type name.", I don't quite understand this, what is IMO? I will be very
thankful if
you can give me its full spelling.

Thanks again for your help.

"Jon Skeet [C# MVP]" <sk***@pobox.com>
??????:MP********************@msnews.microsoft.com ...
David zha0 <zh*****@163.comwrote:
>Thank you.

Seems I've asked a foolish question, haha.
I am studying C# now, and I just want to write something to
test the reflection.
I want to write a small program with plug-in support.
Here's part of the code:

/*The variable "myPluginInfo" follow is a List<of structure PluginInfo

Any reason for using a structure here rather than a class? I can't see
that it'll make much difference, to be honest, but I tend to use a
class unless I *really* want value type semantics.

<snip>

This bit of code is fairly confusing:

if (tmpcoutinfo != null)
{
tmpcoutinfo.Invoke(new object[0]);
myevent.AddEventHandler(tmptype,
new EventHandler(Plugin_Exit));
}

Why are you creating a new instance of the plugin type, but effectively
throwing it away afterwards? In other words, why aren't you using the
return value of ConstructorInfo.Invoke?
>And by the way, is this (I mean the Reflection) the usual way when we
want
to write a program with plug-in support?

Yes, reflection tends to be used to create instances of plug-ins, but
those plug-ins normally then implement a common interface which is used
to communicate with them. That's a nicer way (IMO) of finding out which
types in an assembly are plugins, rather than looking for a particular
type name.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 6 '07 #5
David zha0 <zh*****@163.comwrote:
Thanks a lot for your reply.

Any reason for using a structure here rather than a class?

Someone said that a struct is faster than a class when .NET allocate
memories to it.
I think you should read up about the differences between structures and
classes. See http://pobox.com/~skeet/csharp/references.html
And I think maybe I will need more informations about the plug-in in the
future.
So I use a struct instead of a class.
Is this right to think like this?
Not really - those aren't good reasons to make something a structure.
And these code:
if (tmpcoutinfo != null)
{
tmpcoutinfo.Invoke(new object[0]);
myevent.AddEventHandler(tmptype,
new EventHandler(Plugin_Exit));
}

I don't know how the messages thansmit between the host application and the
plug-in, so I declare a event to recive the message.
But you haven't done anything with the object you've created by
invoking the constructor.
You said "That's a nicer way (IMO) of finding out which
types in an assembly are plugins, rather than looking for a particular
type name.", I don't quite understand this, what is IMO? I will be very
thankful if you can give me its full spelling.
IMO = in my opinion

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 6 '07 #6
Thanks very mush for all the things you've taught me. I think we have a very
nice talk today.

Thank you.
"Jon Skeet [C# MVP]" <sk***@pobox.com>
??????:MP********************@msnews.microsoft.com ...
David zha0 <zh*****@163.comwrote:
>Thanks a lot for your reply.

Any reason for using a structure here rather than a class?

Someone said that a struct is faster than a class when .NET allocate
memories to it.

I think you should read up about the differences between structures and
classes. See http://pobox.com/~skeet/csharp/references.html
>And I think maybe I will need more informations about the plug-in in the
future.
So I use a struct instead of a class.
Is this right to think like this?

Not really - those aren't good reasons to make something a structure.
>And these code:
if (tmpcoutinfo != null)
{
tmpcoutinfo.Invoke(new object[0]);
myevent.AddEventHandler(tmptype,
new EventHandler(Plugin_Exit));
}

I don't know how the messages thansmit between the host application and
the
plug-in, so I declare a event to recive the message.

But you haven't done anything with the object you've created by
invoking the constructor.
>You said "That's a nicer way (IMO) of finding out which
types in an assembly are plugins, rather than looking for a particular
type name.", I don't quite understand this, what is IMO? I will be very
thankful if you can give me its full spelling.

IMO = in my opinion

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Adam | last post by:
Hi, I am having a rather frustrating time with a script I am working on. I am looping through the elements of an array with a foreach statment and inside the foreach statement I have two if...
7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
6
by: Jax | last post by:
Custom control problem. I'm modding a textbox so that it will always have a "%" sign at the end of it. I have overrided the Text property to account for the "%" value within the textbox and have...
5
by: Brad Williams | last post by:
I'm trying to get clearer on limitations of assignment/modifications within a foreach. Why does the following gives a compilation error if MyType is a struct, but it does not if MyType is a class?...
0
by: Lasse Vågsæther Karlsen | last post by:
From the book by Jeffrey Richter: throw; will not change the origin of the exception, whereas throw ex; will change the origin of the exception to this statement. When I try the following...
9
by: Michael | last post by:
Hi all, I would like to get people's opinion about executing SQL statements in C# (or any other .NET language really). I used to create my SQL statement by building a string and replacing single...
2
by: Cerebral Believer | last post by:
Hi folks, I am creating a site in FrontPage, and want to use PHP to validate a form I have created, however I would like the return of the users input (which the user reviews to check for...
11
by: Bit Byte | last post by:
I am writing a generic container class which I want to support the 'foreach' statement. My class looks something like this: class MyClass<T: CollectionBase { //Implementation for...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
12
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.