|
If it's "all your code", than I would change all methods to have an "object sender" (and perhaps an "args-type" instance); and "reflect" on that.
Same pattern as the WPF / Windows Forms (ui) event model.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
With a compiled delegate, it's going to be almost impossible.
In your specific example, the Function.Target property will return an instance of an anonymous type, with a field for each captured variable, and one for the delegate:
Function.Target == new
{
Val1 = 3,
Val2 = 4,
Function = Func<int>
}
However, without reading the IL of Function.Method , you have no way of knowing which field corresponds to which argument.
Things get even more interesting if the anonymous closure type is shared between multiple closures, or you're not using a closure at all.
You might be able to extract the information if you use an Expression<Func<T>> [^] instead of a Func<T> . In simple cases, the following seems to work:
Expression<Func<int>> expr = () => Class1.GetValue(Val1, Val2);
var body = (MethodCallExpression)expr.Body;
var parameters = body.Method.GetParameters();
for (int index = 0; index < body.Arguments.Count; index++)
{
var arg = (MemberExpression)body.Arguments[index];
var instance = arg.Expression == null ? null : ((ConstantExpression)arg.Expression).Value;
var field = (FieldInfo)arg.Member;
object value = field.GetValue(instance);
Console.WriteLine("{0} = {1}", parameters[index].Name, value);
}
However, this code is rather fragile, and likely to break if you pass in any "interesting" expressions.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I've meant to respond to this post for a while as it deserves a proper answer, but I had to leave for Germany to go to a funeral so I haven't really had the time until now.
But the very long answer that I thought out boiled down to a very short one in the end.
I'm going to explore the Expression methodology, more for educational purposes than anything else, but since it indeed seems to be quite fragile I'm going for a different architecture for now.
Thanks for the insight!
|
|
|
|
|
Hello,
I think I am level basic at programming, but I'm not lazy to search on internet when I have a problem.
Although I need help because I searched on the internet for hours and when I found something what was looking good, it didn't help me.
And because of that I'm asking you guys if you would help me please. In code's comments are my problems. Thank you for replies.
<pre>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace kg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
double price = 0;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 0)
price += 82.36;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 1)
price += 125.23;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 2)
price += 163.98;
}
private void Calculate_Click(object sender, EventArgs e)
{
MaterialPriceLabel.Text = price;
}
}
|
|
|
|
|
Member 13010731 wrote: Why is the "price" underlined?
Because you didn't google search "variable scope"
Speed of sound - 1100 ft/sec
Speed of light - 186,000 mi/sec
Speed of stupid - instantaneous.
|
|
|
|
|
You need to learn about variable scope.
3.7 Scopes (C#)[^]
A local variable declared in one method is not available outside of that method. Can you imagine how confusing it would be if that was not the case? If every single local variable had to have a unique name across your entire class or project, so that it didn't conflict with other local variables in other methods?
To make it available to other methods in the same class, make it a field:
public partial class Form1 : Form
{
private double price = 0;
public Form1()
{
InitializeComponent();
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 0)
price += 82.36;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 1)
price += 125.23;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 2)
price += 163.98;
}
private void Calculate_Click(object sender, EventArgs e)
{
MaterialPriceLabel.Text = price;
}
}
Fields (C# Programming Guide)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, I really appreciate it. You helped me a lot, but when I click button, in the "MaterialPriceLabel" appear 0 every time (there should be for example: 82.36) and I think my "if part" of the code is good. So problem would be in the sending of value to the "price", I think. Can you give me some advice please?
public partial class Form1 : Form
{
private double price = 0;
public Form1()
{
InitializeComponent();
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 0)
price += 82.36;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 1)
price += 125.23;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 2)
price += 163.98;
}
private void Calculate_Click(object sender, EventArgs e)
{
MaterialPriceLabel.Text = price.ToString();
}
}
|
|
|
|
|
|
I've got only one problem. I'm trying debug and it works, nevertheless debugging didn't help me.
public partial class Form1 : Form
{
private double price = 0;
public Form1()
{
InitializeComponent();
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 0)
price += 82.36;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 1)
price += 125.23;
if (Material.SelectedIndex == 0 && Thickness.SelectedIndex == 2)
price += 163.98;
}
private void Calculate_Click(object sender, EventArgs e)
{
MaterialPriceLabel.Text = cena.ToString();
}
}
Please help me how to get value of price to Calculate_Click.
|
|
|
|
|
Step through the code, and you'll find that none of your if conditions are matched, so the price field is never updated.
You need to debug your code. I can't do that for you.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Why did you put those if statements in the constructor of the Form? They will only ever run once, when the form instance is created. If seems those statements need to be put somewhere else.
Also, you don't seem to have a grasp of the basics of C# or debugging techniques. True?
|
|
|
|
|
Dave Kreskowiak wrote: Also, you don't seem to have a grasp of the basics of C# or debugging techniques. True?
Yes you are right. I'm beginner, I self study at home(1 month so far) and this little application I am doing for my self study. Please help where should I give that "if section". Images of program here: http://i.imgur.com/7ncQt2m.png http://i.imgur.com/1XNUPeh.png
modified 20-Feb-17 13:15pm.
|
|
|
|
|
Somewhere where it makes sense in your business rules. The constructor isn't it.
When do you want these statements to execute? If it's triggered by the click of a button, there's a big hint right there.
|
|
|
|
|
You check the "SelectedIndices" in the constructor of the form. The constructor gets executed once at the very start of showing the form - and then never again. At that point you couldn't possibly have changed the selections of the controls Material and Thickness . And after you have changed the selections they're never checked again. You need to move the if -checks to a "better" place that fits the (presumed) requirement that they're executed each time you click the calculate-button.
You might also want to take a look at ComboBox.SelectedIndex Property (System.Windows.Forms)[^]
(assuming Material and Thickness are of type System.Windows.Forms.ComboBox)
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I created a RESTful WCF Service and made sure that the data returned from it is of type JSON by setting its ResponseFormat property equal to WebMessageFormat.Json in my WCF Service as follows:
ResponseFormat = WebMessageFormat.Json
I checked to ensure that data returned from the WCF Service is indeed JSON by using Fiddler.
What I don't understand is if the return type of the data is JSON then why do I need to convert it to a JSON string using JSON.stringify() in my jQuery AJAX call.
|
|
|
|
|
First of all, you did not share the output response of the service, and we have no idea what is going wrong there. Many things can go wrong, such as the C# code might be deserializing the JSON to an object and providing you with that object.
Also, the only purpose of JSON.stringify is to serialize the objects, if you are going to render the objects or their properties on the screen, there is no need to serialize the object at all. It would be a good idea to see the output of this,
success: function (response) {
alert(response);
}
This will either show [object Object], or some valid JSON document. That will give you an idea of the type of data that is being passed here.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Thank you so much for your reply. I can now see that the data I received from the AJAX call is indeed JSON.
|
|
|
|
|
increment a file path if already exists using mediafire,conversion options and mega data while saving a video in c#
here is my code
try
{
String path = open.FileName;
var inputFile = new MediaFile { Filename = path };
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
do
{
newLocation = "C:\\Users\\" + server + "\\Videos\\Matchbox\\keeper starts(" + filecount.ToString() + ").mp4";
filecount++;
if (!exists)
{
System.IO.Directory.CreateDirectory(folderLocation);
MessageBox.Show("Folder doesn't exist, Matchbox folder created!!");
}
var outputFile = new MediaFile { Filename = newLocation };
var options = new MediaToolkit.Options.ConversionOptions();
Difference = outTime - inTime;
options.CutMedia(TimeSpan.FromSeconds(inTime), TimeSpan.FromSeconds(Difference));
string message = "Please wait a few minutes while converting a file";
string title = "Warning!!!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult = MessageBox.Show(message, title, buttons);
engine.Convert(inputFile, outputFile, options);
MessageBox.Show("Success");
} while (File.Exists(newLocation));
// inTime = outTime = Difference = 0;
}
}
catch (Exception ex)
{
MessageBox.Show("" + ex.ToString());
}
|
|
|
|
|
Was there a question in there somewhere?
|
|
|
|
|
"C#" is not a good subject-line; all the posts in this forum are about C#.
newLocation = "C:\\Users\\" + server + "\\Videos Sorry, that is not a server, but a user.
..if I understand you correctly, you want to convert the next file in that folder? Get a list of files, and loop through those.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
And you have a question ? a problem ?
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
I would encourage to use Path class for any path related coding in C#
"C:\\Users\\" + server + "\\Videos\\Matchbox\\keeper starts(" + filecount.ToString() + ").mp4";
|
|
|
|
|
I am developing an English vocabulary related project. Is there any open source service available, where I can consume that to get the meanings for words.
Srinubabu Ravilla
|
|
|
|
|
What does this have to do with C#?
I'd suggest you post it as a QA question: [^] where it will reach a wider audience.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I need C# supported service. So that's why I asked this here. Thanks for your suggestion. I'll post it as a question.
Srinubabu Ravilla
|
|
|
|