That name doesn"t exist in the current context error - c#

In public static void ammount() I get an error saying:
"The name 'mps' does not exist in the current context"
How do I fix this error?
struct mp3players{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
public static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
ArrayList mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Make: " + value.make);
Console.WriteLine("Model: " + value.model);
Console.WriteLine("MBSize: " + value.mbsize);
Console.WriteLine("Price: " + value.price);
Console.WriteLine();
}
}
This is where the error occurs.
I tried a couple of ways but did not find what I was searching for.
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
I am not very good in C# so an explanation what I did wrong is also very welcome!

public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}
this code has knowledge of mps.
you would add a parameter the method so public static void ammount() looks like public static void ammount(mp3players[] mps). that will fix this error but cause another
you will have to pass mps into the method.

mps is a local variable inside mp3() method. It is not a global variable, so its not accessible in ammount() method.

I got your point , you are getting compilation error The name 'mps' does not exist in the current context. So to fix this, you have to make mps as global variable and set that value in mp3() method, and use that in other methods as well.
struct mp3players
{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
private static ArrayList mps;
public static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Make: " + value.make);
Console.WriteLine("Model: " + value.model);
Console.WriteLine("MBSize: " + value.mbsize);
Console.WriteLine("Price: " + value.price);
Console.WriteLine();
}
}
public static void ammount()
{
foreach (mp3players value in mps)
{
Console.WriteLine("ID: " + value.id);
Console.WriteLine("Model: " + value.model);
Console.WriteLine();
}
}

The variable 'mps' declared inside the function public static void mp3() and you are trying to access the same from another function public static void ammount() which can't be feasible due to the scope.
To access the 'mps' you have to remove the declaration from the mp3() function and move it directly uder you so defined class:
public class MySongColl
{
private static ArrayList mps = new ArrayList();
struct mp3players
{
....
....
}
public static void mp3()
{
....
....
}
public static void ammount()
{
....
....
}
}
NOTE:
Use proper Naming convention while you write code. e.g: Speeling mistake in Function name ammount() should be amount(). And use camel case while declaring the names like struct mp3players should be Mp3Players etc. These are not necessary to build the code but essential for a developer.

Related

