|
Every different class/struct/whatever returns a unique Type class with the GetType() function, right?
Even when the object is cast into Object, it still returns the previous one, right?
Now how do I see if that type is derived from some other thing?
For example, if I have Object o and I need to see if it derives from ISomething interface.
If System.Type is not for this and does soemthing completely different, what way could I do this?
|
|
|
|
|
To test if an object implements an interface or derives from a type:
if( theObject is ISomething )<br />
{<br />
<br />
}
or
ISomething something = theObject as ISomething;
In the latter something will set to null if theObject doesn't support the interface
Andres Manggini.
Buenos Aires - Argentina.
|
|
|
|
|
Ah, thank you.
But how would I take the y in x is y from a variable?
I mean a type that the variable stores.
Something like this:
??? aaa = ???System.Exception // Checking against System.Exception for example
if (blah is ???aaa)
|
|
|
|
|
As Andres pointed out you can use the is/as statements to determine whether an object can be cast to the specified type name. In this case you use the name of the type, not the Type object representing that type. So for this to work you have to be able to reference the typename at compile time.
If the types are loaded at runtime then you can't use the above methods, instead if you have two Type objects, one from each of the two variables then you can use something like this.
bool IsBDerivedFromA(object a, object b)
{
Type ta = a.GetType();
Type tb = b.GetType();
return tb.IsSubclassOf(a);
}
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
I find IsAssignableFrom() better for most cases as it follows natural logic.
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
leppie wrote:
IsAssignableFrom() better for most cases as it follows natural logic
Yep, I like that one better. I suggested IsSubclassOf because that was the only method I could remember while my MSDN was being laggy.
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
Thank you
|
|
|
|
|
Im so embarressed to ask this, but I've written a c# app which uses windows forms. Well when I first wrote the dlls I used the System.Console for debugging, now I can't get rid of the damn thing. Ive taken out all references to the Console class, how do I lose the System.Console window?
Thanks for tolerating a stupid question,
Ryan
|
|
|
|
|
Assuming you are building with Visual Studio.NET, right click over your project in the solution explorer window and select properties. When the dialog comes up just change the "Output type" to Windows Application.
|
|
|
|
|
Thank you so much, I knew I had to be spacing out!
Ryan
|
|
|
|
|
Is there any function equivalent to the VB function CType (it converts an expression into a type) in C#?
- monrobot13
|
|
|
|
|
Have you tried using the static class with Convert.ChangeType?
|
|
|
|
|
If all you want to do is convert say an object of type "A" in to an object of type "B" (assuming the conversion is valid), then you can just cast like this:
A a1 = new A();
B b1 = (A)a1;
|
|
|
|
|
|
Hi there,
Your problem seems very simple and hence many quick answers are there. But I would elaborate more on ur requirement.
Changing an Expression to a type could be of 2 types...
1. "1" as a String to 1 as an Integer(int)
2. "a" as a String to 97 as an integer(int).
3. obj as an Object to a Specific type like "Customer"
You need to find out ur requirement and accordingly use the Solutions given to u.
Answer 1.: Use Convert.ToInt32 or Int32.Parse functions.
Answer 2.: Use a typecast i.e. (int)num1
Answer 3.: Use a typeCast i.e. (Customer)obj or Use Convert.ChangeType function.
I hope this helps.
regards
Atul
p.s.: Convert.ChangeType could be the sole solution sometimes.
Atul Kale
MCSD, MCT
Sr. Software Engineer
XcelVision Technologies Ltd.
|
|
|
|
|
Thank you for all the replies, but unfortunately none are really what I can use. Here is basically what I'm trying to do. I take two variables and divide them, then I multiply by another variable. The division usually yeilds a number below zero so multiplying still gives me zero. Now with the CType function you can convert the expression to an int and it will round the number up to one instead of zero. Like this for example:
[Visual Basic]
var3 = CType ((var1/var2), Integer)
The variables mentioned above are all ints and changing their type is not an option. I guess what I really need is something that will round an expression to the nearest whole number.
- monrobot13
|
|
|
|
|
I was just wondering, the solution to ur problem is so Basic, in fact u must understand here, that u did not phrase ur question properly! U were just asking for ways to Convert the data.
Its a simple logic that while doing such operations, U never store the intermediate results in variables.
int num1 = 6, num2 = 10, num3 = 5, result;
here if u want,
(num1 / num2) * num3 which yields (6/10) * 5 = 0.6 * 5
Then boss, you should not store num1/num2 in an int variable. That is ur problem.
Do the Direct calculation,
result = (num1 / num2) * num3;
or,
use an intermediate double or float variable it u still wanna store the division.
double div = num1/ num2;
result = div * num3;
Sometimes one should just concenrate on the basics first and not just jump into complexities.
Atul Kale
MCSD, MCT
Sr. Software Engineer
XcelVision Technologies Ltd.
|
|
|
|
|
I want to design a toolbar and toolbox like Visual Studio .NET (e.g C#) ... I want to design some shape as Rectangle,Circle, Arc ... but each ones can be resized automatic at run time ...
Thanks
|
|
|
|
|
What you are asking for requires some fairly in-depth study on your part. The reason you're not getting an answer for this is because it's not something that can be summed up in a few words or lines of code. You might as well have asked "can anyone write my app for me." It doesn't work like that. You need to spend some time learning first and then come up with some specific questions. Do a google search for 'C# Drawing'. This search yielded this article 'System.Drawing with C#' from O'Reilly net among many others.
Best Regards.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
For C# I was trying to make this array use a charecter instead of zeros. I set it up like this in a console program and it works but not in a Windows Form Program. Only the numbers 1 through 9 work.
Map [i,j] = '#';
Also, what are the single quotes used for?
|
|
|
|
|
Hi guys,
I have created a Web Service called BugReporterService and hosted on a machine let's say MAC1 with a Live IP address 203.124.147.42
I have a windows client created in C# which accesses this Web Service and calls its method through a Proxy class which is generated via the Visual Studio.NET IDE.
Now, my problem is that, when this client application runs on a machine that can access Internet via Proxy Server only, It cannot access the Web Service class. If the access is without the HTTP Proxy Server to the internet, then it works fine.
How can I specify programmatically the IP Address/Name of the HTTP Proxy server (say 192.168.0.11) and its username and password. I initially thought that the application would take these settings from the Internet Explorer as lots of other applications do. But it's not wkg that way.
plz get back to me whoever finds out the Answer! I'll be grateful!
Thanx!
Atul Kale
MCSD, MCT
Sr. Software Engineer
XcelVision Technologies Ltd.
|
|
|
|
|
If you use the HttpWebRequest class under the Proxy property you can find Credentials.GetCredential(..params..).Password or .UserName
|
|
|
|
|
Hi there,
In fact I was asking abt the Web Proxy Implementation in the Proxy class generated for WebService. HttpWebRequest is not coing into picture!
Still, I got a hint from ur mail and I checked out a property called Proxy directly for the SoapHttpClientProtocol class.
Thanx a lot anyways!
Atul Kale
MCSD, MCT
Sr. Software Engineer
XcelVision Technologies Ltd.
|
|
|
|
|
I am wondering, what is the point of implementing IDisposable?
Why not just do whatever is needed in the destructor?
|
|
|
|
|
C# dosen't have a delete operator so, we don't have destructors in the C++ sense. You have to override the finalize method which looks strangely like a C++ destructor
But, in order to exercise a more percise destructor like control .net wants us to implement the Dispose pattern which can be easily done through IDisposable. Another bennifit is that much of .net uses this pattern; also a client app using your class class will be able test for this interface using the "as" or "is" operators..making it a tad more reuasable.
Hey don't worry, I can handle it. I took something. I can see things no one else can see. Why are you dressed like that?
- Jack Burton
|
|
|
|