I am trying to use a method to create an object and return that object to main(). Once in main I want to be able to access the properties of that object such as print Holly.age. VS does not recognize the object Holly in main() to have any properties. Is anyone able to tell what I am doing wrong or let me know if it's not something possible in c#.
Thanks in advance to anyone who gives there time to help.
class program
{
static void Main(string[] args)
{
object Holly = createobject(Holly, 21);
int hollyage = Holly.age; // This Line is the problem Holly.age is not recognized
// as anything
}
public static object createobject(string name, int i)
{
Cat Holly = new Cat(name, i);
return Holly;
}
public class Cat
{
public string name;
public int age;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public int Age
{
get { return this.age; }
set { this.age = value; }
}
public Cat(string name, int age)
{
this.name = name;
this.age = age;
}
}
VS does not recognize the object Holly in main() to have any properties
This is because you have declared Holly to be object rather than Cat, and similarly your create routine returns object rather than Cat.
To be able to use any of the properties of Cat you need to cast Holly back to Cat or better still, return Cat from your create routine. Unless you intend to extend your create routine to do more, you don't really need it and can simply do:
Cat Holly = new Cat("Holly", 21);
You also have both public fields and public properties in your Cat class. The fields should be made private, but that's not the cause of the problem.
Your method createobject returns an object. So you must first cast it back into Cat before you can access Age:
int hollyage = ((Cat)Holly).Age;
In addition your call to the createobject is wrong. It requests a string. Change to:
createobject("Holly", 21);
The correct way to write your code would be:
static void Main(string[] args)
{
//variable name in camelCase.
//"Holly" is a string
object holly = CreateObject("Holly", 21);
//Casting from object to Cat so you can access Age property
int hollyAge = ((Cat)holly).Age;
}
//Function names EachWorkCapitalized
public static object CreateObject(string name, int i)
{
Cat holly = new Cat(name, i);
return holly;
}
public class Cat
{
//If you implement your properties with the default get set behavior
//better use automatically implemented properties
public string Name { get; set; }
public int Age { get; set; }
public Cat(string name, int age)
{
Name = name;
Age = age;
}
}
You don't actually need public static object createobject()
You can simply remove that and change your Main to:
static void Main(string[] args)
{
Cat Holly = new Cat("Holly", 21);
int hollyage = Holly.age;
}
The problem is that Holly is defined as an object class, and object classes do not have an age or Age property.
You should change the line as:
Cat Holly = createObject("Holly", 21) as Cat;
Then the intellisense will recognize the Holly variable as being of type Cat.
You could always cast the object to the correct type to access it's members.
int hollyage = ((Cat)Holly).Age;
Related
Consider:
using System;
namespace TuristickaAgencija
{
public class World
{
protected char[] dest;
public World(char[] dest)
{
this.dest = dest;
}
}
class Client : World
{
private char[] name;
private char[] surname;
private int age;
private int noCounter;
private bool hasVehicle;
public Client(char[] name, char[] surname, int age, int noCounter, bool hasVehicle) : base(dest)
{
this.name = name;
this.surname = surname;
this.age = age;
this.noCounter = noCounter;
this.hasVehicle = hasVehicle;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
I'm getting 2 errors:
An object reference is required for the non-static field, method or property 'World.dest'
Missing partial modifier on declaration of type 'Program'
I want all classes to be in the same file.
It's unclear what dest you are trying to pass the constructor of the base class. You can't pass a reference to a field of an instance that has not yet been instantiated.
You should either add a parameter to Client constructor and pass this one to the base constructor:
public Client(char[] name, char[] surname, int age, int noCounter, bool hasVehicle,
char[] dest) : base(dest)
{
this.name = name;
this.surname = surname;
this.age = age;
this.noCounter = noCounter;
this.hasVehicle = hasVehicle;
}
Or you should pass some default value like for example null or an empty array:
public Client(char[] name, char[] surname, int age, int noCounter, bool hasVehicle) : base(default)
You need to pass a valid value in order to initialize dest.
dest is not defined in the csope of constructor yet, you are calling the base ctor and should intialzie it.
you can pass name, or surename or add another param and use it.
As for the partial error probably you have already a Program class defined somewhere else in your code and you cannot declare 2 classes with the same name.
hope this helps.
I am learning C# so I am still on the basics here. This is my code
class foo
{
protected int id;
protected string data;
static int nextId = 1;
public int Id
{
get { return id; }
}
public foo()
{
this.id = nextId++;
}
public foo(string somedata)
{
this.data = somedata;
this.id = nextId++;
}
}
This code works just fine for now. All objects will have a unique ID with them.
Problem is: I don't want the first ID to be 1, I want it to be the number on the first line of the file given as an argument to the application from the commandline. If no file is specified or the file does not exist, then it can be one. Is there a way to make a one-time method to set nextId so it cannot be tampered with outside of the class after it has been once set?
Duh... I guess this is the trick. Still, I'd like to know if there is some build in way to make variables that can only be set once.
public int nextId
{
set {if(nextId<1) nextId = value;}
}
You can implement static constructor, which assigns proper value to nextId before first usage of foo class:
using System.IO;
using System.Linq;
...
class foo
{
...
static int nextId;
// read-only property which however can be assigned in the constructor
public int Id {get;}
// This static costructor will be called once before 1st usage of foo class
static foo() {
//TODO: put the right file name here
string fileName = Environment.GetCommandLineArgs()[1];
nextId = int.Parse(File
.ReadLines(fileName)
.First());
}
public foo()
{
//Let's increment nextId in thread-safe manner
Id = Interlocked.Increment(ref nextId);
}
...
}
You can do this
class foo
{
private static int nextId = 1;
private static bool isNextIdSet;
public SetId(newId)
{
if (!isNextIdSet) nextId = newId;
isNextIdSet = true;
}
}
So the nextId can be set only once from outside the class.
UPDATE: if you prefer to use this with many variables, you can create a helper for this, such as
class SetOnlyOnce<T>
{
private bool isSet;
public T Value
{
get;
set
{
if (!isSet) Value = value;
isSet = true;
}
}
}
Then use it as
class Foo
{
public SetOnlyOnce<int> prop1 {get;set;}
public SetOnlyOnce<string> prop2 {get;set;}
}
class Bar
{
public Bar()
{
var foo = new Foo();
foo.prop1.Value = 2;
foo.prop1.Value = 3; // this doesn't set.
}
}
As an alternative to Dmitry's answer, you can define the initialization through a static method. I prefer avoiding static constructors unless multiple fields need to be initialized simultaneously.
using System.IO;
using System.Linq;
...
class foo
{
...
// The initialization will be called at some time before first use of nextId.
private static int nextId = GenerateInitialNextId();
// read-only property which however can be assigned in the constructor
public int Id {get;}
private static int GenerateInitialNextId() {
//TODO: put the right file name here
string fileName = Environment.GetCommandLineArgs()[1];
return int.Parse(File
.ReadLines(fileName)
.First());
}
public foo()
{
//Let's increment nextId is thread-safe manner
Id = Interlocked.Increment(ref nextId);
}
...
}
Here is my problem:
I want to output a value from a class when I call to its instance.
For example, I have a class like this:
class Car
{
public string name = null;
public int id;
public int horsepower;
public Car(int ID, string Name, int HorsePower)
{
this.id = ID;
this.name = Name;
this.horsepower = HorsePower;
}
}
I want the output will be "aventador lp700-4" when I have a program like this:
class Program
{
static void Main(string[] args)
{
Car car = new Car(1, "aventador lp700-4", 700);
////////////// I want the output will be "aventador lp700-4" /////////////////////
Console.WriteLine(car);
///////////////////////////////
Console.Read();
}
}
I find some dll library could do that, but I don't know how to to.
Console.WriteLine(object) wants to get a string for the object passed in; there are a few different ways it can do that, but the default (in the absence of you telling it something more specific) is that it is just going to call .ToString() on the argument. So: you need to override the ToString() method on Car, to tell it what you want to use to represent that type as a string:
class Car
{
// ... your existing code
public override string ToString() { return name; }
}
Override the ToString method in your car class
public override string ToString(){
return name;
}
How can I pass an object of a "MyClass" (C#) by Parameter-by-Value to a method? example:
MyClass obj = new MyClass();
MyClass.DontModify(obj); //Only use it!
Console.Writeline(obj.SomeIntProperty);
...
public static void DontModify(MyClass a)
{
a.SomeIntProperty+= 100;// Do something more meaningful here
return;
}
By default object types are passed by value in C#. But when you pass a object reference to a method, modifications in the object are persisted. If you want your object to be inmutable, you need to clone it.
In oder to do it, implement the ICloneable interface in your class. Here is a mock example of how to use ICloneable:
public class MyClass : ICloneable
{
private int myValue;
public MyClass(int val)
{
myValue = val;
}
public void object Clone()
{
return new MyClass(myValue);
}
}
By default, it is passed by value. However, you're passing the object reference by value, which means you can still edit values within the object.
In order to prevent the object from being able to change at all, you would need to actually clone the object prior to passing it into your method. This would require you to implement some method of creating a new instance that is a copy of your original object, and then passing in the copy.
public static void DontModify(MyClass a)
{
MyClass clone = (MyClass)a.Clone();
clone.SomeIntProperty+= 100;// Do something more meaningful here
return;
}
You could create a Clone method on your object to pass the return value to your method. C# cannot pass reference types by value so this might be a good alternative.
public MyClass CreateClone()
{
return new MyClass() { SomeIntProperty = this.SomeIntProperty };
}
class Program
{
static void Main(string[] args)
{
Person p1 = new Person()
{
Name = "Alsafoo",
Address = new Address()
{
City = "Chicago"
}
};
Person p2 = new Person(p1.Address);
p2 = p1.GetClone(CloningFlags.Shallow);
p2.Name = "Ahmed";
p2.Address = new Address(){City = "Las Vegas"};
Console.WriteLine("p1 first name: {1} --- p1 city: {2} {0}p2 first name: {3} ---- p2 city: {4}",
Environment.NewLine, p1.Name, p1.Address.City, p2.Name, p2.Address.City);
Console.ReadKey();
}
}
public class Person
{
public Person()
{}
public Person(Address a)
{
Address = a;
}
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string City { get; set; }
}
Download this extension
https://www.nuget.org/packages/CloneExtensions/1.2.0
Created a Extention method
using System.Text.Json;
namespace Student.Utilities
{
public static class CloneExtension
{
public static T Clone<T>(this T cloneable) where T : new()
{
var toJson = JsonSerializer.Serialize(cloneable);
return JsonSerializer.Deserialize<T>(toJson);
}
}
}
Now, while calling, call it like this to pass the clone to another method:
public void CreateStudent(Student student)
{
Student clonedStudent = student.Clone<Student>();
_repository.CreateStudent(clonedStudent);
}
I am a C++ programmer, now working on a C# project.
I am trying to understand in the below snippet why the value of string does not change even though the function is changing its value, I thought it is an object and would be passed as a reference.
public class TestClass
{
public TestClass(String passedStr)
{
passedStr = "Change me";
}
}
class Program
{
static void Main(string[] args)
{
String aString="I am what i am";
TestClass obj = new TestClass(aString);
Console.WriteLine(aString);
}
}
But behavior with user defined classes are different.
public class TestClass
{
private int x;
public int ID
{
get
{
return x;
}
set
{
x = value;
}
}
public TestClass(int a)
{
x = a;
}
}
public class Tester
{
public Tester(TestClass obj)
{
obj.ID = 999;
}
}
class Program
{
static void Main(string[] args)
{
TestClass obj = new TestClass(555);
Tester tester = new Tester(obj);
Console.WriteLine(obj.ID);
}
}
Let's go back to basics.
A variable is a storage location.
A variable of type string is a storage location that stores either null, or a reference to a string.
"passedStr" and "aString" are different variables.
When you call "new TestClass(aString)" you create a new storage location for "passedStr" and copy the contents of "aString" into it. You now have two variables that have the same content: a reference to a string.
Inside the constructor you change the value stored in the storage location for "passedStr". "aString" remains the same.
The "ref" and "out" keywords in C# mean "make the formal parameter and the argument aliases of each other". In that case you have only one variable with two different names. When you change one of them the other one changes as well, because they are the same variable.
No, it's passed by value; there's no ref keyword.
It's passing a reference type (here, a class) by value (no ref keyword), just like passing a copy of a pointer in C++. You're reassigning the pointer, not the actual data (which you can't do with strings anyway).
If you need pass-by-reference, try:
public TestClass(ref String passedStr)
{
passedStr = "Change me";
}
...
TestClass obj = new TestClass(ref aString);
Strings are passed by reference but the pointer is passed by value in C#. If you want to pass the string by reference you'll have to make use of the ref key word.
For example:
public class TestClass
{
public TestClass(ref string passedStr)
{
passedStr = "Change me";
}
}
class Program
{
static void Main(string[] args)
{
string aString="I am what i am";
TestClass obj = new TestClass(ref aString);
Console.WriteLine(aString); // "Change me"
}
}
passedStr is not the string, but a constructor parameter that holds a reference to the string. All your TestClass constructor is doing is changing the string that this parameter references. It has no effect outside the constructor.
What you are doing in the constructor, is that you are assigning a new String literal to the local variable passedStr.
In C, the equivalent function would look something like this:
Testclass constructor_testclass(char* passedStr) {
passedStr = "Change me";
}
I think it's obvious that this function does not change the value of the char* in the calling function.
The object I am what i am is being passed by reference, but the reassignment is to a local variable. You don't change the original object, but instead assign a new object (Change me) to the location of the argument.
public class Employee
{
public string ID { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("ID = {0} Name = {1}", ID, Name);
}
}
public class TestClass2
{
public TestClass2(Employee e)
{
e.ID="007";
e.Name="james";
}
}
static void Main()
{
Employee e = new Employee();
e.ID = "0";
e.Name = "nobody";
TestClass2 t = new TestClass2(e);
Console.WriteLine(e); //Output ID = 007 Name = James
}
strings are passed by reference but the pointer is passed by value in C#
Parameter passing in C# by Jon Skeet