I am writing a piece of code but cannot figure out why it is showing some (from my point of view) weird behaviour. I get this is due to how I have written the code, but I cannot understand why it is not doing what I want it to do.
I have an eventhandler:
private void heatFinishedHandler(object sender, List<Kart> e)
{
mainForm.enableControls();
server.karts = e;
}
If I add e.Clear(); to it, it also clears server.karts.
The argument e comes from another class:
private List<Kart> kartlist = new List<Kart>();
heatFinished?.Invoke(this, kartlist);
The heatFinished event and the heatFinishedEventHandler are connected.
What do I need to do to get e by value, instead of reference? Or is it a value type, but is it pointing to kartlist?
I want to be able to get the value of e and store it in server.karts, not being able to change kartlist.
Make a copy of the list you pass to the function like
private List<Kart> kartlist = new List<Kart>();
heatFinished?.Invoke(this, kartlist.ToList());
Or do it inside the function like
private void heatFinishedHandler(object sender, List<Kart> e)
{
mainForm.enableControls();
server.karts = e.ToList();
}
.ToList() will make a new List-instance with the same values as the original one.
Related
Wierd behaviour when passing values to and from second form.
ParameterForm pf = new ParameterForm(testString);
works
ParameterForm pf = new ParameterForm();
pf.testString="test";
doesn't (testString defined as public string)
maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.
Thanks for help.
Posting more code here:
calling
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
pf.ShowDialog(this);
pf.test = "test";
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
};
definition and use
public partial class ParameterForm : Form
{
public string test;
public XmlElement node;
public delegate void ParameterSubmitResult(object sender, XmlElement e);
public event ParameterSubmitResult Submit;
public void SubmitButton_Click(object sender, EventArgs e)
{
Submit(this,this.node);
Debug.WriteLine(test);
}
}
result:
Submit - null object reference
test - null object reference
pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.
Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);
You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)
I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(testString);
pf.ShowDialog(this); // Blocks until user submits
// Do whatever pf_Submit did here.
};
public partial class ParameterForm : Form
{
public string test; // Generally, encapsulate these
public XmlElement node; // in properties
public void SubmitButton_Click(object sender, EventArgs e)
{
Debug.WriteLine(test);
this.Close(); // Returns from ShowDialog()
}
}
When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).
EDIT (a bit more precise):
You could write:
pf.getString(testString);
, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.
the thing was in line order :)
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
and
pf.Test = "test";
should have been set before
pf.ShowDialog(this);
my mistake thingking that parameter can be passed after 2nd form was displayed
thnx for answers
I am trying to get the value of a text box called txtNum on the form FrmPhone as well as write to the text box txtLog however there seems to be no interacation between the Static Void and the Text Boxes on FrmPhone.
Below is the code for the CreateCall Static Void.
private void btnCall_Click(object sender, EventArgs e)
{
CreateCall();
}
static void CreateCall()
{
FrmPhone FrmPhone = new FrmPhone();
var numberToDial = FrmPhone.txtNum.Text;
FrmPhone.txtLog.Text += "\r\nCall " + numberToDial;
FrmPhone.txtLog.ScrollToCaret();
SetupDevices();
call = softphone.CreateCallObject(phoneLine, numberToDial);
call.CallStateChanged += call_CallStateChanged;
call.Start();
}
What am I missing to allow me to interact with the text boxes on the form while executing code in the static void?
Also worth noting is that If replace
var numberToDial = FrmPhone.txtNum.Text;
with
var numberToDial = "2788";
The call is made but the txtLog text does not change.
There are two options.
You make the method non-static. This is the easiest solution and the one that makes most sense. You are after all using instance variables.
void CreateCall()
{
var numberToDial = this.txtNum.Text;
}
Pass in the form to the method, as said, it doesn't make sense, but it does the job. txtNum needs to be internal or public, or CreateCall must reside in the same class:
static void CreateCall(FrmPhone frmPhone)
{
var numberToDial = frmPhone.txtNum.Text;
}
In calling the code you pass in this:
CreateCall(this);
I have working code that I'd simply like to reference parts of multiple times in other area's of my program, however my problem seems to be with anonymous types (var)
here's a section of my program that loads my XML file
string path = "Data//handling4.meta";
var doc = XDocument.Load(path);
var items = doc.Descendants("HandlingData").Elements("Item");
var query = from x in items
select new
{
HandlingName = (string)x.Element("handlingName"),
HandlingType = (string)x.Element("HandlingType"),
Mass = (decimal?)x.Element("fMass").Attribute("value"),
InitialDragCoeff = (decimal?)x.Element("fInitialDragCoeff").Attribute("value"),
PercentSubmerged = (decimal?)x.Element("fPercentSubmerged").Attribute("value"),
DriveBiasFront = (decimal?)x.Element("fDriveBiasFront").Attribute("value")
};
This code works great, I can also use a foreach loop like this to reference items
foreach(var HandlingName in query)
{
string Names = HandlingName.HandlingName;
}
So my question is, how to reference that to another place in the program? for e.g.
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Add( Names);
}
The above code throws the error 'Names does not exist in the current context'
I need for my comboBox to display each handling name, but it fails because anonymous types cannot be referenced. Am I doing something wrong, or am I just not using the incorrect method? Thanks
Edit: Also to mention that the XML is needing to be referenced in multiple parts of the program, button click even shows the values in a DataGridView for example. If I could make a class to load the XML, then each element in different area's of the program that'd be ideal, rather than loading the hefty block of code each time
the XML is needing to be referenced in multiple parts of the program,
But all your variables are local. You need to store something in a Form fiead or property.
// outside any method:
private List<string> names = new List<string>();
void myLoadMethod()
{
...
foreach(var HandlingName in query)
{
//string Names = HandlingName.HandlingName;
Names.Add(HandlingName.HandlingName);
}
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Add( Names);
}
Within my WinForm i have a method that checks the validation of various user controls and adds them to an errorList. When the user clicks the save button I want it to check the validation method and show the errors if any in a message box. The Validate method is in another form and class so i think that might be my problem.
private void Save_Click(object sender, EventArgs e)
{
var errorList = string.Join(Environment.NewLine, Validate.ToArray());
MessageBox.Show(errorSet);
}
Thank you for any help.
The error 'Form1.Validate(System.Collections.Generic.List<string>)' is a 'method', which is not valid in the given context means that you're using the method wrong.
var errorList = string.Join(Environment.NewLine, Validate.ToArray());
makes no sense. You're missing the parentheses:
var errorList = string.Join(Environment.NewLine, Validate().ToArray());
That's only one problem. The method has a parameter of type List<string>, but you don't pass an argument to the function.
Also, you said in a comment that the return value is of type bool, but it seems you expect it to return a collection of strings.
You have this issue because you are calling the validate method that is on another form without mentioning the instance of that form.
Lets say you have another class Class1.
//create instance of your class/form that has this method
OperationControl oc = new OperationControl ();
private void Save_Click(object sender, EventArgs e)
{
//call the method with form instance created above
var errorList = string.Join(Environment.NewLine, oc.Validate().ToArray());
MessageBox.Show(errorSet);
}
some time this error means you may have the same method with the same name in your program scope. Check if no other function named MessageBox exist in your program
I need to retrieve some property of an element that trigger the event in this context:
SoundEffect alarm;
public MainPage()
{
InitializeComponent();
Pad1.MouseLeftButtonUp += new MouseButtonEventHandler(makeasound);
Pad2.MouseLeftButtonUp += new MouseButtonEventHandler(makeasound);
Pad3.MouseLeftButtonUp += new MouseButtonEventHandler(makeasound);
}
Pad1,2 and 3 are the names of some Ellipse I have in my xaml. Now if I try to do this in the code executed by the event ( declared immediately after the sample code above):
private void makeasound(object sender, MouseButtonEventArgs e)
{
string text = this.Name;
textBlock1.Text = text;
}
The Text Block becomes empty, so I guess the Name of the triggering element never gets there.
Now, things get more messy if I'm trying to retrieve a custom property of the "pads" called "Son", declared with the dependency method , which is a string, like this:
private void makeasound(object sender, MouseButtonEventArgs e)
{
string text = this.Son;
textBlock1.Text = text;
}
VS reports error:
'PhoneApplication.MainPage' does not contain a definition for 'Son' and no extension method 'Son' accepting a first argument of type 'PhoneApplication.MainPage' could be found (are you missing a using directive or an assembly reference?)
Where Phoneapplication is the name of the app and the main namespace of the code behind.
As if it weren't simple enough, what I'm tryin to do is this:
The custom property is actually an INT. I know I declared the dependency right since VS let me compile. Each Pad has this custom property storing an int, and I need to retrieve it to access an array element. The function triggered is this:
private void makeasound(object sender, MouseButtonEventArgs e)
{
int x = this.Son;
var sons = new[] { "sons/firstsound.wav", "sons/secondsound.wav", "sons/thirdsound.wav" };
string target = sons[x];
StreamResourceInfo info = Application.GetResourceStream(
new Uri(target, UriKind.Relative));
alarm = SoundEffect.FromStream(info.Stream);
Microsoft.Xna.Framework.FrameworkDispatcher.Update();
alarm.Play();
}
So, I declare an array storing URI's for sounds that I'd like to play("son" means sound in french, I'm from Belgium). I then use the INT associated with the triggering element to access the URI of a sound, then I play this sound.
The reason I do this is because I'd like to let the user change the INT value for each pad and therefore choose what sound each pad plays. The fact that I seem to have no choice but to declare this array each time the function is called (otherwise it's not in context) is not very elegant but I guess I can live with that ( array will have 50-60 elements in it)
So, for those who read this far, my problem is to use a property of the triggering event, which seems to be harder when it's a custom property. I put the rest of the logic in case someone had advices.
I thank anyone who read this message and who could maybe help me sorting this out. I read online documentation and I have two good c# books, but I havent found a solution for me.
Have a nice day.
EDIT: Some others are willing to help so here is the declaration of the dependency property(Sorry Daniel, hadn't seen you commented my original post)
namespace MyNamespace
{
public static class MyClass
{
public static readonly DependencyProperty SonProperty = DependencyProperty.RegisterAttached("Son",
typeof(string), typeof(MyClass), new PropertyMetadata(null));
public static string GetSon(UIElement element)
{
if (element == null)
throw new ArgumentNullException("element");
return (string)element.GetValue(SonProperty);
}
public static void SetSon(UIElement element, string value)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetValue(SonProperty, value);
}
}
Mynamespace is nested inside the main namespace.
this refers to the current instance of MainPage. Not to the pad that was clicked. That's the sender:
var pad = (Pad)sender;
var text = pad.Name;
textBlock1.Text = text;
Son is an attached property, not a normal one. You can get its value like this:
var pad = (Pad)sender;
var son = MyClass.GetSon(pad);
Please note that you have declared it as a string. Seeing how you want to use it, it seems to make more sense to declare it as an int.
this refers to the instance of the type in which the method is defined in, in this case, MainPage. If you want to get the instance of the type that triggered the event, that's what's in the sender parameter:
private void makeasound(object sender, MouseButtonEventArgs e)
{
textBlock1.Text = ((Pad)sender).Name;
}