|
Hi All,
I am trying to dynamically create object based on values that being submited from a form, but keep on getting a "No parameterless constructor defined for this object." error.
<br />
Type paramType = Type.GetType("System." + Request["type"]);
String paramValue = Request["val"];
<br />
if (paramType != null)<br />
{<br />
Activator.CreateInstance(paramType);<br />
}<br />
|
|
|
|
|
What are the types that you are trying to create, and do they all have public, parameterless constructors?
-Phil
|
|
|
|
|
Hi Phil,
Right now I am just trying to create strings and ints, but will have to create other types of objects. All will be public but some will be parameterless constructors some will not.
Thanks,
tom
|
|
|
|
|
The System.String type does not have a parameterless constructor (they all require at least one parameter), and therefore cannot be instantiated via the Activator.CreateInstance(Type) method.
-Phil
|
|
|
|
|
thanks Phil,
Would you mind showing me a quick example of how to create a string via reflection. This is what I tried using before but it allways returns null.
<br />
Type[] ArgTypes = new Type[1] {Type.GetType("System.String") };<br />
ConstructorInfo oConstructorInfo = paramType.GetConstructor(ArgTypes );<br />
thanks
|
|
|
|
|
Here's one way to create the string "a" via reflection:
<br />
Type type = Type.GetType("System.String");<br />
<br />
object obj = Activator.CreateInstance(type, 'a', 1);<br />
In this case, the .NET runtime would use the System.String.String(char, int) constructor in order to create the instance because we passed a character and an integer to Activator.CreateInstance().
In the case of the .NET native types like strings and integers, however, if you already know what the value of the object will be, using reflection is overkill. There are far easier ways to translate between one native type and another.
-Phil
|
|
|
|
|
You could Create a class called MyString
add a parameterless constructor and an implicit cast
public static implicit operator string (MyString A)
{
return A._internalStringVariable;
}
public static implicit operator MyString (String S)
{
return new MyString(S);
}
Then you'll have an object that behaves a bit like a string but has a parameterless constructor.
HTH
Russ
|
|
|
|
|
Hi,
As part of the application I am working on, I would like to allow the application to dial up to the closest regional office. Our company has servers in every major city in the US so basically the only thing that I need to change is the phone number of the dial-up connection that the application is using.
I have managed to connect/disconnect using an unmanaged c++ code that is based on WinINet(InternetDial, dll or using RASDial. The problem with RasDial is that it's not reliable (At least from my experience...) and the problem with the c++ function that I am using is that it doesn't let me specify the phone number...
At this point, the only option that might be viable is to use RasDialDlg. Has anyone ever tried to use this function with c#? If yes, do you have any code samples or tips you can share with me?
Regards,
Joe
|
|
|
|
|
Hi,
I am trying to develop a webservice which will scan a given directory on the server and expose the result. I want to call this webservice from a WinForm application to populate a treeview.
Do you have any suggestions on the way I should do that ?
Thanks,
Guillaume
|
|
|
|
|
Hi,
Not sure what part you need help with, but the easiest way to consume a webserivice is build a proxy class in vs.net. Just right click on the name of your project in the soloutions explorer and the select add webreference. Then enter the url to the WSDL of your webservice and click on the add web reference button. You can then call the methods of your webservice by doiing th following.
webreferenceNameyoucreatedabove.methodName();
Regards,
tom
|
|
|
|
|
Hello all,
Hopefully someone can help me solve this problem. I'm trying to print a PrintDocument, but I get inconsistent results. Sometimes it works, and sometimes I get "The handle is invalid" or "Incorrect parameter" exceptions. I stripped it down to the following code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
namespace PrintTest
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
PrintDocument pd = new PrintDocument();
PageSetupDialog psd = new PageSetupDialog();
psd.Document = pd;
if (psd.ShowDialog() == DialogResult.OK)
{
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
try
{
ppd.ShowDialog();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Exception");
ppd.Close();
}
}
}
}
}
Thanks in advance!
|
|
|
|
|
I havent had any printing problems, except for the fact that some details are strange at least (where and when to set properties in order to choose color, landscape, duplex...).
But I strongly suggest you throughly process any exception; rather than looking at
exc.Message, you really should look at the multi-line exc.ToString()
it gives you, depending on the kind of exception, much more information, and also
a stack trace, so you can tell in exactly what line things fail.
Good luck !
Luc Pattyn
|
|
|
|
|
try to comment pagesetupdialog object and just try working around with objects printpreviewdialog and printdocument
printPriewdialog ppd = new printpreviewdialog();
ppd.document=pd;
ppd.showDialog();
|
|
|
|
|
Thanks for the reply! If I don't use a PageSetupDialog the problem persists when I actually try to print. Just like before sometimes the document prints and sometimes I get exceptions.
TIA
|
|
|
|
|
I'm still getting exceptions, which actually come from the ShowDialog command. The document is rendered correctly, but if I try to print from the print preview dialog I get an exception.
Thanks in advance for any help on the subject!
|
|
|
|
|
For what it's worth, I think this is a bug somewhere in Microsoft's code. Even if I use the sample code provided on the Microsoft website (http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx) I still get invalid handle exceptions!
|
|
|
|
|
Well, your previous post triggered my memory:
When I earlier said I did not have any printing problems, I was forgotten that I once
had major problems, if clicking the Prin button in the Preview Dialog.
This occurred some 2 years ago, when I added printing capabilities to my major application,
using either .NET 1.0 or 1.1 (without SP), I dont recall.
Since then I have been using my app and have printed and previewed hundreds of pages.
I do not recall what the exact problems were, but I concluded not to use that Print button
anymore.
I should also add I had some dificulties in understanding (or getting to work without
understanding) the printer settings regarding:
- page orientation
- color support
- duplex printing
And finally I have always been puzzled how one is supposed to do a Print Preview without
selecting a printer (with its margins, page orientation, etc). I would find it simpler
to have a print dialog with its normal choices, and both a Preview and a Print button.
Following your input I just finished a couple of experiments, and was unable to make
the Print Button on Print Preview Dialog fail; I have tried .NET 1.1 WITH SP1, as well as
NET 2.0 So maybe there has been a bug and it got fixed either in SP1 or in 2.0
What is your .NET version ? and the one your bug info refers to ?
Luc Pattyn
|
|
|
|
|
My .NET version is 2.0. I'm not sure what you mean when you ask "the one your bug info refers to", as I assume it would be the same. Can you elaborate if this isn't the information your looking for!
Thanks!
|
|
|
|
|
I was under the impression you had found a bug report.
Since you are using 2.0 I can't guess what's happening.
Could you try with different printers, and/or PDF writers, faxes ?
do they all show the same problem ?
BTW: I am actually preparing an article on a control to ease control previewing and
printing, so in another week or so, I will publish a program that works fine on my system,
and that you then could test on yours.
Luc Pattyn
|
|
|
|
|
I find it almost impossible to find any information on the Adobe site, so I thought I'd ask and see if anyone can clarify this issue.
In our software we want to use the adobe reader component embedded in a form to display a pdf. I found on the adobe site the licensing agreement for distributing adobe reader, but if we do not distribute it but only use the control and the user of our software either already has adobe reader or goes to the site and downloads it themself - are we allowed to do that or is there some agreement or conditions? I'm fairly certain that the control does not work unless the person has adobe reader already installed so it is not like we are actually distributing anything of adobe's at all.
I think I saw something at least hinting at the answer if not outright stating it, but I can't find it now!
ps. I know this is not strictly c# question, but I wasn't sure what forum to ask in.
"Your typical day is full of moments where you ask for a cup of coffee and someone hands you a bag of nails." - Scott Adams
|
|
|
|
|
FYI, you can view PDF files (as well as office documents and HTML files) using
AxSHDocVw.AxWebBrowser, which is, AFAIK the way Internet Explorer does the same.
Doing it that way you dont have to add anything to your PC...
Luc Pattyn
|
|
|
|
|
Hello, I'm asking yet another question to all of you geniuses.
I've gotten nearly every function working in the program I'm developing for work. The one thing I'm missing, however, is the ability to run commands on remote computers.
At first, I was hoping there was some .NET function to do this easily (you can start and stop services, so why not apps?) However, it does not appear so. Perhaps I missed one.
My next guess was to try to start telnet, and try to use SendKeys.Send() to send my commands... however, the commands pass into my Command Prompt before telnet finishes connecting.
The actual problem is that i'm hoping for a .NET managed way to run remote commands. A workaround for telnet like that would be usable, but only as a last-hope sort of thing. If possible, perhaps there is a .NET managed way to perform the task without running DOS commands remotely? This is what I'm trying to do:
RemoteComputer comp = new RemoteComputer("Target");
comp.Run("wuauclt.exe /resetauthorization");
comp.Run("wuauclt.exe /detectnow");
I'm simply trying to get the remote computer's Windows Update Agent to reset authorization, and then detect updates.
Thanks in advance to any of you who can help.
|
|
|
|
|
I suspect you need to have a program installed on the target computer, which you can communicate with. Otherwise, Windows would have no security at all, right ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Well, the security is not an issue.
Whatever the method I try to utilize, my account has Administrator rights on all of the target computers....... but perhaps I can work on the "Install a Program" idea.
Can anyone teach me about making a pushing services? I only know how to Start and Stop them remotely.
edit:
Actually, i found a great tutorial on how to make basic services. Cool.
-- modified at 18:24 Wednesday 17th January, 2007
|
|
|
|
|
How do you select the row that has the ">" pointer on it on the DataGridView? I guess i am looking for the current row or the active row or the row that has focus.
|
|
|
|