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", ... })
Related
I am trying to get the path to the private external storage directory of my c# xamarin android app. The microsoft docs tell us to use
Android.Content.Context.GetExternalFilesDir(string)
However, when I try to use this function, I get the following error regarding that function:
An object reference is required for the non-static field, method or property
This is my code
class RecorderController {
string externalStoragePath;
public RecorderController() {
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
// I have also tried the following:
// string s = null;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
// Even when I try to get the path to the Downloads folder, I get the same error:
// string s = Android.OS.Environment.DirectoryDownloads;
// externalStoragePath = Path.Combine(Context.GetExternalFilesDir(s), "recordings");
}
}
I have no clue how to solve this, does anybody know what I am doing wrong?
Try to change
externalStoragePath = Path.Combine(Context.GetExternalFilesDir(null), "recordings");
to
externalStoragePath = Path.Combine(GetExternalFilesDir(null).AbsolutePath, "recordings");
Apparently I had to use an instance of Context, like Mike Christensen commented. I also missed that GetExternalFilesDir returns a File object, and that I should use AbsolutePath after it, like Leo Zhu said.
I changed my code to
class RecorderController {
string externalStoragePath;
public RecorderController(Context con) {
PermanenteOpslagPad = Path.Combine(con.GetExternalFilesDir(null).AbsolutePath, "recordings");
}
}
I have properly overwrite commit in InstallerSetup.cs I do not wish to write the user entered value to app.config but rather I want to pass the string Context.Parameters["TESTPARAMETER"]; to another class in form1.cs on load function. I tried string test = InstallerSetup.Context.Parameters["TESTPARAMETER"];
but getting InstallerSetup.Context is null. Please Help.
InstallerSetup.cs
public static string SQLSERVERNAME = "";
public static string HMSTENANTDB;
public static string SQLLOGIN;
public static string SQLPASSWORD;
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
try
{
SQLSERVERNAME = Context.Parameters["SQLSERVERNAME"];
HMSTENANTDB = Context.Parameters["HMSTENANTDB"];
SQLLOGIN = Context.Parameters["SQLLOGIN"];
SQLPASSWORD = Context.Parameters["SQLPASSWORD"];
}
catch (Exception e)
{
MessageBox.Show("Failed to update the application configuration file : " + e.Message);
base.Rollback(savedState);
}
}
from1.cs
InstallerSetup InsSetup = new InstallerSetup();
string Vsqlserver = InsSetup.Installers.Count.ToString();
string Vtenant = "";
if (InsSetup.Context != null)
{
Vtenant = InsSetup.Context.Parameters["HMSTENANTDB"];
}
else
{
Vtenant = "context is null";
}
As far as I can tell, the issue is that the property values are not being passed into the custom action. That would be the most obvious explanation. A commentfrom the poster says:
"passed those parameters to the custom action...................................... SQLSERVERNAME = Context.Parameters["SQLSERVERNAME"];
etc...
//................there is only these 4 lines in my custom actions"
which is essentially repeating the code that was previously posted.
This is NOT passing the values into the custom action. This is retrieving values which must already have been passed into the custom action.
Assuming that the custom action has been correctly added to (typically) the install nod of the custom action, and also assuming that the property names are in a TextBoxes dialog in the install, the values must be passed in to the custom action via the CustomActionData settings. To use one example, the CustomActionData setting must be something like:
/SQLSERVERNAME=[SQLSERVERNAME]
or /SQLSERVERNAME=[EDITA1] if EDIOTA1 is being used because that's the default property name.
However there is no reference to the TextBoxes (or any other) install dialog in the original question, so it's not really clear where the value of (say) SQLSERVERNAME is supposed to come from. It may be passed in on the msiexec command line, for example.
I imported the taglib-sharp dll (that had been copied to the bin/debug folder of my project) in my C# application and then used types and methods from the library in the following way:
using TagLib;
private void method()
{
TagLib.File file = TagLib.File.Create("C:\\temp\\some.mp3");
TagLib.Tag tag = file.GetTag(TagLib.TagTypes.Id3v2);
}
Now I want to link the dll dynamically. How can I implement the same functional in this case?
That, what I've tried:
using System.Reflection
private void method()
{
Assembly TagLib = Assembly.Load("taglib-sharp");
Type TagLibFile = TagLib.GetType("File");
dynamic LibFile = Activator.CreateInstance(TagLibFile);
TagLibFile file = LibFile.Create("c:\\temp\\some.mp3");
}
In this implementation, VisualStudio says that I can't use the tagLibFile variable as a type. I supposed that when I get a type from dll, I will be able to create variables of this type.
By the way, is this approach is correct?
P.S. Also, I tried to use the invoke method, but I was not sure what object I should pass as a first argument.
UPD
Based on #nawfal's awnser below, I've got the following working code:
using System.Reflection
private void method()
{
Assembly TagLib = Assembly.Load("taglib-sharp");
// get the File type
var fileType = TagLib.GetType("TagLib.File");
// get the overloaded File.Create method
var createMethod = fileType.GetMethod("Create", new[] { typeof(string) });
// get the TagTypes method that contains Id3v2 field
Type tagTypes = TagLib.GetType("TagLib.TagTypes");
// get the overloaded File.GetTag method
var getTagMethod = fileType.GetMethod("GetTag", new[] {tagTypes});
// obtain the file
dynamic file = createMethod.Invoke(null, new[] { "C:\\temp\\some.mp3" });
// obtain the Id3v2 field value
FieldInfo Id3TagField = tagTypes.GetField("Id3v2");
var Id3Tag = Id3TagField.GetValue(tagTypes);
// obtain the actual tag of the file
var tag = getTagMethod.Invoke(file, new[] { Id3Tag });
}
You should be doing something like this:
private void method()
{
var assembly = Assembly.Load("taglib");
var type = assembly.GetType("namespace.File"); // namespace qualified class name
// assuming you only have one Create method, otherwise use reflection to resolve overloads
var method = type.GetMethod("Create");
dynamic file = method.Invoke(null, new[] { "C:\\temp\\some.mp3" }); // null for static methods
var tag = file.GetTag(TagLib.TagTypes.Id3v2); // not sure if you can pass those params,
// may be do reflection to get them too
}
Kindly rethink if you want it to be dynamic. If you can reference the dll then you can still get the benefits of strong typing.
Save it as object.
object file = LibFile.Create(fi.FullName);
Should work.
Dynamic loading dlls works much different.
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;
}
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.