public string F03_veri_textbox(string veriadi3)
{
string Veritext;
Veritext = webBrowser_sample.Document.GetElementById(veriadi3).GetAttribute("value");
return Veritext;
}
I have a webBrowser_sample object in Form1. I use this function to collect data from specific webpage. It is working properly.
I want to use this function from a class.
But when I try to move it, C# says "The name 'webBrowser_sample' does not exist in the current context".
If I define a new webBrowser_sample in the Class, it will create new webBrowser_sample object.
So I can't use it because I use this function to collect data while I am surfing this browser.
Replace 'mytype' with the object type that webBrowser_sample is. You need to pass in a reference to the object, as in the code below. Another option would be to use an extension method.
public string F03_veri_textbox(string veriadi3, mytype browser)
{
string Veritext;
Veritext = browser.Document.GetElementById(veriadi3).GetAttribute("value");
return Veritext;
}
Related
I have a list of images like this:
public List<Image> imageList = new List<Image>();
I also have a picture class in order to collect and manipulate data about the images in the list:
public Class Pic {
// properties and stuff
}
And then I have a function that takes an integer as an argument. That integer corresponds to an image in the image list. What I want to do in the function is to check if an instance of the Pic class has been created for that particular image. If not, I want to create it, using the value of the variable passed into the function. The following code obviously doesn't work, but it shows what I want:
public void doStuffWithImage(int picNumber) {
// Check if instance called pic + picNumber exists
if(pic + picNumber.toString() == null) {
// Create an instance
Pic pic + picNumber.toString() = new Pic();
}
}
Suggestions on how to accomplish this?
It seems like you're trying to create individual variables pic1, pic2, etc. you'd be better off using a dictionary:
Dictionary<int, Pic> pics = new Dictionary<int, Pic>();
public void doStuffWithImage(int picNumber) {
// Check if instance called pic + picNumber exists
if(!pics.ContainsKey(picNumber)) {
// Create an instance
pics[picNumber] = new Pic();
}
}
You need to create a "registry" of known Pics. DIctionary<int,Pic> would be good collection to hold this registry. You need to store the registry itself somewhere - perhaps in the "factory" object that registers your pictures.
class PicFactory {
private readonly IDictionary<int,Pic> knownPics = new Dictionary<int,Pic>();
public Pic GetOrCreate(int id) {
Pic res;
if (knownPics.TryGetValue(id, out res)) {
return res;
}
res = new Pic(id.ToString()); // This assumes that Pic(string) exists
knownPics.Add(id, res);
return res;
}
}
This way of implementing a registry may be too primitive for your purposes - for example, if you need your registry to be concurrent, you would need to set up some sort if a locking scheme to protect the knownPics dictionary. The class that accesses pictures would need to create an instance of PicFactory, so that it could access pictures through the GetOrCreate(id) call.
If you are using .net 4.0 or more you can use Lazy type which:
Provides support for lazy initialization.
Which means that the object will be constructed not in the moment of declaration, but when first accessed.
So you can basically declare them like
List<Lazy<Pic>> ....
See Lazy<T> and the Lazy Loading Pattern in general - this is actually a common optimization technique as it defers what can add up to a lot at startup to microdelays during runtime.
Be wary about making sure the microdelays are worth it, and I advise leaving methods about which can force loading.
If you're grabbing from a list, preface with a .Any or .Contains check, and since you're looking up by name like that, consider using a Dictionary instead
Here I am again with another question about C#.
So,here are some of the files i have in my project.
Configuration.cs
Settings1.cs
Bot.cs
Now, the problem is, in Settings1.cs I have made a callback (If that is what you call it in C#).
public void LoadText(Configuration.BotInfo config)
{
txtUsername.Text = config.Username;
txtPassword.Text = config.Password;
txtName.Text = config.DisplayName;
txtPrefix.Text = config.DisplayNamePrefix;
txtBackpack.Text = config.Backpack;
txtSell.Text = KeyUserHandler.SellPricePerKey.ToString();
txtBuy.Text = KeyUserHandler.BuyPricePerKey.ToString();
lblPrice.Text = value.ToString();
}
As you can see, it is getting the data from the Configuration.cs file. What I want to do, is that I wanna call this under the Settings1_Load callback.
So, when I type
LoadText();
It gives me the error that it cannot have 0 arguments.. But what argument can I use here? I am only kind of 'dimming' Configuration.BotInfo as config because if I use the full name everywhere, it gives me the non-static and static field error.
No, it is not getting data from Configuration.cs file, it is getting data from that argument which is named config and the type of argument is Configuration.BotInfo. Probably BotInfo is a class which is defined inside of your Configuration.cs file.You should pass a BotInfo instance to your function to make it work.
For example you can call your method like this:
// set your other properties
LoadText(new BotInfo { Username = "user2331", Password="1234", ... })
I'm working on a windows phone 7 application which uses Silverlight. What I'm looking to do is create a new instance of a class given a string containing the name of the correct class I would like to create. Below is the snipit of code I'm referring to and I am trying to use it to create the new instance. I know from debugging that serviceClass contains the correct string and if I add ".cs" to it, it would now correspond directly to one of the classes I have, so why isn't it being created?
WebViewService foundService; //From above
....
....
services.TryGetValue(mode, out foundService); //Find service
if (foundService == null)
{
string serviceClass;
serviceRegistry.TryGetValue(mode, out serviceClass); //Find serviceClass
if (serviceClass != null) //create new web service if one is found
{
try
{
//TODO: This is not working properly, should create an instance of some child class of WebViewService
foundService = (WebViewService)System.Activator.CreateInstance(Type.GetType(serviceClass+".cs"));
services.Add(mode, foundService);
}
catch
{
//But instead it always ends up here with ArgumentNullExeption
}
}
}
return foundService;
}
Any help at all or any suggestions would be greatly appreciated. Thanks for your time!
If your string contains fully qualified type name then you can create instance.
Try using the full name of the class (namespace included), without the ".cs" part. For example: YourApp.Services.YourWebViewService
Type.GetType doesn't take a string with a file name. It takes a class name or a struct name.
In my WinForms application I need to call javascript function from my WebBrowser control. I used Document.InvokeScript and it works perfect with functions alone e.g
Document.InvokeScript("function").
But when i want to call javascript object method e.g.
Document.InvokeScript("obj.method")
it doesn't work. Is there a way to make it work? Or different solution to this problem? Without changing anything in the javascript code!
Thanks in advance :)
The example in the documentation does NOT include the parenthesis.
private void InvokeScript()
{
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
String str = doc.InvokeScript("test").ToString() ;
Object jscriptObj = doc.InvokeScript("testJScriptObject");
Object domOb = doc.InvokeScript("testElement");
}
}
Try
Document.InvokeMethod("obj.method");
Note that you can pass arguments if you use HtmlDocument.InvokeScript Method (String, Object[]).
Edit
Looks like you aren't the only one with this issue: HtmlDocument.InvokeScript - Calling a method of an object . You can make a "Proxy function" like the poster of that link suggests. Basically you have a function that invokes your object's function. It's not an ideal solution, but it'll definitely work. I'll continue looking to see if this is possible.
Another post on same issue: Using WebBrowser.Document.InvokeScript() to mess around with foreign JavaScript . Interesting solution proposed by C. Groß on CodeProject:
private string sendJS(string JScript) {
object[] args = {JScript};
return webBrowser1.Document.InvokeScript("eval",args).ToString();
}
You could make that an extension method on HtmlDocument and call that to run your function, only using this new function you WOULD include parenthesis, arguments, the whole nine yards in the string you pass in (since it is just passed along to an eval).
Looks like HtmlDocument does not have support for calling methods on existing objects. Only global functions. :(
Unfortunately you can't call object methods out of the box using WebBrowser.Document.InvokeScript.
The solution is to provide a global function on the JavaScript side which can redirect your call. In the most simplistic form this would look like:
function invoke(method, args) {
// The root context is assumed to be the window object. The last part of the method parameter is the actual function name.
var context = window;
var namespace = method.split('.');
var func = namespace.pop();
// Resolve the context
for (var i = 0; i < namespace.length; i++) {
context = context[namespace[i]];
}
// Invoke the target function.
result = context[func].apply(context, args);
}
In your .NET code you would use this as follows:
var parameters = new object[] { "obj.method", yourArgument };
var resultJson = WebBrowser.Document.InvokeScript("invoke", parameters);
As you mention that you cannot change anything to your existing JavaScript code, you'll have to inject the above JavaScript method in some how. Fortunately the WebBrowser control can also do for you by calling the eval() method:
WebBrowser.Document.InvokeScript("eval", javaScriptString);
For a more robust and complete implementation see the WebBrowser tools I wrote and the article explaining the ScriptingBridge which specifically aims to solve the problem you describe.
webBrowser.Document.InvokeScript("execScript", new object[] { "this.alert(123)", "JavaScript" })
for you supposed to be like this
webBrowser.Document.InvokeScript("execScript", new object[] { "obj.method()", "JavaScript" })
I am using dynamic method calls to access a method in a dynamically loaded dll.
I am doing:
dynamic classInstance = Activator.CreateInstance(cmd.Type);
classInstance.AddString(); //This line works
classInstance.AddString(cmd); //this line does not work
The methods in the dll are:
public CustomCommandTest1()
{
}
public void AddString(Command cmd, ExposedVariables exv)
{
exv.ChopResults += "Add string Command";
}
public void AddString(ExposedVariables exv)
{
exv.ChopResults += "Add string Command";
}
public void AddString()
{
string ChopResults = "Add string Command";
}
I can access (call) all the methods that do not have parameters but those that do give me a RuntimeBindingInternalCompilerException. There is no usable info in the exception to try to figure this out.
I have done this before and it has worked. I don't know what I am doing differently here.
Further discovery here reveals that it is related to the complex variable types. Simple builtin variable types works. There is no difference in the definition of the complex variables however as I refer to the definition in a common file.
AddString(cmd) could only work if cmd is actually an instance of ExposedVariables. There's no overload of just
public void AddString(Command cmd)
which is what it looks like you're expecting.
This has nothing to do with complex variable types - it has everything to do with you trying to call a method which doesn't exist. Which overload did you expect to be called, out of the ones you presented to us?
If the cmd variable in your example is a reference to a Command instance rather than an ExposedVariablesinstance, then the call is wrong. You do not have an AddString overload that takes a Command only.
try
ExposedVariables exv = new ExposedVariables();
classInstance.AddString(cmd, exv);
as you don't seem to have an overload that takes just cmd.