|
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
|
|
|
|
|
|
I want to show time in my applicattion based on different area time zone.When user select different area it shows the time on that area.Is there any way I can get the time based on time zone?
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
I'm assuming the user will select the time zone from a dropdown or similar control. In that case you should have the time zone offset for that area, you could just add or subtract the hours from the current system time.
|
|
|
|
|
Have you looked at the System.TimeZone class? It looks like the ToLocalTime() and GetUtcOffset() methods should be close to what you need.
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
|
|
|
|
|
I'm using a background thread in my Windows Forms app to retrieve all user objects in our Active Directory. (15,000+ users) I am retrieving users in lots of 1,000, at which time I call the Suspend() method on my thread to pause the GUI and allow them to select a user or users.
If the user presses the Cancel button while the thread is in the running state, I call the Abort() method and catch the corresponding ThreadAbortException, and the thread terminates gracefully.
The problem I'm having is that if I call the Abort() method while the thread is in a suspended state, the thread never terminates. I am catching the ThreadStateException that is thrown, but is there something else I must do to the thread to properly terminate it at this point? Code follows:
private void buttonMemberSelectCancel_Click(object sender, System.EventArgs e)
{
try
{
if (!bgListThread.IsAlive) // is alive?
this.Close();
else
{
try
{
bgListThread.Abort();
}
catch(System.Threading.ThreadAbortException)
{
// abort thread gracefully
}
catch(System.Threading.ThreadStateException)
{
// if suspended, abort gracefully
}
}
}
catch (NullReferenceException)
{
// if thread doesn't exist, just close dialog
}
finally
{
this.Close();
}
}
|
|
|
|
|
In my unscientific tests, I noticed that the Thread.Abort() method cannot cancel an interop call, only managed code.
It's not the fall that kills you: it's the sudden stop - Down by Law, Jim Jamursch (1986)
|
|
|
|
|
Daniel Turini wrote:
I noticed that the Thread.Abort() method cannot cancel an interop call, only managed code.
Huh? I'm not making any Interop calls in this function at all.
|
|
|
|
|
It's a gruesome suggestion, but just as a test, you might try resuming the thread and then aborting it.
|
|
|
|
|
Maybe, but according to the MSDN docs, the CLR already does that for you when you call Abort.
|
|
|
|
|
Marc Merritt wrote:
according to the MSDN docs, the CLR already does that for you when you call Abort.
I don't know if that has always been in the docs, but it certainly isn't what happens under .NET v1.0; in v1.1 it has been corrected and works correctly.
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|