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 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
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.
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.
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
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 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...
|
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 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....
|
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
| |
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.)
|
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...
|
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...
|
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?
|
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()...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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();...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |