Hi!
Is the following program valid, although it compiles and executes successfully? -
// params modifier // used to declare array parameter
-
-
// value type, no constructor
-
-
using System;
-
-
class MyClass
-
{
-
public int minimumMethod(params int[] array)
-
{
-
int m;
-
-
if(array.Length == 0)
-
{
-
Console.WriteLine("Error: no arguments");
-
return 0;
-
}
-
-
m = array[0];
-
for(int i=1; i<array.Length; i++)
-
if(array[i] < m)
-
m = array[i];
-
-
return m;
-
-
}
-
}
-
-
class MainClass
-
{
-
int a = 5;
-
int b = 6;
-
int min;
-
-
static void Main()
-
{
-
MainClass mn = new MainClass();
-
-
MyClass mc = new MyClass();
-
-
mn.min = mc.minimumMethod(mn.a, mn.b);
-
-
Console.WriteLine("The minimum value is = {0} \n", mn.min);
-
-
mn.min = mc.minimumMethod(mn.a, mn.b, -7);
-
-
Console.WriteLine("The minimum value is = {0} \n", mn.min);
-
-
-
mn.min = mc.minimumMethod(6, 7, -7, 9, -7);
-
-
Console.WriteLine("The minimum value is = {0} \n", mn.min);
-
-
int[] args = {55, 7 , -7 , -88, 10};
-
mn.min = mc.minimumMethod(args);
-
Console.WriteLine("The minimum value is = {0} \n", mn.min);
-
}
-
}
-
-
/*
-
Output:
-
The minimum value is = 5
-
-
The minimum value is = -7
-
-
The minimum value is = -7
-
-
The minimum value is = -88
-
*/
6 1509
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
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.
Not at all! It was my own query. Anyways, thanks!
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.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Neil Zanella |
last post by:
Hello,
Suppose I have some method:
Foo::foo() {
static int x;
int y;
/* ... */
}
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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,...
|
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,...
|
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...
| | |