473,383 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Call Instance Method From Static Method (C#)

233 100+
I have two classes in the same namespace, Form1 and NewAccount. I am attempting to call an instance method that will reload a combobox on Form1. I have Form1 implemented as a singleton, and a static method in Form1, but I cannot access the instance method 'LoadAccounts()' which will reload the combobox.

Expand|Select|Wrap|Line Numbers
  1. public static void SetAccountID(string account)
  2.         {
  3.             Form1._instance.Show(); //checks for an open instance
  4.             Form1.LoadAccounts(); //not sure how to/if I can call instance function          
  5.         }

My understanding of the singleton pattern was that the call

Expand|Select|Wrap|Line Numbers
  1. Form1._instance.Show();
would check for an existing instance, and if one is not present, the class would be instantiated.
Expand|Select|Wrap|Line Numbers
  1. public Form1 Instance
  2.         {
  3.             get
  4.             {
  5.                 if (Form1._instance == null)
  6.                 {
  7.                     Form1._instance = new Form1();
  8.                 }
  9.  
  10.                 return Form1._instance;
  11.             }
  12.         }
  13.  
  14. private static Form1 _instance = null;
If this means that Form1 will always exist when this is called, I thought I would then be able to call any instance method of that class, which does not seem to be the case. Can I simply not call instance methods from a static method?
Dec 6 '11 #1
1 2270
mcfly1204
233 100+
After firing this line of code in the static method

Expand|Select|Wrap|Line Numbers
  1. Form1._instance.Show();
how do I access this instance, i.e., how do I call instance method for this instance? Am I even on the correct path, or is my line of thinking off on this?
Dec 6 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
6
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream...
6
by: z_learning_tester | last post by:
Hello, I am new to C# and have a quick question. Any replies much appreciated ;-) It is regarding static methods... Are they public by default? I mean it seems like for them to be usable, they...
6
by: rlvladbob | last post by:
Hi, I've try to access a static method using an instance instead of a class. public class test{ public static void ShowAText(string ThisText) { System.Console.WriteLine("->{0}",ThisText); }
3
by: Bob | last post by:
I have an abstract class Thing which has a static method Thing GetThing(). Class Something inherits from Thing. SomeThing supplies static info for GetThing to get stuff from the database to create...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
19
by: =?Utf-8?B?Sko=?= | last post by:
I have a logging component that I will access in other assemblies. So it was brought up to me that I should pass an instance around to these components instead of just making the logging class...
1
by: Mads Lee Jensen | last post by:
Hi i was wondering why i can call a class method, by double colon. without declaring it static. this is my test class Test_1 { public function hello() { echo 'Hello';
2
by: Mads Lee Jensen | last post by:
Sorry for posting this message again. Im new with the google groups and something seems wrong maybe it is because i changed my email after posting to a valid email i noticed the other didnt exists...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.