473,385 Members | 1,958 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,385 software developers and data experts.

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

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 1509
Frinavale
9,735 Expert Mod 8TB
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 Expert 4TB
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
Not at all! It was my own query. Anyways, thanks!
Aug 7 '07 #4
r035198x
13,262 8TB
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
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 Expert Mod 8TB
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
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
45
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...
3
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
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...
5
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...
2
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...
6
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...
6
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...
12
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...
0
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,...
0
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...

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.