Variable is not updating [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 4 years ago.
Improve this question
Problem:
A List<> variable is passing a reference check even though there is no accessible data in the list.
Solution:
The Save() function finished executing before the save thread had finished causing the thread to abort.
added threadrunning = true before starting the thread.
Code:
Thread saveThread;
public string filepath;
public string rootElementName;
public string objectElementName;
public int objectsSaved;
public SaveData saveData;
public List<SavedObject> datasets;
public bool threadRunning = false;
public bool syncingData = false;
public void StartSaveThread()
{
bool warned = false;
//The "error" is here
//The reference returns true but none of the data is accessible, there is nothing in the list
while(datasets != saveData.objects)
{
if(warned == false)
{
Debug.Log("Waiting for data to sync");
syncingData = true;
warned = true;
}
}
syncingData = false;
saveThread = new Thread(SaveData);
saveThread.Start();
}
public void SaveData()
{
bool saveComplete = false;
threadRunning = true;
while (threadRunning && saveComplete == false)
{
objectsSaved = 0;
XmlDocument saveFile = new XmlDocument();
XmlElement documentRoot = saveFile.CreateElement(rootElementName);
foreach (SavedObject dataset in datasets)
{
XmlElement savedObjectElement = saveFile.CreateElement(objectElementName);
foreach (SavedElement savedValue in dataset.data.savedElements)
{
XmlElement newElement = saveFile.CreateElement(savedValue.name);
newElement.InnerText = savedValue.value;
savedObjectElement.AppendChild(newElement);
}
documentRoot.AppendChild(savedObjectElement);
objectsSaved++;
}
saveFile.AppendChild(documentRoot);
saveFile.Save(filepath);
saveComplete = true;
}
threadRunning = false;
}
More of the code can be found bellow if you want to reproduce the error.
SavedObject Class (Simplified):
//this class is in SavedObject.cs
public int id = 0;
public string name = "";
public string objectPath = "";
public bool saveData;
public bool loadData;
public bool savePosition;
public bool saveRotation;
public bool saveScale;
public SavedObjectData data = new SavedObjectData();
public GameObject parent;
public List<SavedElement> savedElements = new List<SavedElement>();
public void StoreData()
{
data.id = id;
data.name = name;
data.objectPath = objectPath;
data.saveData = saveData;
data.loadData = loadData;
data.savePosition = savePosition;
data.saveRotation = saveRotation;
data.saveScale = saveScale;
Vector3 position = transform.position;
data.position = position;
Vector3 rotation = transform.rotation.eulerAngles;
data.rotation = rotation;
Vector3 scale = transform.localScale;
data.scale = scale;
}
public void LoadData()
{
id = data.id;
name = data.name;
objectPath = data.objectPath;
saveData = data.saveData;
loadData = data.loadData;
savePosition = data.savePosition;
saveRotation = data.saveRotation;
saveScale = data.saveScale;
if (savePosition) { transform.position = data.position; }
if (saveRotation) { transform.eulerAngles = data.rotation; }
if (saveScale) { transform.localScale = data.scale; }
savedElements = data.savedElements;
}
SavedObjectData Class(simplified):
//this class is in SavedObject.cs
public int id;
public string name;
public string objectPath;
public bool saveData;
public bool loadData;
public bool savePosition;
public bool saveRotation;
public bool saveScale;
public Vector3 position;
public Vector3 rotation;
public Vector3 scale;
public List<SavedElement> savedElements = new List<SavedElement>();
public string BoolToString(bool value)
{
string result = "";
if (value)
{
result = "t";
}
else
{
result = "f";
}
return result;
}
public bool StringToBool(string value)
{
bool result;
if (value == "t")
{
result = true;
}
else
{
result = false;
}
return result;
}
public void StoreValues()
{
savedElements = new List<SavedElement>();
savedElements.Add(new SavedElement() { name = "ID", value = id.ToString() });
savedElements.Add(new SavedElement() { name = "Name", value = name });
savedElements.Add(new SavedElement() { name = "Path", value = objectPath });
savedElements.Add(new SavedElement() { name = "SD", value = BoolToString(saveData) });
savedElements.Add(new SavedElement() { name = "LD", value = BoolToString(loadData) });
savedElements.Add(new SavedElement() { name = "SP", value = BoolToString(savePosition) });
savedElements.Add(new SavedElement() { name = "SR", value = BoolToString(saveRotation) });
savedElements.Add(new SavedElement() { name = "SS", value = BoolToString(saveScale) });
if (savePosition) { savedElements.Add(new SavedElement() { name = "P", value = "x:" + position.x + "|y:" + position.y + "|z:" + position.z }); }
if (saveRotation) { savedElements.Add(new SavedElement() { name = "R", value = "x:" + rotation.x + "|y:" + rotation.y + "|z:" + rotation.z }); }
if (saveScale) { savedElements.Add(new SavedElement() { name = "S", value = "x:" + scale.x + "|y:" + scale.y + "|z:" + scale.z }); }
}
public void LoadValues()
{
foreach (SavedElement savedElement in savedElements)
{
string[] sectionedData;
switch (savedElement.name)
{
default:
break;
case "ID":
id = int.Parse(savedElement.value);
break;
case "Name":
name = savedElement.value;
break;
case "Path":
objectPath = savedElement.value;
break;
case "SD":
saveData = StringToBool(savedElement.value);
break;
case "LD":
loadData = StringToBool(savedElement.value);
break;
case "SP":
savePosition = StringToBool(savedElement.value);
break;
case "SR":
saveRotation = StringToBool(savedElement.value);
break;
case "SS":
saveScale = StringToBool(savedElement.value);
break;
case "P":
sectionedData = savedElement.value.Split('|');
position = new Vector3(float.Parse(sectionedData[0].Split(':')[1]), float.Parse(sectionedData[1].Split(':')[1]), float.Parse(sectionedData[2].Split(':')[1]));
break;
case "R":
sectionedData = savedElement.value.Split('|');
rotation = new Vector3(float.Parse(sectionedData[0].Split(':')[1]), float.Parse(sectionedData[1].Split(':')[1]), float.Parse(sectionedData[2].Split(':')[1]));
break;
case "S":
sectionedData = savedElement.value.Split('|');
scale = new Vector3(float.Parse(sectionedData[0].Split(':')[1]), float.Parse(sectionedData[1].Split(':')[1]), float.Parse(sectionedData[2].Split(':')[1]));
break;
}
}
}
SavedElement Class (simplified):
//this class is in SavedObject.cs
public string name;
public string value;
SaveData Class (simplified):
//this class is in SaveData.cs
private IEnumerator SaveObjects(string path, string rootElementName, string objectElementName, GameObject savedObjects){
objects = new List<SavedObject>();
//Compiles List
foreach (Transform savedObject in savedObjects.transform)
{
if(savedObject.gameObject.GetComponent<SavedObject>() != null)
{
objects.add(savedObject.gameObject.GetComponent<SavedObject>())
}
}
//Creates new instance of SaveSystem
saveSystem = new SaveSystem
{
filepath = path,
rootElementName = rootElementName,
objectElementName = objectElementName,
saveData = this,
datasets = objects
};
//checks if saveSystem.datasets == objects then starts the thread to save the data
//PLEASE NOTE: the reference is true but none of the data is updated and accessible
saveSystem.StartSaveThread();
while (saveSystem.threadRunning || saveSystem.syncingData)
{
yield return null;
}
}
SaveSystem Class (simplified):
//this class is in SaveSystem.cs
public SaveData saveData;
public string filepath;
public string rootElementName;
public string objectElementName;
public int objectsSaved;
public List<SavedObject> datasets;
Thread saveThread;
public volatile bool threadRunning = false;
public volatile bool syncingData = false;
public void SaveData()
{
bool saveComplete = false;
threadRunning = true;
while (threadRunning && saveComplete == false)
{
objectsSaved = 0;
XmlDocument saveFile = new XmlDocument();
XmlElement documentRoot = saveFile.CreateElement(rootElementName);
foreach (SavedObject dataset in datasets)
{
XmlElement savedObjectElement = saveFile.CreateElement(objectElementName);
foreach (SavedElement savedValue in dataset.data.savedElements)
{
XmlElement newElement = saveFile.CreateElement(savedValue.name);
newElement.InnerText = savedValue.value;
savedObjectElement.AppendChild(newElement);
}
documentRoot.AppendChild(savedObjectElement);
objectsSaved++;
}
saveFile.AppendChild(documentRoot);
saveFile.Save(filepath);
saveComplete = true;
}
threadRunning = false;
}
public void StartSaveThread()
{
bool warned = false;
//The error is here
//The reference returns true but none of the data is accessible
while(datasets != saveData.objects)
{
if(warned == false)
{
Debug.Log("Waiting for data to sync");
syncingData = true;
warned = true;
}
}
syncingData = false;
saveThread = new Thread(SaveData);
saveThread.Start();
}
I'd first make it easily reproducible, for example, start the thread, wait a second, and then assign the List variable so that you can reproduce the race condition constantly, by just letting the thread run first.
Perhaps change your logic so that you don't start your thread until after you know for certain the variable has been set, or you can use a https://msdn.microsoft.com/en-us/library/system.threading.manualresetevent(v=vs.110).aspx object where the thread calls WaitOne(), which will block that thread on that line, until your List code path assigns the value and then calls the Set() event on the ManualResetEvent.

How to list a list of classes in a listbox?

I want to make a list with person's names, in a listbox. The names should be added using the textbox, and i am using a class for the name savement etc.
How can i show the name of the person that added info to a listbox, and only show the information to a richtextbox when the list is clicked/pressed?
class code
class Persoon
{
private string naam;
private string geslacht;
private double gewicht;
private double lengte;
public double bmi;
public string Naam
{
get { return naam; }
set { naam = value; }
}
public string Geslacht
{
get { return geslacht; }
set { geslacht = value; }
}
public double Gewicht
{
get { return gewicht; }
set { gewicht = value; }
}
public double Lengte
{
get { return lengte; }
set { lengte = value; }
}
public double Bmi
{
get { return bmi; }
set { bmi = value; }
}
public object Convert { get; internal set; }
public Persoon(string nm, string gt, int wt, int le)
{
naam = nm;
geslacht = gt;
gewicht = wt;
lengte = le;
}
public double BMI()
{
double bmiuitkomst = gewicht / Math.Pow(lengte / 100.0, 2);
return bmiuitkomst;
}
public override string ToString()
{
return "Persoon: " + Naam + " " + Geslacht;
}
form code
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string naam = tb_naam.Text;
string geslacht = tb_geslacht.Text;
double gewicht = Convert.ToDouble(tb_gewicht.Text);
double lengte = Convert.ToDouble(tb_lengte.Text);
double bmiuitkomst = gewicht / Math.Pow(lengte / 100.0, 2);
Persoon nieuwbmi = new Persoon(naam, geslacht, Convert.ToInt32(gewicht), Convert.ToInt32(lengte));
rtb_uitkomst.Text = "Naam: "+ naam + Environment.NewLine + "Geslacht: " + geslacht + Environment.NewLine + "BMI: " + Convert.ToString(bmiuitkomst);
// rtb_uitkomst.Text = nieuwbmi.ToString();
List<string> uitkomsten = new List<string>();
foreach (var item in uitkomsten)
{
uitkomsten.Add(Convert.ToString(nieuwbmi));
}
// lb_list.(Convert.ToString(nieuwbmi));
lb_list.DataSource = uitkomsten;
}
}
thanks in advance.
You are declaring a variable uitkomsten locally in button1_Click. This variable exists only while the method is executed. To make it live during the whole lifetime of the form, declare it as a field of the form class (and remove the declaration from the button1_Click method)
public partial class Form1 : Form
private List<string> uitkomsten = new List<string>();
public Form1()
{
InitializeComponent();
}
...
}
Then you try to add a person with
foreach (var item in uitkomsten)
{
uitkomsten.Add(Convert.ToString(nieuwbmi));
}
This cannot work. The foreach loop loops as many times as there are elements in the uitkomsten list. But since the list is empty at the beginning, the Add method will never be executed. Remove the loop.
uitkomsten.Add(Convert.ToString(nieuwbmi));
Note that you could add persons directly to the list, if you had a List<Persoon>. The listbox automatically uses .ToString() to display objects.
uitkomsten.Add(nieuwbmi);
Another problem is that lb_list.DataSource = uitkomsten; always assigns the same list. Because of how the listbox is implemented, it does not notice any difference and will therefore not display new persons added. To work around this problem, assign null first.
lb_list.DataSource = null;
lb_list.DataSource = uitkomsten;
Also change the weight and length parameters to double in the constructor of Persoon. You have doubles everywhere else for these measurements. If you use auto-implemented properties, this simplifies the class
class Persoon
{
public string Naam { get; set; }
public string Geslacht { get; set; }
public double Gewicht { get; set; }
public double Lengte { get; set; }
public double Bmi { get; set; }
public Persoon(string nm, string gt, double wt, double le)
{
Naam = nm;
Geslacht = gt;
Gewicht = wt;
Lengte = le;
}
public double BMI()
{
return Gewicht / Math.Pow(Lengte / 100.0, 2);
}
public override string ToString()
{
return $"Persoon: {Naam} {Geslacht}";
}
}
The form then becomes (with a few additional tweaks)
public partial class Form1 : Form
{
private List<Persoon> uitkomsten = new List<Persoon>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string naam = tb_naam.Text;
string geslacht = tb_geslacht.Text;
double gewicht = Convert.ToDouble(tb_gewicht.Text);
double lengte = Convert.ToDouble(tb_lengte.Text);
Persoon nieuwbmi = new Persoon(naam, geslacht, gewicht, lengte);
rtb_uitkomst.Text = $"Naam: {naam}\r\nGeslacht: {geslacht}\r\nBMI: {nieuwbmi.BMI()}";
uitkomsten.Add(nieuwbmi);
lb_list.DataSource = null;
lb_list.DataSource = uitkomsten;
}
}
private List<string> uitkomsten = new List<string>();
private BindingSource bs = new BindingSource();
public MainForm()
{
InitializeComponent();
bs.DataSource = uitkomsten;
lb_list.DataSource = bs;
}
private void button1_Click(object sender, EventArgs e)
{
......
uitkomsten.Add(Convert.ToString(nieuwbmi));
bs.ResetBindings(false);
}
How can i make sure it only displays the info in the richtextbox when the name is selected (in the listbox)?
private void lb_list_SelectedIndexChanged(object sender, EventArgs e)
{
Persoon nieuwbmi = ((Persoon)lb_list.SelectedItem).Name1;
rtb_uitkomst.Text = "Naam: " + naam + Environment.NewLine + "Geslacht: " + geslacht + Environment.NewLine + "BMI: " + Convert.ToString(bmiuitkomst);
}

