I'm playing with VS. I'm a rookie, It must be basic. I have created 2 classes and I puzzled with result. I'm using Visual Studio 2015 community edition.
I'm expect to receive at console :
myfirstClass
Class
first.
mysecondClass
Class
second.
I received :
myfirstClass
Class
_
class Program
{
public class mysecondClass
{
static public string myName ;
public mysecondClass()
{
Console.WriteLine("mysecondClass");
myName = "Class";
}
public static void Display()
{
Console.WriteLine(myName);
}
~mysecondClass()
{
Console.WriteLine("second.");
}
}
public class myfirstClass
{
public string myName;
public myfirstClass()
{
Console.WriteLine("myfirstClass");
myName = "Class";
}
public static void Display()
{
myfirstClass d = new Program.myfirstClass();
Console.WriteLine(d.myName);
}
~myfirstClass()
{
Console.WriteLine("first.");
}
}
static void Main(string[] args)
{
myfirstClass.Display();
mysecondClass.Display();
Console.ReadLine();
}
}
This is not a bug in Visual Studio. I think you have got two concepts wrongly.
Firstly, the finaliser of a class will not be called immediately after the object is out of scope. It will be called at a random time. It is quite unpredictable.
Therefore, this:
Console.WriteLine("first.");
is not printed.
The second thing is that constructors of a class is only called when you write new XXX(...) (or through reflection). Just calling a static method will not invoke the constructor.
In other words, these lines are never executed:
Console.WriteLine("mysecondClass");
myName = "Class";
You never wrote new mysecondClass().
When this line in the display method of mysecondClass executes:
Console.WriteLine(myName);
Since myName has not been assigned, it is null, and so nothing is printed.
class Program
{
public class mysecondClass
{
public string myName { get; set; }
public mysecondClass()
{
Console.WriteLine("mysecondClass");
myName = "Class";
}
public static void Display()
{
var mySecond = new mysecondClass();
Console.WriteLine(mySecond.myName);
}
~mysecondClass()
{
Console.WriteLine("second.");
}
}
public class myfirstClass
{
public string myName { get; set; }
public myfirstClass()
{
Console.WriteLine("myfirstClass");
myName = "Class";
}
public static void Display()
{
myfirstClass d = new myfirstClass();
Console.WriteLine(d.myName);
}
~myfirstClass()
{
Console.WriteLine("first.");
}
}
static void Main(string[] args)
{
myfirstClass.Display();
mysecondClass.Display();
Console.ReadLine();
}
}
try this
Related
Here is the code I currently have, the question follows after:
class Program
{
static void Main(string[] args)
{
var obj1 = new A();
obj1.DoIt();
obj1.SetFlyBehavior(new BehaviorB());
obj1.DoIt();
string input = Console.ReadLine();
}
};
class BaseOfA
{
protected ObjectBehavior behavior;
public void DoIt()
{
behavior.DoIt();
}
public void SetBehavior(ObjectBehavior ob) {
behavior = ob;
}
};
class A : BaseOfA {
public A() {
behavior = new BehaviorA();
}
}
interface ObjectBehavior {
void DoIt();
}
class BehaviorA : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: A");
}
}
class BehaviorB : ObjectBehavior {
void ObjectBehavior.DoIt() {
Console.WriteLine("Behavior: B");
}
}
Now my question is, in this case, how am I going to make it work so that I can assign both BehaviorA and BehaviorB to instance obj1 as long as they implement ObjectBehavior?
You are calling obj.SetFlyBehaviour this method is not defined anywhere. The method you define on BaseOfA is called SetBehaviour. Once that is fixed the code you gave compiles fine for me
using System;
namespace inheritance1
{
public class Employee
{
public string FirstName;
public string LastName;
public string Email;
public void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName);
}
}
public class FullTimeEmployee : Employee
{
public float YearlySalary;
}
public class PartTimeEmployee : Employee
{
public float HourlyRate;
}
public class Program
{
public static void main(String[] args)
{
FullTimeEmployee FTE = new FullTimeEmployee();
FTE.FirstName = "Max";
FTE.LastName = "Striker";
FTE.YearlySalary = 500000;
FTE.PrintFullName();
PartTimeEmployee PTE = new PartTimeEmployee();
PTE.FirstName = "king";
PTE.LastName = "Maker";
PTE.HourlyRate = 500;
PTE.PrintFullName();
}
}
}
It is Main with an upper case M, not main.
public static void Main(String[] args)
{
Main () and Other Methods (C# vs Java)
Every C# application must contain a single Main method specifying
where program execution is to begin. In C#, Main is capitalized,
while Java uses lowercase main.
a simple question :
public class class1
{
public string string1;
public class class2
{
public string string2
{
get{ string tmp = class1.string1; }
}
}
}
I want to be able to reach class1.string1 from class2.string2.get, but I cant. What would you recommend me to change, so that I can do that?
Thanx
Passing class1 reference to class2 in constructor:
public class class1 {
public string string1;
public class class2 {
private class1 _Reference;
public class2(class1 reference) {
if (reference == null) {
throw new ArgumentNullException("reference");
}
_Reference = reference;
}
public string string2 {
get { return _Reference.string1; }
}
}
}
Passing class1 reference to class2 after both classes have been created:
public class class1 {
public string string1;
public class class2 {
private class1 _Reference;
public class1 Reference {
set { _Reference = value; }
}
public string string2 {
get { return _Reference.string1; }
}
}
}
static void usage() {
var foo = new class1();
var bar = new class1.class2();
bar.Reference = foo;
string value = bar.string2;
}
There is no means of accessing a class from within a nested class that I know of. Class nesting doesn't lead to automatic instantiation of the surrounding class, it's just a (usually rather smelly) means of structuring your code.
You would either need a reference to an actual instance of Class1 inside Class2 or you'd need a static method on Class1.
Another way to accomplish this would be to use inheritance, but that's a whole different beast to tame:
public class Class1 {
protected String String1 { get; set; }
}
public class Class2 : Class1 {
public String String2 {
get {
String PropertyFromClass1 = base.String1;
// ...
}
}
}
That said: Your code wouldn't compile, string2's getter doesn't return anything. And please make yourself familiar with C#'s naming conventions.
Thanx for the suggestions. Due to the specific nature of the code, I had to solve this situation with a global public static class in another namespace.
Coming from Java I faced this "problem" when I started developing in C#.
As clearly explained by Dennis Traub and in this article in C# you can't access outer class members or methods. So you have to implement what in Java happens automatically:
class OuterClass {
string s;
// ...
class InnerClass {
OuterClass o_;
public InnerClass(OuterClass o) { o_ = o; }
public string GetOuterString() { return o_.s; }
}
void SomeFunction() {
InnerClass i = new InnerClass(this);
i.GetOuterString();
}
}
I only want this invoked when a property is set. Why is this not working?
[DirtyTrackingAttribute(AttributeTargetElements =
PostSharp.Extensibility.MulticastTargets.Property)]
class Program
{
public static string Test { get; set; }
static void Main(string[] args)
{
TestIt();
Test = "foo";
Console.ReadKey();
}
private static void TestIt()
{
Console.WriteLine("Real method called");
}
}
[Serializable]
public class DirtyTrackingAttribute : OnMethodInvocationAspect
{
public override void OnInvocation(MethodInvocationEventArgs eventArgs)
{
Console.WriteLine("Property invoked");
eventArgs.Proceed();
}
}
If you want the aspect to be applied on property setters only, you can filter the method name with the expression "set_*":
[DirtyTrackingAttribute(AttributeTargetMembers="set_*")]
PostSharp 1.* does not support explicitely properties; property accessors are considered as plain methods.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3Generics
{
class Program
{
static void Main(string[] args)
{
ScheduleSelectedItems sitems = new ScheduleSelectedItems("Yusuf");
ScheduleSelectedItemsList slist = new ScheduleSelectedItemsList();
slist.Items.Add(sitems);
Console.Write(slist.Items[0].ToString());
Console.ReadKey();
}
}
public class ScheduleSelectedItems
{
private string Ad;
public ScheduleSelectedItems(string ad)
{
Ad = ad;
}
}
public class ScheduleSelectedItemsList
{
public List Items;
public ScheduleSelectedItemsList()
{
Items = new List();
}
}
}
how can i add "yusuf" on my Console?
public class ScheduleSelectedItems
{
private string Ad;
public ScheduleSelectedItems(string ad)
{
Ad = ad;
}
public override string ToString()
{
return this.Ad;
}
}
What BFree said, with a slight modification to make it singular instead of plural:
public class ScheduleSelectedItem
{
private string Ad;
public ScheduleSelectedItem(string ad)
{
Ad = ad;
}
public override string ToString()
{
return this.Ad;
}
}
Additionally, you want an "Add" method for your list. While you're at it, why not just inherit from the list class:
public class ScheduleSelectedItemsList : List<ScheduleSelectedItem>
{
}
Or you could just create a type alias:
using ScheduleSelectedItemsList = List<ScheduleSelectedItem>;
Either way, you can use the new code like this:
class Program
{
static void Main(string[] args)
{
var slist = new ScheduleSelectedItemsList()
{
new ScheduleSelectedItem("Yusuf")
};
//write every item to the console, not just the first
slist.All(item => Console.Write(item.ToString()) );
Console.ReadKey();
}
}
Add this to your ScheduleSelectedItems class:
public override string ToString() {
return Ad;
}
That tells the system how such an object should be formatted.
You need to override the toString() method of ScheduleSelectedItems to return 'Ad'.