About foreach statement.

D

David zha0

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.
 
J

Jon Skeet [C# MVP]

David zha0 said:
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?
 
D

David zha0

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 said:
David zha0 said:
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?
 
J

Jon Skeet [C# MVP]

David zha0 said:
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.
 
D

David zha0

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 said:
David zha0 said:
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.
 
J

Jon Skeet [C# MVP]

David zha0 said:
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
 
D

David zha0

Thanks very mush for all the things you've taught me. I think we have a very
nice talk today.

Thank you.


Jon Skeet said:
David zha0 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top