473,748 Members | 9,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#, can Main()' s class have instance / static variable? If so, how do you access?

4 New Member
Hi!

Is the following program valid, although it compiles and executes successfully?
Expand|Select|Wrap|Line Numbers
  1. // params modifier // used to declare array parameter
  2.  
  3. // value type, no constructor
  4.  
  5. using System;
  6.  
  7. class MyClass
  8. {
  9.     public int minimumMethod(params int[] array) 
  10.     {
  11.         int m;        
  12.  
  13.         if(array.Length == 0)
  14.         {
  15.             Console.WriteLine("Error: no arguments");
  16.             return 0;
  17.         }
  18.  
  19.         m = array[0];
  20.         for(int i=1; i<array.Length; i++)
  21.             if(array[i] < m)
  22.                 m = array[i];
  23.  
  24.         return m;
  25.  
  26.     }
  27. }
  28.  
  29. class MainClass
  30. {
  31.     int a = 5;
  32.     int b = 6;
  33.     int min; 
  34.  
  35.     static void Main()
  36.     {  
  37.         MainClass mn = new MainClass();
  38.  
  39.         MyClass mc = new MyClass();
  40.  
  41.         mn.min = mc.minimumMethod(mn.a, mn.b); 
  42.  
  43.         Console.WriteLine("The minimum value is = {0} \n", mn.min);
  44.  
  45.         mn.min = mc.minimumMethod(mn.a, mn.b, -7);
  46.  
  47.         Console.WriteLine("The minimum value is = {0} \n", mn.min);
  48.  
  49.  
  50.         mn.min = mc.minimumMethod(6, 7, -7, 9, -7);
  51.  
  52.         Console.WriteLine("The minimum value is = {0} \n", mn.min);
  53.  
  54.         int[] args = {55, 7 , -7 , -88, 10}; 
  55.         mn.min = mc.minimumMethod(args); 
  56.         Console.WriteLine("The minimum value is = {0} \n", mn.min);
  57.     }
  58. }
  59.  
  60. /* 
  61. Output:
  62. The minimum value is = 5
  63.  
  64. The minimum value is = -7
  65.  
  66. The minimum value is = -7
  67.  
  68. The minimum value is = -88
  69. */
Aug 6 '07 #1
6 1535
Frinavale
9,735 Recognized Expert Moderator Expert
Hi!

Is the following program valid, although it compiles and executes successfully?
Hi Recherche,

I've moved your question to the .NET Forum.
The .NET Articles section is reserved for "how-to" articles and other useful information and is not meant for questions. You should post your question in the .NET Forum section in the future to receive help with your problem. (Blue Menu: Forums->.NET)

Your question sounds as if you were asking us to answer a homework question for you. It is up to you to decide if the program is valid or not according to what its supposed to accomplish.
The experts on this site are more than happy to help you with your problems but they cannot do your assignment for you.
Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

MODERATOR
Aug 7 '07 #2
Plater
7,872 Recognized Expert Expert
I maintain any program that "works" is "valid".
And since Frinny pointed out this could be a homework I can't be detailed, but I would suggest looking at where the arguments get passed into the function are handled and make some decisions.
Aug 7 '07 #3
recherche
4 New Member
Not at all! It was my own query. Anyways, thanks!
Aug 7 '07 #4
r035198x
13,262 MVP
Not at all! It was my own query. Anyways, thanks!
You still haven't told us whether you think it's valid or not and your reasons.
Aug 7 '07 #5
recherche
4 New Member
Sorry for the tardy reply!

Maybe, I should have rephrased my query. There's no doubt that it is valid, because in case of programs that contain a single class, the instance / static variable is contained in the Main()'s class. Hence Main()' s class can have instance / static variable. The reason for my query is that I never came across a program (with more than 1 class) where the Main()' s class have instance / static variable! Perhaps, a convention or rule?

Furthermore, I don't know any forum where I can ask such "unconventional " queries as I am a self-taught person.
Aug 14 '07 #6
Frinavale
9,735 Recognized Expert Moderator Expert
Sorry for the tardy reply!

Maybe, I should have rephrased my query. There's no doubt that it is valid, because in case of programs that contain a single class, the instance / static variable is contained in the Main()'s class. Hence Main()' s class can have instance / static variable. The reason for my query is that I never came across a program (with more than 1 class) where the Main()' s class have instance / static variable! Perhaps, a convention or rule?

Furthermore, I don't know any forum where I can ask such "unconventional " queries as I am a self-taught person.
A static variable retains its value during program execution.

When you declare one within your method it becomes a local variable to that method (or class)...it's only accessible there.

Once that method is finished executing the variable retains its value for the next time the method is called.

To declare one in Main() would be kind of redundant, because main is only called once and the lifetime of a static variable lasts only until the program's life ends.

(maybe try posting such queries in the Software Development forum?)

-Frinny
Aug 14 '07 #7

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

Similar topics

30
2265
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
45
3613
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
3
17907
by: Ray Stevens | last post by:
How do you access properties of the main program's class from another form? There does not apear to be an instance variable that can be used.
7
3386
by: Terry | last post by:
I have a Mainform with a Statusbar. When opening another form or doing some processing I want to display info in the Statusbar of the Mainform. I have read a lot of articles on this & have come up with the code below. It seems to work(!!!) in that when coding the second form I can see the DisplayStatusMsg of the main form. During debug the code runs through & seemingly executes the call without error. But!...The message is not displayed....
5
2056
by: Scott Starker | last post by:
Is there anyway to do this? Every time any button is click inside class Form1, MyButtomArray.CharArray (MyButtomArray is a class) gets set (or reset) (bool). Once this is done the class TEC gets executed. I want to access MyButtonArray.CharArray to get the bool values in TEC. Any ideas? Scott
2
2647
by: Rafe Culpin | last post by:
Does anyone please know of a way to access static methods of a class, when the name of that class is held in a variable? I have several classes (PHP5) which all have identically named methods and members. I want to pass the name of one of the classes to an included file which does some standard operations using those methods and members. (So several different programs can include that file, each passing a different class name.)
6
1914
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context. Now, every now and then I get this...
6
3193
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the singleton is initialized as a static variable , it seems there is some threading issue . Is it the issue during singleton initialization only , or during the access also? If the singleton is per thread basis (then no more singleton though ), and...
12
3116
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is only ever ONE instantiation of this class, and this outside method in a separate thread has to access it. How do i do this?
3
3939
by: Jon | last post by:
My main form opens up another form, and from this other form, I'd like to access things in the main form. The problem is that although I know the name of the class of the main form (FormMain) I don't know the name of the instance of it since it was generated by the VS C# Express 2005 designer. In program.cs, I notice that there is the line: Application.Run(new FormMain()); I guess I could replace this with: FormMain fMain = new FormMain()...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.