Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to develop a class library in C# whose methods I would call in a hierarchical way, like this:
using MyProduct.Common;
protected void Page_Load(object sender, EventArgs e)
{
Utils myUtils = new Utils();
int res = myUtils.Numbers.Add(1, 2);
string text = myUtils.Text.ToUpper("test"); //***
}
Is it possible to do, using Namespaces, classes, interfaces, whatever?
UPDATE:
I have my library like this, but it doesn't work, as I can't "reference a type through an expression" (line with *** above)
namespace MyProduct.Common
{
public class Utils
{
public static class Text
{
public static string ToUpper(string s)
{
return s.ToUpper();
}
}
}
}
UPDATE 2:
Ok, it seems I need to clarify...
I have this MyProduct.Common.dll:
namespace MyProduct.Common
{
public static class Utils
{
public static class Text
{
public static void ToUpper(string s)
{
return s.ToUpper();
}
}
public static class Number
{
public static void Add(int a, int b)
{
return (a + b);
}
}
}
}
and I have my project:
using MAD.Comum;
...
protected void Page_Load(object sender, EventArgs e)
{
string x = Utils.Text.ToUpper("aa"); //this works
string res = Utils.Number.Add(1, 2); //this works
}
and now 2 questions:
- is it possible to separate classes Number and Text in 2 files?
- is this a good design?
Of course, you can do something like this:
namespace MyProduct.Common.Utils
{
public static class Numbers
{
public static int Add(int n, int m)
{
return n + m;
}
}
public static class Text
{
public static string ToUpper(string str)
{
return str.ToUpper();
}
}
}
And you use it like this:
MyProduct.Common.Utils.Numbers.Add(1, 2);
Hope this helps.
Cheers
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am getting this error:
"The variable 'a' is assigned but its value is never available"
namespace test
{
public class Program
{
static void Main(string[] args)
{
test();
Console.WriteLine(a);
Console.ReadLine();
}
public static void test()
{
int a = 11;
}
}
}
You are talking about "methods" not classes.
You can read more here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
But in the mean time, try this:
namespace test
{
public class Program
{
static void Main(string[] args)
{
int a = test();
Console.WriteLine(a);
Console.ReadLine();
}
public static int test()
{
int a = 11;
return a;
}
}
}
class Program
{
static void Main(string[] args)
{
int a = test();
Console.WriteLine(a);
Console.ReadLine();
}
public static int test()
{
int a = 11;
return a;
}
}
You should probably get this error from your code. "The name 'a' does not exist in the current context"
As you do not get the variable "a" out from your method and try to access it.
Try the above and see if it helps.
This is a class and a method, what you have there. NOT 2 classes.
What do you want to do exactly?
The a variable does not exist where you want to print it.
Something like this?
static void Main(string[] args)
{
test();
Console.ReadLine();
}
public static void test()
{
int a = 11;
Console.WriteLine(a);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
It seems that I am getting a null/zero for my passed param n&m and their values are not updated in AHPmodel class after being assigned locally!
{
public partial class Weighing_Factors_Pairwise : Form
{
private Form4 frm4;
public static int n { get; private set; }
public static int m { get; private set; }
AHPModel model = new AHPModel(n,m);
public Weighing_Factors_Pairwise(Form4 frm4)
{
InitializeComponent();
this.frm4 = frm4;
n = frm4.checkbox.Count;
m = frm4.checksys.Count;
}
public AHPModel(int n, int m)
{
superDim = n * m;
nCriteria = n;
mChoices = m; }
}
private void button5_Click(object sender, EventArgs e)
{
model.CalculateModel();
GeneralMatrix calcCriteria = model.CalculatedCriteria;
GeneralMatrix results = model.ModelResult;
GeneralMatrix choices = model.CalculatedChoices;
}
}
I think this is because you first Create an instance of AHPModel where n, m are both null. you should make some replacement in order of your statements, try this one:
AHPModel model;
public Weighing_Factors_Pairwise(Form4 frm4)
{
InitializeComponent();
this.frm4 = frm4;
n = frm4.checkbox.Count;
m = frm4.checksys.Count;
APModel = new AHPModel(n,m);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to write a simple program in C# to display the sum and the average of the numbers 1 to 100. the output should look like...
the sum is 5050
the average is 50.5
I cant seem to get it to work properly though, the return value only returns the sum not the wording before it, I have tried other ways for it to display the message and sum and ave but they arent working. I am trying to do this using the model view view controller method, but cant understand where I am going wrong and how to get it to display the above result. my code is below.
class SumAndAverage
{
public float sum = 0;
public float ave = 0;
public float SumAndAve()
{
for (int i = 1; i <= 100; i++)
{
sum = sum + i;
ave = sum / i;
}
return sum + ave;
}
}
}
class SumAndAverageController
{
IView view;
SumAndAverage sumAndAverage;
public SumAndAverageController(IView theView, SumAndAverage theSumAndAverage){
view = theView;
sumAndAverage = theSumAndAverage;
}
public void Go()
{
view.Start();
//mAndAverage.SetNumber(view.GetString("Please enter a number"));
view.Show(sumAndAverage.SumAndAve());
//view.Show(sumAndAverage.Result());
view.Stop();
}
}
class ConsoleView : IView
{
public void Start()
{
Console.Clear();
}
public void Stop()
{
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
public void Show<T>(T message)
{
Console.WriteLine(message);
}
}
interface IView
{
void Start();
void Stop();
void Show<T>(T message);
}
class Program
{
static void Main(string[] args)
{
new SumAndAverageController(new ConsoleView(), new SumAndAverage()).Go();
}
}
using System;
using System.Linq;
namespace SumAndAverage
{
class Program
{
static void Main(string[] args)
{
var data = Enumerable.Range(1, 100);
Console.WriteLine("the sum is " + data.Sum());
Console.WriteLine("the average is " + data.Average());
}
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I know there is some posts on that but I didn't find the answer I'm looking for, and its simple.
I know calling a method static or not (instanciation (OOP)).
Example, using static:
using System;
namespace Projet_Test
{
class surfacesCalcul
{
public static int calc_air(int a, int b){
int result = a * b;
return result;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine(surfacesCalcul.calc_air(4,5));
}
}
}
You call the class(surfacesCalcul).Method and you can access the Method.
--
Normal, using OOP:
using System;
namespace Projet_Test
{
class surfacesCalcul
{
public int calc_air(int a, int b){
int result = a * b;
return result;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
surfacesCalcul calculMeThis = new surfacesCalcul();
Console.WriteLine(calculMeThis.calc_air(4,5));
}
}
}
You create an instantiation and then you can access the methods in the class.
--
[My Question]
Which one is the best, I'm just wondering why using static method more than from an instance or the inverse. Is there a real reason or both are good. Is there a best time to use one more than the other?
I'll argue both sides and you can choose.
Consider your first example with a static function:
namespace Projet_Test
{
class surfacesCalcul
{
public static int calc_air(int a, int b){
int result = a * b;
return result;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine(surfacesCalcul.calc_air(4,5));
}
}
}
This is fine if you never care about overriding or any kind of inheritance.
Now consider this example:
class surfacesCalcul
{
public virtual int calc_air(int a, int b){
int result = a * b;
return result;
}
}
class surfacesCalcul2 : surfacesCalcul {
public override int calc_air(int a, int b)
{
var g = base.calc_air(a, b);
return g*2;
}
}
/*
* ##########################
* ##########################
*/
class MainClass
{
public static void Main (string[] args)
{
var calculMeThis = new surfacesCalcul2();
Console.WriteLine(calculMeThis.calc_air(4,5));
}
}
You're not changing the "correctness" of the program at all just allowing polymorphism to do its thing.
This example it really to trivial for it to really matter. I would prefer the route with the class since static methods can't have instance variables and you can't do any kind of dependency injection, which makes unit testing so much easier. The object instantiation also allows for different instances to contain different state information where static can't contain any instance specific data.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got a C# project where I need to have a base class and two sub classes and I want the sub classes to be private to the base classes. This is how it looks like:
public class MyBaseClass
{
public void doMyWork()
{
doSubClass1();
doSubClass2();
}
}
private class MySubClass1
{
public void doSubClass1()
{
//Do stuff
}
}
private class MySubClass2
{
public void doSubClass2()
{
//Do stuff
}
}
How i can i get this to work?
Does it hurt if the classes themselves are not-so-private but still wrapped in your base & unaccessible outside?
public class MyBaseClass
{
private readonly MySubClass1 _sub1 = new MySubClass1();
private readonly MySubClass2 _sub2 = new MySubClass2();
public void DoMyWork()
{
_sub1.DoSubClass1();
_sub2.DoSubClass2();
}
}
public class MySubClass1
{
public void DoSubClass1() { /* Do stuff */ }
}
public class MySubClass2
{
public void DoSubClass2() { /* Do stuff */ }
}
If this is not good enough and you need those in separate files, as you stated, then you can use partial classes, like so.
Class1.cs file
public partial class MyBaseClass
{
public void DoMyWork()
{
(new MySubClass1()).DoSubClass1();
(new MySubClass2()).DoSubClass2();
}
}
Class2.cs file
public partial class MyBaseClass
{
private class MySubClass1
{
public void DoSubClass1() { /* Do stuff */ }
}
}
Class3.cs file
public partial class MyBaseClass
{
private class MySubClass2
{
public void DoSubClass2() { /* Do stuff */ }
}
}
But still it's not quite clear what you are trying to achieve here.
If you put your subclasses inside of the base class, the base class can access them. To use your example, it would be like this:
public class MyBaseClass
{
public void doMyWork()
{
new MySubClass1().doSubClass1();
new MySubClass2().doSubClass2();
}
private class MySubClass1
{
public void doSubClass1()
{
//Do stuff
}
}
private class MySubClass2
{
public void doSubClass2()
{
//Do stuff
}
}
}
Note that all I did was move the last } to the bottom.