Can anyone suggest how to generate Rest body dynamically while calling Get API with Query match

In my case, while calling API (GET Method), I should send the below query as part of the body.
URI : GET XXX/_search
Body:
{
"query":
{
"match_all":
{
}
}
}
As of now, I'm hard-coding the string and appending to the resource body.
hopefully below code snippet can help you.
Consumer:
JsonField jsn1 = new JsonField() { Name = "_id"};
jsn1.AddValue("1234");
JsonField jsn2 = new JsonField() { Name = "_name" };
jsn2.AddValue("CDE");
JsonField jsn0 = new JsonField(true) { Name = "match" };
JsonField jsn = new JsonField(true) { Name = "query" };
JsonConverter.MapField(jsn0, jsn1);
JsonConverter.MapField(jsn, jsn0);
JsonConverter jsonconverter = new JsonConverter();
jsonconverter.Add(jsn);
JsonField jsn3 = new JsonField() { Name = "name" };
jsn3.AddValue("temp");
jsonconverter.Add(jsn3);
Console.WriteLine(jsonconverter.GetJsonFormat());
Check logic below.
public class JsonConverter
{
LinkedList<JsonField> jsonlinking = new LinkedList<JsonField>();
public void Add(JsonField jsonfield)
{
LinkedListNode<JsonField> jsonelement = jsonlinking.Last;
if (jsonelement != null)
{
jsonelement.Value.SpecialChar = ",";
}
jsonlinking.AddLast(jsonfield);
}
public string GetJsonFormat()
{
jsonformat = string.Empty;
jsonformat += "{" + Environment.NewLine;
foreach (var item in jsonlinking)
{
ReadJson(item);
}
jsonformat += Environment.NewLine + "}" + Environment.NewLine;
return jsonformat;
}
public static void MapField(JsonField primary, JsonField relative)
{
primary.Next = relative;
}
public static void MapField(Queue<JsonField> linksource)
{
JsonField primary = null;
JsonField relative = null;
foreach (var item in linksource)
{
if (primary == null)
{
primary = item;
}
else if (relative == null)
{
relative = null;
}
else
{
JsonConverter.MapField(primary, relative);
primary = null;
relative = null;
}
}
}
private string jsonformat = string.Empty;
private void ReadJson(JsonField field)
{
if (field != null)
{
jsonformat += #"""";
jsonformat += field.Name;
jsonformat += #"""";
if (field.isContainer)
{
jsonformat += #":{" + Environment.NewLine;
int count = field.ChildNodes.Count();
if (count > 0)
{
foreach (var item in field.ChildNodes)
{
ReadJson(item);
}
}
jsonformat = (jsonformat.Substring(jsonformat.Length - 1, 1) == ",") ? jsonformat.Substring(0, jsonformat.Length - 1) : jsonformat;
jsonformat += #"}" + field.SpecialChar + Environment.NewLine;
}
else
{
jsonformat += #":";
jsonformat += field.GetValues();
}
}
}
}
public class JsonField
{
private int Size = 1;
private int CurrentIndex = 0;
public string Name { get; set; }
private string[] _value;
public string[] Value { get { return _value; } private set { _value = value; } }
public bool isContainer{get;set;}
public JsonField()
{
this.Value = new string[this.Size];
}
private Queue<JsonField> _next = new Queue<JsonField>();
public JsonField Next {
set
{
if (this._next.Count>0)
{
foreach (var item in this._next)
{
item.SpecialChar = ",";
}
}
this._next.Enqueue(value);
}
}
public IEnumerable<JsonField> ChildNodes { get { return this._next; } }
public JsonField(int valuesize)
{
this.Size = valuesize;
this.Value = new string[this.Size];
}
public JsonField(bool iscontainer)
{
this.isContainer = iscontainer;
}
public void AddValue(string value)
{
if (CurrentIndex >= this.Value.Length)
{
throw new ArgumentException("Index Out of Range over Value Array");
return;
}
this.Value[CurrentIndex] = value;
CurrentIndex++;
}
public void ClearValue()
{
this.Value = null;
this.Value = new string[this.Size];
}
public string GetValues()
{
if (this.Value.Length == 1)
{
return #"""" + this.Value[0] + #"""";
}
string returns=string.Empty;
for (int index = 0; index < this.Value.Length; index++)
{
returns += #"""" + this.Value[index] + #"""";
if ((index +1) < this.Value.Length)
{
returns += ",";
}
}
return "[" + returns+ "]";
}
public string SpecialChar { get; set; }
}

