I have a public class EmailHelp.
In this class there is a public String called doIt.
How do i execute this public String DoIt from within let´s say a onClick event?
public class EmailHelp{
-----
public String DoIt {
------
public void crxExecute () {
execute DoIt
finish();
Thank you in advance.
Assuming that "DoIt" is not a static method, you'd need to create an instance of the EmailHelp class, and call that method:
public void Button_OnClick(object sender, EventArgs e)
{
var emailHelper = new EmailHelp();
emailHelper.DoIt();
}
The fact that it's not obvious that you need to instantiate EmailHelper may indicate that the method should in fact be a static one. If it were a static method, calling it would look like this:
public void Button_OnClick(object sender, EventArgs e)
{
EmailHelp.DoIt();
}
Related
In a project created in Windows 7 and Visual Studio 2012, I have a form with the following code:
public partial class Form1 : Form
{
private Form2 m_form2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
m_form2 = new Form2();
m_form2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Refreshing form1");
Refresh();
m_form2.Refresh();
}
}
Form2 has the following method:
public override void Refresh()
{
MessageBox.Show("Refreshing Form2");
base.Refresh();
}
Form2 contains an instance of the RefreshTestControl, which contains this method:
public override void Refresh()
{
MessageBox.Show("Control is being refreshed.");
base.Refresh();
}
I expected that my control's overridden Refresh() method would get called automatically when Form2.Refresh() is called, but it's not. Why not? Am I doing something wrong, or do I just not understand what happens when a form is refreshed?
There is a final static member in class ToBeTestClass:
protected final static LMSServiceHelper externalService;
I mock it with
#Mock
protected LMSServiceHelper externalService;
Then I want to get different values in different test methods:
public void testMethod1() {
PowerMockito.when(externalService.getSomething).thenReturn("aaa");
}
public void testMethod2() {
PowerMockito.when(externalService.getSomething).thenReturn("bbb");
}
public void testMethod3() {
PowerMockito.when(externalService.getSomething).thenReturn("ccc");
}
However, I can't get "bbb" or "ccc" whereas always get "aaa". It seems when I set the return value first time, and it will never changes.
Anyone has met this?
#Before
public void setUp() {
Mockito.when(externalService.getSomething)
.thenReturn("aaa")
.thenReturn("ccc")
.thenReturn("ccc"); //any subsequent call will return "ccc"
}
How to tell a Mockito mock object to return something different the next time it is called?
Reset your mocked object as shown below then you will see different values
public void testMethod1() {
PowerMockito.when(externalService.getSomething).thenReturn("aaa");
Mockito.reset(externalService);
}
public void testMethod2() {
PowerMockito.when(externalService.getSomething).thenReturn("bbb");
Mockito.reset(externalService);
}
public void testMethod3() {
PowerMockito.when(externalService.getSomething).thenReturn("ccc");
Mockito.reset(externalService);
}
public static bool WriteBeamDataToFile(string Filename, List<Part> Parts)
{
// Open a Streamwriter to write data to the specified Filename
using (StreamWriter TeklaDataWriter = new StreamWriter(Filename))
{
// Connect to the Currently Open Tekla Model
Model Model = new Model();
foreach (Part CurrentPart in Parts)
{
if (CurrentPart != null)
{
string Name = CurrentPart.Name;
string Profile = CurrentPart.Profile.ProfileString;
string Material = CurrentPart.Material.MaterialString;
string Finish = CurrentPart.Finish;
TeklaDataWriter.WriteLine(Name + "," + Profile + "," + Material + "," + Finish);
}
}
}
return File.Exists(Filename);
}
Example:
private void button1_Click(object sender, EventArgs e)
{
How to call above method here?
}
private void button1_Click(object sender, EventArgs e) {
private bool isFileExists;
List<Parts> partsList = new List<Parts>();
isFileExists = WriteBeamDataToFile("example.txt",partsList)
if(isFileExists){
//do something..
}
}
Method above is mark as static. That's why you faced some issue.
Static method could be call from Class it' self.
While non static methods could be called from class instance.
See example:
class MyClass {
//static method
public static void Method1() {}
//non static method
public void Method2() {}
}
class MyForm:Form {
...
private void button1_Click(object sender, EventArgs e)
{
//here we call static method of MyClass
MyClass.Method1();
}
//or
private void button1_Click(object sender, EventArgs e)
{
// Here we create an instance of MyClass
var class = new MyClass();
// and call non static Method
class.Method2();
}
}
}
I have a public method in project and here is that method.
public bool SaveDocument(string customer)
{
//some codes
}
I have another project in same solution with this method
private void buttonSaveReport_Click(object sender, EventArgs e)
{
}
button click event I need to pass 3 parameters (name,title,isbn) to SaveDocument() method and override it.I am having a difficulty with implementing this.Trying with this and can anyone help me out to complete it.
public class Ext : DocumentManager.DocumentManager
{
public override void SaveDocument()
{
base.SaveDocumentCustomer(customer);
}
}
Thanks
I have a javafx design in the file javafx.fxml where the root element has the following attribute
fx:controller="de.roth.jsona.javafx.ViewManagerFX"
This controller class has a singleton machanism and is binded with some ui-elements.
public class ViewManagerFX {
private static ViewManagerFX instance = new ViewManagerFX();
#FXML
private Slider volumeSlider;
#FXML
private Label volumeLabel;
public IntegerProperty volumeValue = new SimpleIntegerProperty();
#FXML
private TabPane musicTabs;
public List<StringProperty> tabNames = new ArrayList<StringProperty>();
public static ViewManagerFX getInstance() {
return (instance);
}
public void initialize() {
// Volume
volumeSlider.valueProperty().bindBidirectional(volumeValue);
volumeLabel.textProperty().bindBidirectional(volumeValue, new Format() {
#Override
public StringBuffer format(Object obj, StringBuffer toAppendTo,
FieldPosition pos) {
toAppendTo.append(obj);
toAppendTo.append("%");
return toAppendTo;
}
#Override
public Object parseObject(String source, ParsePosition pos) {
return null; // no need to be implemented
}
});
volumeValue.set(Config.getInstance().VOLUME);
}
public void addMusicFolderTab(final String t, final ArrayList<MusicListItem> items) {
Platform.runLater(new Runnable() {
#Override
public void run() {
Tab m = new Tab("Test Tab");
musicTabs.getTabs().add(0, m);
}
});
}
}
The method addMusicFolderTab is called from a thread that is used to scan files and directories.
In the initialize method I can access the ui-elements but in the method addMusicFolderTab, that is called from the filescanner-thread, the variable musicTabs is null. Here is the exception:
java.lang.NullPointerException
at de.roth.jsona.javafx.ViewManagerFX$3.run(ViewManagerFX.java:110)
I have no clue, why I can't access the TabPane from outside the initialize method.
Aside from the many questionable patterns used here, the problem is that your ViewManagerFX singleton (besides not being a singleton) never has its instance set.
When using FXML, the Controller is created and loaded dynamically by Reflection from the FXMLoader.
What happens is that by calling ViewManagerFX.getInstance(), you access the a different controller than the one created by the FXMLoader. The instance you access is the one created here:
private static ViewManagerFX instance = new ViewManagerFX();
The quickest way to solve the issue is to set the instance in the initialize() since it's called by the FXMLoader on the instance created by the FXMLoader.
public void initialize() {
instance = this;
// Volume
...
}