I have a very basic understanding of using this technique so I probably and missing something simple. All I want to do is add a variable that holds the TOTAL items but the variable seems to be getting initialized back to 0 when I check it. My best guess is something is going out of scope and I am losing the value.
I call a function that passes a delegate(?)
[TestMethod]
public void Batch_Update_Is_Working()
{
DataTable dt = ExcelHelper.ReadAsDataTable(pathFileName);
EncompassBoxHelper.UpdateBoxes(ec, dt, progess => UpdateProgressBar(pi));
}
Inside the function I want to pass a value back.
public static void UpdateBoxes(EncompassConnection ec, DataTable dt, Action<ProgressInfo> updateProgress)
{
Session s = EncompassSession.Instance(ec.Url, ec.Name, ec.Password);
updateProgress(new ProgressInfo(dt.Rows.Count));
This is where the value is stored. In ItemsTotal. All good so far.
public class ProgressInfo
{
public int itemsProcessed { get; set; }
public int itemsTotal { get; set; }
public ProgressInfo()
{
}
public ProgressInfo(int it)
{
itemsTotal = it;
}
}
Now when it goes to the callback the value is 0??? pi.itemsProcessed is suppose to be 100.
public void UpdateProgressBar(ProgressInfo pi)
{
pi.itemsProcessed++;
Debug.WriteLine("Progress Info.ItemsProcessed: " + pi.itemsProcessed);
Debug.WriteLine("Progress Info.ItemsTotal: " + pi.itemsTotal);
}
Change your
EncompassBoxHelper.UpdateBoxes(ec, dt, progess => UpdateProgressBar(pi));
to
EncompassBoxHelper.UpdateBoxes(ec, dt, UpdateProgressBar);
Instead of sending the UpdateProgressBar() method to match the delegate expected in UpdateBoxes parameter, you're creating a new one by converting lambda expression, telling it to call itself specifically with a parameter of pi, instead of the one, that should be passed inside UpdateBoxes method (the new ProgressInfo(dt.Rows.Count) object).
Looks like your pi variable is not being passed by reference so it is never really getting updated. Honestly changing a value like pi.itemsProcessed++ in your delegate function is not very common although there are ways to do it like this.
Once your delegate supports passing in a variable with the "ref" keyword, then you should be able to call your delegate with something like this:
progess => UpdateProgressBar(ref pi)
You could also make pi a static variable and then it can be updated from your delegate as well so you don't have to worry about passing it around.
Related
I have this code:
public class NewFrame
{
public NewFrame(string iconSource = Const.Car,
string iconColor = Const.Red)
{
When I try and use it then it's telling me I am missing a default constructor. How can I add one of these and still make the code use the default values for iconBackgroundColor and IconSource? I thought that adding in those defaults with the = Const. would make it work but it seems like it doesn't think my constructor is a default (with no params).
You just have to add another empty overload and call the required constructor with defaults. See below:
public class NewFrame
{
public NewFrame() : this(Const.Car, Const.Red){
}
public NewFrame(string iconSource,
string iconColor)
{
...
}
}
By having two optional parameters, you don't actually create 4 different constructor declarations under the hood (one with both parameters, one with the first parameter, one with the second parameter, and one with neither). There is still only one constructor, with two parameters. It's just that C# recognises that the parameters are optional, and has syntactic sugar to let you omit them when you call the constructor.
However, if you use reflection to create an instance of your class (probably whatever the thing that requires a default constructor is doing), and you attempt to invoke the parameterless constructor, it won't find one, because there is no syntactic sugar in reflection.
Here is an example:
class MainClass
{
public static void Main(string[] args)
{
Type t = typeof(MainClass);
object o = Activator.CreateInstance(t, 1);
Console.WriteLine(o);
}
public MainClass(int a = 10)
{
}
}
If you use typeof(MainClass).GetConstructors(), it will tell you that there is only one.
To actually declare a default constructor, you can do:
public class NewFrame
{
public NewFrame(string iconSource = Const.Car,
string iconColor = Const.Red)
{
...
}
public NewFrame() : this(Const.Car, Const.Red) { }
}
For what it's worth, when I do something like this, I take the route that #VyacheslavBenedichuk's answer is showing.
I'm not sure what your complaint is. This code compiles for me:
public class TestConstructor
{
public TestConstructor(string what = Const.Car, string color = Const.Red)
{
}
public static void Test()
{
var tc = new TestConstructor();
}
public class Const
{
public const string Car = "car";
public const string Red = "red";
}
}
What do your definitions of Const.Car and Const.Red look like? Where are you seeing the error?
But, if you use something that requires a default constructor, then this will not work. For example, this will fail at runtime:
var tc2 = Activator.CreateInstance(typeof(TestConstructor));
Please, when you are reporting an error, describe it exactly - in particular say whether it's a runtime or a compile-time error, the exact wording of the error, and the context in which the error occurs. In this case (the call to CreateInstance) will result in a System.MissingMethodException: 'No parameterless constructor defined for this object.'
In this case, you need to follow #VyacheslavBenedichuk's advice
I would like to use a function as a variable for me to assign.
Something like this
Class:
public class Func
{
public string Name { get; set; }
public string Content { get; set; }
public Function function { get; set; } //Not sure what type for a func
}
Func:
public void Click_Event(object sender,EventArgs e)
{
//Some code here.
}
List to assign the func:
List<Func> funcList = new List<Func>();
Func func = new Func();
func.Name = "Name";
func.Content = "Hello World";
func.function = Click_Event; //This is what I really want to do.
funcList.Add(func);
Is there anyway to achieve this?
Edited
Some of the comment below said that Reflection may cause the performance issue, but I use it and doesn't seen any slow issue currently. I use it when I first start the app only.
This is the sample code I use it.
MethodInfo method = this.GetType().GetMethod("FuncName"); //Or using nameof
btnFunc.AddHandler(Button.ClickEvent, Delegate.CreateDelegate(Button.ClickEvent.HandlerType, this, method, false));
Try using a delegate:
A delegate is a data structure that refers to a static method or to a
class instance and an instance method of that class.
https://learn.microsoft.com/en-us/dotnet/api/system.delegate?view=netframework-4.8
Another option to look into would be an Action or Func
After a several research, I have found another way to solve this.
Save the function name as a string instead of as a Action or delegate
Then use System.Reflection.MethodInfo and btn.AddHandler to use it.
By this way, the function must be public.
I have a static double variable in a static class. When I create a specific class, I use the double variable as one of the args of the constructor. What would be the easiest way of manipulating field of the object by changing the variable in static class.
Code for clarity:
public static class Vars
{
public static double Double1 = 5.0;
}
public class ClassFoo
{
public double Field1;
public ClassFoo(double number)
{
Field1 = number;
}
}
class Program
{
static void Main(string[] args)
{
ClassFoo Foo = new ClassFoo(Vars.Double1);
Console.WriteLine(Foo.Field1 + " " + Vars.Double1); //5 5
Vars.Double1 = 0.0;
Console.WriteLine(Foo.Field1 + " " + Vars.Double1); //5 0
//Foo.Field1 need to be a reference to Vars.Double1
}
}
EDIT that goes beyond the question (no more answers needed, other solution found):
I change some values (fields) very often (at runtime, or at least i would like to change them at runtime) to look for one that is right for me. Implementing:
if(KeyDown)
variable++;
if(OtherKeyDown)
variable--;
Wasn't convenient enough. I just checked Visual Studio Debugger. It's not good (fast) enough. Have to pause, change and run code code again. Method i presented would be good if changed static variable would change field of the object.
In short: no, you can't do this... at least, not seamlessly.
As noted, this is generally considered to be A Bad Idea™. There is no reference encapsulation for value types, and no simple way to implement a seamless wrapper class to do it because you can't overload the assignment operators. You can use the techniques from the Nullable<T> type to get part-way there, but no further.
The big stumbling block is the assignment operator. For the Nullable type this is fine. Since it is non-referencing (new values are distinct), an implicit conversion operator is sufficient. For a referencing type you need to be able to overload the assignment operator to ensure that assignment changes the contained data instead of replacing the wrapper instance.
About the closest you can get to full reference is something like this:
public class Refable<T> where T : struct
{
public T Value { get; set; }
public Refable(T initial = default(T))
{
Value = initial;
}
public static implicit operator T(Refable<T> self)
{
return self.Value;
}
}
This will hold a value of the specific type, will automatically convert to that type where applicable (Refable<double> will implicitly convert to double when required for instance), but all assignments must be done by referencing the Value property.
Example usage:
Refable<double> rd1 = new Refable<double>(1.5);
Refable<double> rd2 = d1;
// get initial value
double d1 = rd1;
// set value to 2.5 via second reference
rd2.Value = 2.5;
// get current value
double d2 = rd1;
// Output should be: 1.5, 2.5
Console.WriteLine("{0}, {1}", d1, d2);
What you really want to do is have Vars be a regular class, not a static class. For all methods and classes that need to deal with the variables contained in Vars, you can pass in a reference to that Vars instance.
Here is a very simple example program that illustrates the above. Note that you could probably do a lot to improve the design of your program, but this will at least get you going in the right direction, and away from trying to bend the language to do things it can't or shouldn't do.
public class SharedVars {
public static double Foo = 0.0;
}
public class ClassFoo {
private SharedVars mySharedVars;
public ClassFoo(SharedVars sharedVars) {
// save a reference to the shared variables container class for future use
mySharedVars = sharedVars;
}
// here's an example use
public void ProcessKeyDown() {
mySharedVars.foo++;
}
}
class Program {
static void Main(string[] args) {
SharedVars sharedVars = new SharedVars();
ClassFoo foo = new ClassFoo(sharedVars);
// ... some stuff happens ...
if(KeyDown)
foo.ProcessKeyDown();
}
}
I have class named "config" that have private string variable named "param".
I need to get from "config" class "param" variable sometimes as int type sometimes as bool type or string.
As I understand I need create 3 properties in config class,each property have to convert type, as follow:
The first property converts string to int, the second converts string to bool, the third property gets me the string value.
The class should look something like this:
class Config
{
private string param;
public int ParamAsInt
{
get
{
return int.Parse(param);
}
}
public bool ParamAsBool
{
get
{
return bool.Parse(param);
}
}
public string ParamAsString
{
get
{
return param;
}
}
}
But I don't know how can those properties be used in accordance to the variable type that I want to get out of class.
This code won't compile - int and such are reserved keywords and cannot be used as identifiers. You can either try naming your properties something like Int32Value, StringValue, etc., or try this:
public static implicit operator bool (Config config)
{
return bool.Parse(config.param);
}
public static implicit operator int (Config config)
{
return int.Parse(config.param);
}
This will allow for much cleaner code:
Config c = GetConfig("foo");
var isFeatureEnabled = false || c;
var spacing = 23 + GetConfig("bar");
You forgot to give your properties names. How would you expect to reference them? Something like this:
class Config
{
private string param;
public int ParamAsInt
{
get
{
return int.Parse(param);
}
}
public bool ParamAsBool
{
get
{
return bool.Parse(param);
}
}
public string ParamAsString
{
get
{
return param;
}
}
}
Note that I also fixed the casing in your calls to .Parse(). C# is case-sensitive. I also replaced the call to bool.TryParse() with bool.Parse(). The former (when used correctly, which this wasn't because it was missing a parameter) will only tell you if it is a bool, it won't tell you what value the bool actually has. (For example, bool.TryParse('false' out someBool) will return true.)
Of course, this code is a bit dangerous. You'll want to start with some more defensive programming to check those values. Basically, look up TryParse() and how to use it correctly. Something like this, for example:
public int ParamAsInt
{
get
{
var tmp = default(int);
if (int.TryParse(param, out tmp))
return tmp;
else
// do something else? throw a specific exception?
}
}
Additionally, what is the purpose of this code? It seems like a very rushed and poor design. For any given value of param (how is that even being set, by the way?) this just sort of randomly tries to expose typed properties for it. If you guess the correct one, you're still left with others that will throw exceptions. Surely there's a much cleaner way to accomplish what you're trying to do. So what are you trying to do?
I have some extension methods which could be used like this:
MyType myObject;
string displayName = myObject.GetDisplayName(x => x.Property);
The problem here is that it needs an instance, even if the extension method only needs the type MyType. So if there is no instance, it needs to be called like this:
string displayName = BlahBlahUtility.GetDisplayName((MyTpe x) => x.Property);
Which is not so nice anymore.
Is there a way to write better syntax for such cases?
What I actually want to do is this (pseudo language):
string displayName = MyType.Property.GetDisplayName()
Which of course does not work with C#.
But what about something like this:
string displayName = ((MyType x) => x.Property).GetDisplayName();
This is also not possible (after a lambda, a dot is not accepted).
Any ideas?
Edit:
My "favorite syntax" MyType.Property.GetDisplayName() seems to be misleading. I don't talk about static properties here. I know that this syntax won't be possible. I just tried to show in pseudo language, what information is necessary. This would be ideal, every additional stuff is just syntactical overhead. Any working syntax that is close to this would be great.
I don't want to write a certain extension method. I want an easy, readable and compile time safe syntax, using any language feature.
Have a look at the Express and Reflect classes in the Lokad Shared Libraries. Think they may help out with what you are trying to do. Read more here:
Strongly Typed Reflection in Lokad Shared
How to Find Out Variable or Parameter Name in C#?
From your comment: "I want an easy and compile time safe syntax to get information about members".
This is a very frequently requested feature and has been discussed in the C# team's meetings for about a decade, but has never been prioritised high enough to be included.
This blog post explains why:
http://blogs.msdn.com/ericlippert/archive/2009/05/21/in-foof-we-trust-a-dialogue.aspx
So for now, you're just going to be fighting against a missing feature. Maybe you could post more information about your broader problem and see if people can suggest different approaches.
Update
Without more info about your problem this is just guesswork. But if you have a property that represents a value but also carries additional "meta" information, you could always represent that as a new type and use an "injection" step to set everything up.
Here's a suggested abstract interface to such a "meta property":
public interface IMetaProperty<TValue>
{
TValue Value { get; set; }
string DisplayName { get; }
event Action<TValue, TValue> ValueChanged;
}
The value of the property is just another sub-property, with its type defined by the user.
I've put in the display name, and also as a bonus you've got an event that fires when the value changes (so you get "observability" for free).
To have properties like this in a class, you'd declare it like this:
public class SomeClass
{
public IMetaProperty<string> FirstName { get; private set; }
public IMetaProperty<string> LastName { get; private set; }
public IMetaProperty<int> Age { get; private set; }
public SomeClass() { MetaProperty.Inject(this); }
}
Note how the setters on the properties are private. This stops anyone from accidentally setting the property itself instead of setting the Value sub-property.
So this means the class has to set up those properties so they aren't just null. It does this by calling a magic Inject method, which can work on any class:
public static class MetaProperty
{
// Make it convenient for us to fill in the meta information
private interface IMetaPropertyInit
{
string DisplayName { get; set; }
}
// Implementation of a meta-property
private class MetaPropertyImpl<TValue> : IMetaProperty<TValue>,
IMetaPropertyInit
{
private TValue _value;
public TValue Value
{
get { return _value; }
set
{
var old = _value;
_value = value;
ValueChanged(old, _value);
}
}
public string DisplayName { get; set; }
public event Action<TValue, TValue> ValueChanged = delegate { };
}
public static void Inject(object target)
{
// for each meta property...
foreach (var property in target.GetType().GetProperties()
.Where(p => p.PropertyType.IsGenericType &&
p.PropertyType.GetGenericTypeDefinition()
== typeof(IMetaProperty<>)))
{
// construct an implementation with the correct type
var impl = (IMetaPropertyInit)
typeof (MetaPropertyImpl<>).MakeGenericType(
property.PropertyType.GetGenericArguments()
).GetConstructor(Type.EmptyTypes).Invoke(null);
// initialize any meta info (could examine attributes...)
impl.DisplayName = property.Name;
// set the value
property.SetValue(target, impl, null);
}
}
}
It just uses reflection to find all the IMetaProperty slots hiding in the object, and fills them in with an implementation.
So now a user of SomeClass could say:
var sc = new SomeClass
{
FirstName = { Value = "Homer" },
LastName = { Value = "Simpson" },
Age = { Value = 38 },
};
Console.WriteLine(sc.FirstName.DisplayName + " = " + sc.FirstName.Value);
sc.Age.ValueChanged += (from, to) =>
Console.WriteLine("Age changed from " + from + " to " + to);
sc.Age.Value = 39;
// sc.Age = null; compiler would stop this
If you're already using an IOC container you may be able to achieve some of this without going directly to reflection.
It looks like you're trying to create a static extension method?
DateTime yesterday = DateTime.Yesterday(); // Static extension.
Instead of
DateTime yesterday = DateTime.Now.Yesterday(); // Extension on DateTime instance.
If this is what you're trying to pull off, I do not believe it is possible in the current version of C#.
It sounds like you are integrating layers a little too tightly. Normally in this type of situation I would let the presentation layer decide the implementation of GetDisplayName() instead of making it an extension of the property itself. You could create an interface called MyTypeDisplayer or whatever you fancy, and let there be multiple implementations of it not limiting you to a single display implementation.
The issue here is that one cannot get a reference to non-static methods via instance MyType.[Member]. These can only be seen through a reference to an instance of the type. You also cannot build an extension method on-top of a type declaration, only on an instance of a type - that is the extension method itself has to be defined using an instance of a type (this T x).
One can however define the expression like this to get a reference to static members:
((MyType x) => MyType.Property)
One could do something similar to string displayName = ((MyType x) => x.Property).GetDisplayName();
The first issue is guaranteeing that the compiler treats your (x=> x.Property) as an Expression rather than an action/func etc...
To do this one might need to do this:
string displayName = ((Expression<Func<PropertyType>>)((MyType x) => x.Property).GetDisplayName();
The extension method would then have to be defined like this:
public static string GetDisplayName<T>(this Expression<Func<T>> expression)
You might also have to define an extension method on top of Expression<Action>> and Expression<Action<T>> if your members are also methods.
You can do a dot after an Expression - this is where the Compile method would reside.
Appended:
I think the static call to the extension method in cases that one doesn't have an instance of the type one needs to do "reflection" on to determine a Members name would be the cleanest syntax still - this way you could still use the extension method when using an instance of a type and fall back to the static call definition => MyExtensionClass.GetDisplayName(TypeOfX x => TypeOfX.StaticMember OR x.Property/Member) when one doesn't have an instance
If you interface your properties, you could make the extension on the interface instead:
namespace Linq1
{
class Program
{
static void Main(string[] args)
{
MyType o = new MyType();
o.Property.GetDisplayName();
}
}
public class MyType
{
public IDisplayableProperty Property { get; set; }
}
public interface IDisplayableProperty
{
string GetText();
}
public class MyProperty1 : IDisplayableProperty
{
public string GetText() { return "MyProperty2"; }
}
public class MyProperty2 : IDisplayableProperty
{
public string GetText() { return "MyProperty2"; }
}
public static class Extensions
{
public static string GetDisplayName(this IDisplayableProperty o)
{
return o.GetText();
}
}
}