C# array size not defined by integer variable [closed] - c#

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" };
}

Related

c# copy only integer value of variable [closed]

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.

C# - Add node to serialized class [closed]

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 4 years ago.
Improve this question
I'm very new to C# and please excuse me if i'm asking you stupid question.
I don't know how to add node to serialized class.
Here is my code:
namespace DuloGames.UI
{
[Serializable]
public class UISpellInfo
{
public int ID;
public string Name;
public Sprite Icon;
public string Description;
public float Range;
public float Cooldown;
public float CastTime;
public float PowerCost;
public float test;
[BitMask(typeof(UISpellInfo_Flags))]
public UISpellInfo_Flags Flags;
}
}
Here is how i try add new node to the serialized class above from another class:
using DuloGames.UI;
public class Character : MonoBehaviour
{
private void AddToNode()
{
UISpellInfo serializedNode = new UISpellInfo();
serializedNode.Add(new UISpellInfo()
{
ID = 1,
Name = "test",
Description = "description"
});
}
}
This is how i try to add new node to the serialized class but it seems i'm not doing it correctly. Can you please help me out ?
I'm not sure what your are trying to do but you UISpellInfo class is just a POCO that has no Add method right?
What are you trying to do.. I think you need to have a List of UISpellInfo object before you can add something to it?
List<UISpellInfo> nodes = new List<UISpellInfo>();
nodes.Add(new UISpellInfo{ID = 1, Name = "test", Description = "description"});
This should work i think.
But then again what will you do with serializedNode, i don't see you are doing anything with it?

global array won't read in to console window c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
Schedule.cs
public static class SchArray
{
public static string[] clientName = new string[20];
public static DateTime[] startDate = new DateTime[20];
public static DateTime[] endDate = new DateTime[20];
public static string[] allocatedDriver = new string[20];
public static string[] depot = new string[20];
public static int count = 3;
}
public void schedule()
{
SchArray.clientName[0] = "eric cartman";
SchArray.clientName[1] = "peter griffin";
SchArray.clientName[2] = "homer simpson";
SchArray.startDate[0] = Convert.ToDateTime("2016,3,2");
SchArray.startDate[1] = Convert.ToDateTime("2016,3,4");
SchArray.startDate[2] = Convert.ToDateTime("2016,3,5");
SchArray.endDate[0] = Convert.ToDateTime("2016,3,3");
SchArray.endDate[1] = Convert.ToDateTime("2016,3,5");
SchArray.endDate[2] = Convert.ToDateTime("2016,3,6");
SchArray.allocatedDriver[0] = "owen";
SchArray.allocatedDriver[1] = "daniel";
SchArray.allocatedDriver[2] = "owen";
SchArray.depot[0] = "depot1";
SchArray.depot[1] = "depot2";
SchArray.depot[2] = "depot3";
}
Work_Schedule.cs
public void schedule()
{
Console.Clear();
Console.WriteLine(" Create Work Schedule ");
Console.WriteLine(Schedule.SchArray.clientName[0]);
Console.ReadKey();
}
Console.WriteLine(Schedule.SchArray.clientName[0]);
^^^^^ this line should display the name the eric cartman, i've debugged it and it says there are no objects in the array, they're are all null.
You need to create an object of Schedule since the array elements ware initialized inside the constructor:
Schedule scheduleObject = new Schedule();
Console.WriteLine(SchArray.clientName[0]);
For this particular scenario, i would like to suggest a different approach of creating a static list/array of objects. Let me modify the class as like the following:
public class SchArray
{
public string clientName;
public DateTime startDate;
public DateTime endDate;
public string allocatedDriver;
public string depot;
public int count = 3;
}
And i have a List<SchArray> defined as static;
public static List<SchArray> StaticSchArray = new List<SchArray>();
Then i can populate the list as like the following:
StaticSchArray.Add(new SchArray() {clientName="eric cartman",
startDate=Convert.ToDateTime("2016,3,2"),
endDate= Convert.ToDateTime("2016,3,2"),
depot="depot1",allocatedDriver ="owen" });
Similarly other elements can also be added to the array. this will be a better option for this scenario.

Can We Have Class Name Instead Of Return Type In c#? [closed]

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
Can We Have Class Name Instead Of Return Type ? If Yes Than How And If No Than How ? , Please Explain Me With This Basic Singleton Pattern
class Program
{
static void Main(string[] args)
{
Sample s1;
s1 = Sample.cr();
Sample s2 = Sample.cr();
Console.ReadLine();
}
}
class Sample
{
static Sample p;
private Sample()
{
}
// Why Here Retrun Type Is Missing I Mean Void / Int ?
public static Sample cr()
{
if(p==null)
{
p = new Sample();
}
return p;
}
}
Return type is not missing in your sample. Your cr method has return type, and it is Sample.
Any of your custom class is type too, not only primitive types such as int, string and so on.

Making a list in the property [closed]

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 have a list like that.
bread.BreadAdd(new Bread("DF6", "8", 16, 500));
bread.BreadAdd(new Bread("ZER6D23", "5", 8, 1000));
As I understood, I need to create a code in the bread class, but furthermore I have no idea is has to be in the property? If so, where exactly?
edit: uups. I wrote it wrong. it has to be in a function.
Can be something like this:
using SCG = System.Collections.Generic;
public class Bread {
public class Bread(string name, string version, int foodValue, int weight) {
// ...
}
}
public class Breads {
private readonly SCG.List breads = new SCG.List();
public void AddBread(Bread bread) {
breads.Add(bread);
}
public SCG.IEnumerable Breads {
get { return breads; }
}
}
See below.. You can create a method to add it to a private List variable and then expose it via property or you can have a setter for BreadList property and call breadClass.BreadList.Add((new Bread("DF6", "8", 16, 500));. Either option should be fine.
public class BreadClass
{
private List<Bread> lstbread;
public List<Bread> BreadList
{
get
{
return bread;
}
}
public void BreadAdd(Bread bread)
{
lstbread.Add(bread)
}
}

Categories