How do I call static from the class I created in c#

namespace HelloDogs
{
class Dog
{
private string barkSound;
private string breed; // Added dogHeight, dogColor and noOfLegs to
private int dogHeight; // the private variable
private string dogColor;
private static int noOfLegs;
public string Breed
{
get { return breed; }
set { breed = value; }
}
public static int NoOfLegs
{
get{return noOfLegs; } // I created properties to encapsulate the variables
set {noOfLegs = value; } // dogHeight, dogColor and noOfLegs using the properties
}
public int DogHeight
{
get {return dogHeight;}
set{dogHeight = value;}
}
public string DogColor
{
get {return dogColor;}
set{ dogColor = value;}
}
private string dogSpeech;
public Dog()
{
barkSound = "Jack!";
breed = "cocker spaniel";
}
// Added a new constructor below that takes the following parameters
public Dog(int h,string b, string c )
{
dogHeight = h;
dogColor = c;
breed = b;
}
// A private method to check the dogHeight if true or false
private bool IsBig(int x)
{
if (x < 50)
return false;
else
return true;
}
// Change the GetSpeech method below to display all details about the dog
public string GetSpeech()
{
dogSpeech = "Hello, I am a " + breed + " , " + dogHeight + "cm" + ","
+ dogColor + "." + barkSound ;
return dogSpeech;
if(IsBig(dogHeight))
{
return dogSpeech = "You are big ";
} else
{
dogSpeech = " You are not big enough";
}
}
public void SetSound(String barkSound)
{
this.barkSound = barkSound;
}
}
}
Calling static from class please see this in msdn
https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

