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

Very strange behaviour, can't cast to Object!

Hi,
I'm getting a very strange behaviour while running a project I've
done.... Let's expose it: I've two projects. Both of them use a Form
to do some Gui stuff. Other threads pack up messages this way like:

public class UiMsg
{
public enum MsgType { StatusOk };

public MsgType Type;
public Object args;
}

this for the first, while the second:

public class UIData{

public enum dataType{msgSendError, netWorkStatus,
msgFromOp, okClose};

public Object content;
public dataType type;

}

And that's very similar... Then I use the usual Invoke, passing it a
delegate function and an Object Array like:

Object o = (Object)ums;
Invoke(FormCall, new Object[]{ o });

this in the first, where ums is a UiMsg, while in the second

Object []args=new Object[1];

args[0]=(Object)udata;

Invoke(uiUp,args);

.... again pretty similar... Well both compile fine, but when they run
THE FIRST casts out an ArgumentException, complaing that "Object of
type 'ServerApp.UIManager+UiMsg' cannot be converted to type
'System.Object[]'."

..... strange enough... isn't it??? And more strange is the fact that
it says +UiMsg... why it doesn't use the point?!?! What's wrong, did I
forgot checking something??? I tried lot of thing, even making the
codes identical... but no way still the same.

Plase help me solve this mistery...

Aug 24 '07 #1
4 2055
The + is just the runtime name of the nested class; ignore it...

The problem is not that it can't cast to object, but that it cant cast
to an array-of-object (object[]).

Do you perhaps have the method declared with a "params" array? If this
is the case, then although via C# you can use:

FormCall(singleValue);

behind the scenes, this is actually FormCall(new object[]
{singleValue});

i.e. the arg must be in an object-array. This is a common gotcha when
using reflection. In this case, the args (to Invoke[]) must be an
array, and the corresponding element must *itself* be an array - i.e.
object[] args = {new object[] {value}};

any use?

Marc

Aug 24 '07 #2
Gotch@ wrote:
I'm not sure to understand you at all.... Well I know it cannot cast
to an array of objects... but in a case (the second one) it works
fine, in the first it doesn't really work, even if the code is very
similar. I'm pasting the function pointed by the form delegate:

private void updateMyForm(Object []args)
There's your problem right there. And it's just what Marc said it was.

The delegate method updateMyForm (that is, the method assigned to the
delegate variable) takes an object[] parameter ("args"), but you are
passing it a UiMsg instance.

And again, to fix it you need to do just as Marc suggested. Instead of:

Invoke(FormCall, new Object[]{ o });

You need:

Invoke(FormCall, new Object[]{ new object[] { o } })

Actually, there's no need for the "o" variable, since "ums" is already
an object instance (everything inherits object). So you really could
just write:

Invoke(FormCall, new Object[]{ new object[] { ums } });

While I don't recommend creating forms from more than one thread, it's
not necessarily a problem and for sure it has nothing to do with this
issue. This is a straight-forward issue of you failing to provide the
correct type for the parameter to the method.

As for the difference from the second example you posted, well...since
you didn't post complete code, it's difficult to say for sure. But it's
pretty obvious that if it works, you are passing the correct type for
the parameter and so obviously the delegate method being called is not
exactly the same or the way in which you are calling it is not exactly
the same.

Pete
Aug 25 '07 #3
Thanks a lot, I put in the

Invoke(FormCall, new Object[]{ new object[] { o } })

and everything works fine. But now, sorry if I bore you, but I code in
C# just from 3 months and I'm curious, I'd like to understand better
the thing... Becuase the form:
Object []{ new object[]{ o }} remembered me very much like I was
doing an Object array with only one element and that element was
another Object array. I think that this comes from my C++ background,
where it would have taken just a simple (Object [] ) cast.... C#
syntax not very clear to me in these corners yet...

Aug 26 '07 #4
Gotch@ <da******@gmail.comwrote:
Thanks a lot, I put in the

Invoke(FormCall, new Object[]{ new object[] { o } })

and everything works fine. But now, sorry if I bore you, but I code in
C# just from 3 months and I'm curious, I'd like to understand better
the thing... Becuase the form:
Object []{ new object[]{ o }} remembered me very much like I was
doing an Object array with only one element and that element was
another Object array. I think that this comes from my C++ background,
where it would have taken just a simple (Object [] ) cast.... C#
syntax not very clear to me in these corners yet...
That's exactly what it's doing - it's creating an object array wrapped
in another object array. The outer object array represents *all* the
arguments to the delegate. You've got one argument, which you want to
be an object array in itself.

--
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
Aug 26 '07 #5

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

Similar topics

2
by: Alex Mizrahi | last post by:
Hello, All! i admit that it's better to ask questions connected with atl/mfc classes in special newsgroups, but seems like people there are interested more in discussing stuff like MFC GUI than...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
10
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
9
by: Karahan Celikel | last post by:
Here are three simple classes: class A { public void DoIt(B b) { DoSomething(b); } public void DoSomething(B b) {
10
by: conor.robinson | last post by:
The Problem (very basic, but strange): I have a list holding a population of objects, each object has 5 vars and appropriate funtions to get or modify the vars. When objects in the list have...
31
by: gamehack | last post by:
Hi all, I've been testing out a small function and surprisingly it does not work okay. Here's the full code listing: #include "stdlib.h" #include "stdio.h" char* escaped_byte_cstr_ref(char...
7
by: ChrisM | last post by:
I posted this last week, so apologies for re-posting but I'm still looking for a sensible answer, and I'm hoping somone new might be able to cast some light... Basically, I have a fairly...
24
by: asdf | last post by:
I got a warning from the following statement: fprintf(point_file, "CONTOUR\nCLOSE\n%d\n", curve.size()); warning: format '%d' expects type 'int', but argument 3 has type 'size_t' should I...
6
by: Andrew Ducker | last post by:
I have a class "Validator" which can be cast to a Control. The code: ValidTextBox t = (ValidTextBox)v; works just fine. However, because v doesn't descend from t, I can't use "is" or "as". I...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.