-2

I want to use show dialog () to display a message in another form. How do I show the message? Here's what I've tried.

//return total 
return total; 

//change total to string 
total = total.ToString;

//create an instance of the MessageForm class
MessageForm myMessageForm = new MessageForm();

//Display the form
myMessageForm.ShowDialog(total);

This gives me an error message. i don't know how to go about showing the total in the other form. Any suggestions?

Dmitry
  • 13,797
  • 6
  • 32
  • 48
dijit
  • 1
  • 3
  • 4
    Nothing after that `return` would run – jdphenix Apr 21 '15 at 03:41
  • Even if the return wasn't there, ShowDialog doesn't take an argument. You need to add a value to the constructor of the form or add a property to the form and set that property to the total. – Ron Beyer Apr 21 '15 at 03:43
  • What is the error it's giving you? In addition to the comment above, the thread needs to have the appropriate threading apartment (STA). – CalebB Apr 21 '15 at 03:43
  • 1
    I'll bite. What's the error message? And does it happen before or after the return total statement? Please don't say after. – Mike Cheel Apr 21 '15 at 03:49

1 Answers1

-3

The value of total you're passing in is supposed to be the owner of this new form.

You probably want to achieve this:

// remove the return line

total = total.ToString();

//create an instance of the MessageForm class
MessageForm myMessageForm = new MessageForm();

// set the total value which is now a property on message form
myMessageForm.Total = total;

//Display the form
myMessageForm.ShowDialog();

You would also have to add this new property to MessageForm class

// decide the visibility yourself (e.g. public vs internal)
internal string Total { get; set; }

You should also consider whether you want to pass total in the constructor as opposed to a property. If you have other existing classes calling MessageForm, this mayrequire some refactoring.

Amir
  • 486
  • 4
  • 14
  • It would probably be better to pass the value in the constructor to avoid the value not being updated to the UI or exceptions being thrown by a thread trying to set a value owned by another thread. Also for your MessageBox example, it's not a bad idea but specifying it is alternative instead of a direct answer would probably get you better views since it is not in accordance with the system used by the OP. (Using his' own custom view) – CalebB Apr 21 '15 at 03:51
  • I guess that's better, but you're making an assumption that his form has a `Total` property (and the way he was calling `ShowDialog`, a better guess might be that he has a constructor that takes a "total" string, so `MessageForm myMessageForm = new MessageForm(total);` would also be a good guess). Seems like we don't have enough info to give a good answer yet, aside from the obvious "remove the return statement" and "don't pass a string to `ShowDialog`". – Rufus L Apr 21 '15 at 03:54
  • @CalebB The OP never mentioned that this property will be required so additional information is necessary before making such assumptions. Use constructor if the arguments are required (without them the form cannot start to live). Use properties for the parameters which have an acceptable default value, so it's OK not to assign them at all. – Amir Apr 21 '15 at 03:55
  • Thanks Amir, your answer was helpful. Now I'm getting the error message "does not contain a definition for .Total (regarding the myMessageForm.Total). How do I add the accessor. also cannot convert double to string. Do I need a parse method for that or maybe I don't need to convert it? – dijit Apr 21 '15 at 03:59
  • @dijit You will need to modify your implementation of MessageForm. As others have mentioned, you can add a constructor that takes the total value and you decide what to do with Total. Other options is to just add a property that for Total (see updated comment) – Amir Apr 21 '15 at 04:00
  • @Amir An answer is a solution to the question and doesn't require you to follow the direct course as the op was on. Rufus is absolutely correct in his' advice. Your argument is invalid because you need to assign a value to a property whether it's through the constructor or manually after instantiating the view, using the constructing kills two birds with one stone, it's the whole concept of coding smart and accomplishing your task the best way possible not just any way possible. You'd be wise to listen to people such as Rufus with more experience than yourself, you might learn something. – CalebB Apr 21 '15 at 04:51
  • @CalebB I do not disagree that if value of Total was required in initializing the form, it would need to get injected in the constructor. However, we don't have enough information to assume that. MessageForm can be used in different parts of the application and this form may function just fine without the Total value. If this value is not required and this class is being used in other places, you will have to start refactoring for no reason and pass in a null value in the constructor. I think you both have lost track of the objective here. – Amir Apr 21 '15 at 13:15
  • @Amir heard of overloading? You don't need to restructure anything, just add an overload for the constructor and there you go. Work smart not hard. Google overloading and polymorphism, I think it'll be an eye opening experience. :) – CalebB Apr 21 '15 at 13:34
  • @CalebB once again, if the class can work w/o the dependency, a setter is fine. We really don't know the context we're dealing with here. – Amir Apr 21 '15 at 14:58