get set help using a structure and arraylist

enter link description hereI am trying to make (private int vr;) to work in the loop so I could change it easily, however I have no idea how to make a get / set for this, or If i have to use get set at all.
I hope this is enough information if further info in necessary please leave a comment saying so.
struct mp3players
{
private int ID;
private string Make;
private string Model;
private int MBSize;
private double Price;
private int vr;
public int id
{
get { return this.ID; }
set { this.ID = value; }
}
public string make
{
get { return this.Make; }
set { this.Make = value; }
}
public string model
{
get { return this.Model; }
set { this.Model = value; }
}
public int mbsize
{
get { return this.MBSize; }
set { this.MBSize = value; }
}
public double price
{
get { return this.Price; }
set { this.Price = value; }
}
public int VR
{
get { return this.vr; }
set { this.vr = value; }
}
}
private static ArrayList mps;
static void mp3()
{
mp3players mp1 = new mp3players();
mp1.id = 1;
mp1.make = "GET technologies .inc";
mp1.model = "HF 410 ";
mp1.mbsize = 4096;
mp1.price = 129.95;
mp1.VR = 500;
mp3players mp2 = new mp3players();
mp2.id = 2;
mp2.make = "Far & Loud";
mp2.model = "XM 600 ";
mp2.mbsize = 8192;
mp2.price = 224.95;
mp2.VR = 500;
mp3players mp3 = new mp3players();
mp3.id = 3;
mp3.make = "Innotivative";
mp3.model = "Z3 ";
mp3.mbsize = 512;
mp3.price = 79.95;
mp3.VR = 500;
mp3players mp4 = new mp3players();
mp4.id = 4;
mp4.make = "Resistance S.A.";
mp4.model = "3001 ";
mp4.mbsize = 4096;
mp4.price = 124.95;
mp4.VR = 500;
mp3players mp5 = new mp3players();
mp5.id = 5;
mp5.make = "CBA";
mp5.model = "NXT volume ";
mp5.mbsize = 2048;
mp5.price = 159.05;
mp5.VR = 500;
mps = new ArrayList();
mps.Add(mp1);
mps.Add(mp2);
mps.Add(mp3);
mps.Add(mp4);
mps.Add(mp5);
}
static void mutteer()
{
int i = 1;
int a;
startloop:
while (i == 1)
{
Console.WriteLine("Wat is het Id van de mp3 speler?");
try
{
a = Convert.ToInt16(Console.ReadLine());
}
catch
{
Console.WriteLine("Dat is geen nummer waarde!");
goto startloop;
}
if (a == 1)
{
Console.WriteLine("Wat is het nieuwe voorraad");
try
{
as you can see I wrote vr here what obviously doesn't work,
I need to use the set and get here in my understanding but how do I get a get and set to work with it?
VR = Convert.ToInt32(Console.ReadLine());
i = 2;
}
catch
{
Console.WriteLine("Dat is geen nummer waarde!");
goto startloop;
}
}
}
}

Categories

Resources