473,662 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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{msgSen dError, 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,arg s);

.... again pretty similar... Well both compile fine, but when they run
THE FIRST casts out an ArgumentExcepti on, complaing that "Object of
type 'ServerApp.UIMa nager+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 2092
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(single Value);

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(Ob ject []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.co m>
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
3068
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 C++ 8-/, so i'll better ask here too.. CStringW is a class in atl/mfc dealing with strings 8-]. the only typecast operator i found is to PCXSTR that is LPCWSTR that is simply "const unsigned short*" (wchar_t is unsigned short). wcout accepts...
6
2927
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
10
2579
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 optimization). the codec between /***********/ is the initialization code. What supprise me a lot is the code with initialization(io==1) is much faster than without initialization(io!=1). The initialization code should takes some time and it should...
9
1652
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
1248
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 identical vars (like all = 5 for var "a" and all = 10 for var "b" across all vars and objects) and i change self.mylist.change_var_a(5) to a new value, in this case var "a" in object i to 5, now all vars of
31
2865
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 byte); int main (int argc, const char * argv)
7
1439
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 complicated application which seems to me to be misbehaving. The following is an attempt to reproduce the error. There are reasons in the real application why I'm trying to do things the way they are here, so please no comments on techniques here,...
24
2300
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 change the coding to fprintf(point_file, "CONTOUR\nCLOSE\n%u\n", curve.size());
6
1295
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 did want a safe way of casting though - so I tried the following code: public static T ConvertClass<T>(Validator myValidator) where T : Control {
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7367
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.