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 3 years ago.
Improve this question
Sorry for this noob question I forgot my lesson in c# so bear with me. I have this code
public class TestClass
{
int indexcounter = 3;
public int returnInteger()
{
int temporarystorage = indexcounter;
indexcounter --;
return temporarystorage;
}
}
I first stored value of indexcounter to temporary storage which is 3. So i can return temporarystorage value which is 3. but it returns the value of 2. What's happening here
Well, your code works just as you describe it should work.
BTW, the returnInteger method code can be simplified to return indexcounter--; - see Decrement operator -- for details.
Here's a quick demonstration:
public class TestClass
{
int indexcounter = 3;
public int returnInteger()
{
int temporarystorage = indexcounter;
indexcounter --;
return temporarystorage;
}
// I've added that property so that we can inspect the value of indexcounter outside this class
public int IndexCounter {get {return indexcounter;} }
}
public class Program
{
public static void Main(string[] args)
{
var a = new TestClass();
Console.WriteLine(a.IndexCounter); // prints 3
Console.WriteLine(a.returnInteger()); // prints 3
Console.WriteLine(a.IndexCounter); // prints 2
Console.WriteLine(a.returnInteger()); // prints 2
Console.WriteLine(a.IndexCounter); // prints 1
}
}
You can see a live demo on rextetser.
Related
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 4 months ago.
Improve this question
I'm trying to create a deep copy of object but the complier is throwing this error
As I'm beginner in C# and want to grip over these concepts of oop so your valuble answer explaining this concept would be highly appreciated
We would need to see the ctor of the quadratic class, it seems you are missing an overload that accepts one parameter.
Like so.
namespace ConsoleApp1
{
internal class Class1
{
public Class1()
{
}
public Class1(int input)
{
}
}
}
I assume that your Quadratic class doesn't have an overload for its own type to create deep copy. You have few options to create deep copy of your classes.
Create the class by setting its properties yourself in the constructor
Use the MemberwiseClone() method.
Serialize it and deserialize it again. You can do this with built-in JSON serializer that's located in the System.Text.Json namespace or with Protobuf. (Do NOT use BinaryFormatter to do this. Check this article for more information.)
public class Quadratic
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public Quadratic()
{
}
public Quadratic(Quadratic quadraticToCopy)
{
A = quadraticToCopy.A;
B = quadraticToCopy.B;
C = quadraticToCopy.C;
}
public Quadratic CreateDeepCopy1() => new Quadratic(this);
public Quadratic CreateDeepCopy2() => (Quadratic)this.MemberwiseClone();
}
And then you can use it like this;
var originalQuadratic = new Quadratic();
originalQuadratic.Input();
originalQuadratic.Display();
var deepCopiedQuadratic1 = originalQuadratic.CreateDeepCopy1();
var deepCopiedQuadratic2 = originalQuadratic.CreateDeepCopy2();
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm fairly new to c# and I'm trying to create a custom object, but they always seem to be empty.
public class Item
{
public string itemName {get;set;}
public int itemAmount {get;set;}
public Item (string name, int amount)
{
name = itemName;
amount = itemAmount;
}
}
public class Backpack : MonoBehaviour
{
void Start()
{
Item gold = new Item ("gold", 5)
}
}
When I try to get gold parameters I get null, 0. Should it work like that? I wanted to use it to quickly add items to a list, and right now I would have to change all of them manually with something like
gold.itemName = "gold"; gold.itemAmount = 5.
Can I do it in another way?
Your assignment in the constructor is the wrong way around, instead it should be:
public Item (string name, int amount)
{
itemName = name;
itemAmount = amount;
}
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 5 years ago.
Improve this question
There is a simple code:
class Test
{
public int number;
public void method()
{
Console.WriteLine("Something");
}
}
class Program
{
public static void Main(string[] args)
{
while(true)
{
Test obj=new Test();
obj.number=3;
}
}
}
This program sets the "number" of obj to 3 in every moment. But I would like to create a totally another, unique object with copy of content of the original object in every loop, automatically. If I make an object with same name, it will be overwritten.
Naturally I don't want to use it in an endless loop, it would be meaningless, but it was the easiest way to explain my problem.
You are creating new objects each iteration, but you only keep reference to the last created object.
public static void Main(string[] args)
{
var myOjects = new List<Test>();
int startIndex = 1;
while(true)
{
Test obj=new Test();
obj.number=startIndex;
myObjects.Add(obj);
startIndex = startIndex + 1;
if (startIndex > 5) break;
}
}
Now you can go through all objects in your list:
foreach (var obj in myObjects) obj.method();
There are a couple of ways that you can do this.
My first thought would be to use a factory method (it just returns an instance of a class). It would look something like this.
public Test TestFactory()
{
return new Test() { number = 3 };
}
Or add a constructor to the Test class that takes an instance of Test:
// inside Test class
public Test(Test that)
{
number = that.number;
}
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 6 years ago.
Improve this question
namespace OO_Assign_2_eDepot_
{
public partial class Driver
{
int driver_size = 1;
public string[] d_username = new string[driver_size] {"john"};
public string[] d_password = new string[driver_size] { "pass1" };
}
}
driver.size is not recognized as the variable size in the arrays
Choose one:
public partial class Driver
{
int driver_size = 1;
// Set size
public string[] d_username = new string[driver.size];
// Set value
public string[] d_password = new string[] { "pass1" };
}
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 7 years ago.
Improve this question
I tried to write my First WCF service and here i have some problems,
First,I create a WCF Project,Then i added Entity Model.After that i added IEmpService.svc file.then i'm going to get a List of Customers.
I follow THIS BLOG POST
IEmpService
[ServiceContract]
public interface IEmpService
{
[OperationContract]
List<Customer> GetAllCustomers();
}
EmpService
public class EmpService : IEmpService
{
public EmpDBEntities dbent = new EmpDBEntities(); // I can't create thisone inside GetAllCustomer method.
public List<Customer> GetAllCustomers
{
//var x = from n in dbent.Customer select n; // This is what i need to get but in here this program not recognize `var` also.
//return x.ToList<Customer>();
}
}
Can anyope please tell me which point i'm missing ? or whythis problem happend? how to solve this ?
Not really sure what your question is, but did you define "Customer" as a DataContract? If that's the object that your service returns, you need to define it so the client can use it.
I am still confused by your question, but I will attempt an answer.
Your Customer class needs to have a DataContract with DataMembers if you want to return it.
You probably saw this example:
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
Also, do not return List. Microsoft has List defined, but this is a web service - the rest of the world (apple, android, linux, php, etc) will not know how to interpret a List.
Instead, change your function's signature to an array of strings.
[OperationContract]
string[] GetAllCustomers();
You should remove public keyword if you want to create this inside GetAllCustomer method. Like this:
public List<Customer> GetAllCustomers()
{
EmpDBEntities dbent = new EmpDBEntities();
var x = from n in dbent.Students select n;
return x.ToList<Student>();
}