i am a new programmer here. I have the following code. I passed the object by value, but when i printed the results, i got this
elf attacked orc for 20 damage!
Current Health for orc is 80
elf attacked orc for 20 damage!
Current Health for orc is 80
this got me confused about passing by reference because I did not expect the health in the main to be 80 since i passed the object by value. Can someone explain how the result for health in the program main function was 80 instead of 100?
//MAIN class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace passingTest
{
class Program
{
static void Main(string[] args)
{
// Enemy Objects
Enemy elf = new Enemy("elf", 100, 1, 20);
Enemy orc = new Enemy("orc", 100, 1, 20);
elf.Attack(orc);
Console.WriteLine("{0} attacked {1} for {2} damage!", elf.Nm, orc.Nm, elf.Wpn);
Console.WriteLine("Current Health for {0} is {1}", orc.Nm, orc.Hlth);
Console.ReadLine();
}
}
}
// Enemy Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace passingTest
{
class Enemy
{
// variables
string nm = "";
int hlth = 0;
int lvl = 0;
int wpn = 0;
public string Nm
{
get { return nm; }
set { nm = value; }
}
public int Wpn
{
get { return wpn; }
set { wpn = value; }
}
public int Hlth
{
get { return hlth; }
set { hlth = value; }
}
public Enemy(string name, int health, int level, int weapon)
{
nm = name;
hlth = health;
lvl = level;
wpn = weapon;
}
public void Attack(Enemy rival){
rival.hlth -= this.wpn;
Console.WriteLine("{0} attacked {1} for {2} damage!", this.nm, rival.nm, this.wpn);
Console.WriteLine("Current Health for {0} is {1}", rival.nm, rival.hlth);
}
}
}
In C#/.NET, whether an object is passed by reference or by value is determined by the type of the object. If the object is a reference type (i.e. it is declared with class), it is passed by reference. If the object is a value type (i.e. it is declared with struct) it is passed by value.
If you change the declaration of Enemy to
struct Enemy
you will see pass-by-value semantics.
In C# Classes are considered reference types. A reference type is a type which has as its value a reference to the appropriate data rather than the data itself. For instance, consider the following code:
Here;s a link with more information on the subject: http://jonskeet.uk/csharp/parameters.html
Related
I am trying to call a Matlab function from c# by using the instructions in this Matlab help website.
Using the method and the example in the Matlab website went well but now trying to redo everything with writing a separated class for calling the Matlab function, and call the method in the main class and give the parameters values to get results.
When doing so, an error in reusing the variables happend.
The following is the class that is calling the Matlab function.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace fromMatlab
{
public class Class1
{
public static Data FromMatLab(double a, double b, string c)
{
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(#"cd c:\Users\abdhab\Documents\MATLAB");
object result = null;
matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world");
object[] res = result as object[];
object x = res[0];
object y = res[1];
return new Data { X = x, Y = y };
}
}
}
The class that deals with the variables and the constructers.
namespace fromMatlab
{
public class Data
{
public object X { get; set; }
public object Y { get; set; }
}
public struct DataStruct
{
object X;
object Y;
}
}
The Main class.
using System;
using System.Collections.Generic;
using System.Text;
namespace fromMatlab
{
class Program
{
static void Main(string[] args)
{
double a = 1;
double b = 2;
string c = "world";
object data =Class1.FromMatLab(a, b, c);
object X = data.X;
object Y = data.Y;
Console.WriteLine(X);
Console.WriteLine(Y);
Console.ReadLine();
}
}
}
The error is the compiler error CS1061 and it occur in the following lines.
object X = data.X;
object Y = data.Y;
The Matlab function is the following:
function [x,y] = myfunc(a,b,c)
x= a+b;
y = sprintf('Hello %s',c);
You are assigning a result of Class1.FromMatLab(a, b, c); to a variable of type object, which doesn't have X or Y property. If you change object data to Data data or var data, the compiler error should disappear.
I'm learning C# and have written a console program to save an an array of high scores to a file. Although the program works, how I have got it to work is making me feel uneasy and feels more of a hack than a solution so I was looking for guidance on how I should have written it.
What I am currently doing within the Main method is:
Declaring an array of highscore objects
Initialising them
Assigning some values to the array.
I am happy with what I have done up until now, it's the following two steps that make me uneasy
I then declare another HighScore object
I use this object to pass the array of highscores to the SaveHighScores method.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace HighScore
{
class HighScore
{
public string Name { get; set; }
public int Score { get; set; }
public void SaveHighScores(HighScore[] highScores)
{
string allHighScoresText = "";
foreach (HighScore score in highScores)
{
allHighScoresText += $"{score.Name},{score.Score}" + Environment.NewLine;
}
File.WriteAllText("C:/Temp/highscores.csv", allHighScoresText);
}
static void Main(string[] args)
{
HighScore[] highScore = new HighScore[2];
for (int i = 0; i < highScore.Length; i++)
{
highScore[i] = new HighScore();
}
highScore[0].Name = "A";
highScore[0].Score = 100;
highScore[1].Name = "B";
highScore[1].Score = 200;
// are the following two lines correct or am I missing something?
HighScore hs = new HighScore();
hs.SaveHighScores(highScore);
}
}
}
Make SaveHighScores static and you won't need an instance of HighScore to call it. (You can call it directly as HighScore.SaveHighScores())
I prefer to split the representation of your data from the actions that you perform on this data. So I would go for two classes, one for the Data and one for the Save/Load and other business logic
public class HighScore
{
public string Name { get; set; }
public int Score { get; set; }
}
// This class handles the core work to persist your data on the storage medium
// The class is static so you don't need to declare instances and use directly the methods available.
public static class Repo_HighScore
{
// For simplicity, no error Handling but, for a robust implementation,
// error handling is required
public static bool SaveHighScores(HighScore[] highScores)
{
StringBuilder allHighScoresText = new StringBuilder();
foreach (HighScore score in highScores)
allHighScoresText.AppendLine($"{score.Name},{score.Score}");
File.WriteAllText("C:/Temp/highscores.csv", allHighScoresText.ToString());
}
public static HighScore[] LoadHighScores()
{
List<HighScore> hs = new List<HighScore>();
foreach(string line in File.ReadLines("C:/Temp/highscores.csv"))
{
string[] parts = line.Split(',');
HighScore temp = new HighScore()
{ Name = parts[0], Score = Convert.ToInt32(parts[1])};
hs.Add(temp);
}
return hs.ToArray();
}
}
I was just making this program to experiment with lists and such, and I was curious as to why in the foreach loop the Object always shows up as the "Minecraft" Wish object. Is it because it was the last Wish object to be created? And how can I fix it, so all 3 Wish objects which have been declared show up?
Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Wish iPod = new Wish("iPod", "Various", 299.00);
Wish Phone = new Wish("New Phone", "Various", 00.00);
Wish Minecraft = new Wish("Minecraft Account", "www.minecraft.net", 30.00);
List<Wish> Wishlist = new List<Wish>();
Wishlist.Add(Phone);
Wishlist.Add(iPod);
Wishlist.Add(Minecraft);
Console.WriteLine("Toby's Wishlist");
Console.WriteLine("If cost is 00.00, the Wish's cost varies.");
Console.WriteLine(" ");
foreach (Wish wish in Wishlist)
{
Console.WriteLine("---Wish---");
Console.WriteLine("Name: {0}", wish.getName());
Console.WriteLine("Store: {0}", wish.getStore());
Console.WriteLine("Cost: ${0}", wish.getCost().ToString());
Console.WriteLine("----------");
Console.WriteLine(" ");
}
Console.ReadLine();
}
}
public class Wish
{
static string Name, Store;
static double ApproxCost;
public Wish(string name, string store, double approxCost)
{
Name = name;
Store = store;
ApproxCost = approxCost;
}
public string getName()
{
return Name;
}
public string getStore()
{
return Store;
}
public double getCost()
{
return ApproxCost;
}
}
}
Remove static from Wish members declaration
static means that the data will be shared across all the instances. So static members are also so called class variables. While not static members - are object variables.
It's because in class Wish you declared Name, Score, and ApproxCost as static.
So I am developing an append-only 64-bit-ish List and Dictionary, and I've run into a memory error. I figured I would at some point, but not at 64 MBs. I find that somewhat unexpected, and am curious if someone could explain to me why it's running into an issue at 64 MBs.
My test for my new List class is simply an attempt to create and load 8 GBs worth of bools into the List. I figured they'd suck up only ~1 bit each, so I'd get some good metrics / precision for testing my program.
Here is the output from VS:
- this {OrganicCodeDesigner.DynamicList64Tail<bool>} OrganicCodeDesigner.DynamicList64Tail<bool>
Count 536870912 ulong
- data Count = 536870912 System.Collections.Generic.List<bool>
- base {"Exception of type 'System.OutOfMemoryException' was thrown."} System.SystemException {System.OutOfMemoryException}
- base {"Exception of type 'System.OutOfMemoryException' was thrown."} System.Exception {System.OutOfMemoryException}
+ Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
HelpLink null string
+ InnerException null System.Exception
Message "Exception of type 'System.OutOfMemoryException' was thrown." string
Source "mscorlib" string
StackTrace " at System.Collections.Generic.Mscorlib_CollectionDebugView`1.get_Items()" string
+ TargetSite {T[] get_Items()} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
+ Static members
+ Non-Public members
- Raw View
Capacity 536870912 int
Count 536870912 int
- Static members
+ Non-Public members
- Non-Public members
+ _items {bool[536870912]} bool[]
_size 536870912 int
_syncRoot null object
_version 536870912 int
System.Collections.Generic.ICollection<T>.IsReadOnly false bool
System.Collections.ICollection.IsSynchronized false bool
System.Collections.ICollection.SyncRoot {object} object
System.Collections.IList.IsFixedSize false bool
System.Collections.IList.IsReadOnly false bool
item false bool
- Type variables
T bool bool
And here are the classes I am currently working on:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrganicCodeDesigner
{
public class DynamicList64Tail<T> : iList64<T>
{
private List<T> data;
public DynamicList64Tail()
{
data = new List<T>();
}
public void Add(T item)
{
data.Add(item);
}
public void Clear()
{
data.Clear();
}
public bool Contains(T item)
{
return data.Contains(item);
}
public ulong? IndexOf(T item)
{
if(this.data.Contains(item)) {
return (ulong)data.IndexOf(item);
}
return null;
}
public T this[ulong index]
{
get
{
return data[(int)(index)];
}
set
{
data[(int)(index)] = value;
}
}
public ulong Count
{
get { return (ulong)data.Count; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace OrganicCodeDesigner
{
// #todo: Create IList64, with 64-bit longs in mind.
// #todo: Create BigIntegerList, which may supersede this one.
public class DynamicList64<T> : iList64<T>
{
private List<iList64<T>> data;
private ulong count = 0;
private ulong depth = 0;
public DynamicList64()
{
data = new List<iList64<T>>() { new DynamicList64Tail<T>()};
count = 0;
}
public DynamicList64(ulong depth)
{
this.depth = depth;
if (depth == 0)
{
data = new List<iList64<T>>() { new DynamicList64Tail<T>() };
}
else
{
depth -= 1;
data = new List<iList64<T>>() { new DynamicList64<T>(depth) };
}
}
internal DynamicList64(List<iList64<T>> data, ulong depth)
{
this.data = data;
this.depth = depth;
this.count = Int32.MaxValue;
}
public void Add(T item)
{
if (data.Count >= Int32.MaxValue)
{
//#todo: Do switch operation, whereby this {depth, List l} becomes this {depth + 1, List.Add(List l), count = 1}, and the new object becomes {depth, List l, count = max}
DynamicList64<T> newDynamicList64 = new DynamicList64<T>(this.data, this.depth);
this.data = new List<iList64<T>>() { newDynamicList64 };
this.count = 0;
this.depth += 1;
}
if(data[data.Count-1].Count >= Int32.MaxValue) {
if (depth == 0)
{
data.Add(new DynamicList64Tail<T>());
}
else
{
data.Add(new DynamicList64<T>(depth - 1));
}
}
data[data.Count - 1].Add(item);
count++;
}
public void Clear()
{
data.Clear();
data = new List<iList64<T>>() { new DynamicList64Tail<T>() };
count = 0;
depth = 0;
}
public bool Contains(T item)
{
foreach(iList64<T> l in data) {
if(l.Contains(item)) {
return true;
}
}
return false;
}
public ulong? IndexOf(T item)
{
for (int i = 0; i < data.Count; i++ )
{
if (data[i].Contains(item))
{
return (ulong)(((ulong)i * (ulong)(Int32.MaxValue)) + data[i].IndexOf(item).Value);
}
}
return null;
}
public T this[ulong index]
{
get
{
return data[(int)(index / Int32.MaxValue)][index % Int32.MaxValue];
}
set
{
data[(int)(index / Int32.MaxValue)][index % Int32.MaxValue] = value;
}
}
public ulong Count
{
get { return this.count; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrganicCodeDesigner
{
public interface iList64<T>
{
void Add(T item);
void Clear();
bool Contains(T item);
ulong? IndexOf(T item);
T this[ulong index] { get; set;}
ulong Count { get; }
}
}
And the test program's code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrganicCodeDesigner;
namespace OrganicCodeDesignerListDictionaryTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void Button_TestList_Click(object sender, EventArgs e)
{
DynamicList64<bool> newList = new DynamicList64<bool>();
newList.Add(true);
newList.Add(false);
bool b = true;
for (ulong i = 0; i < 68719476736; i++)
{
b = !b;
newList.Add(b);
//if(i%4096==0) {
//TextBox_Output.Text += "List now contains " + i + "\r";
//}
}
TextBox_Output.Text += "Successfully added all the bits.\r";
}
private void Button_TestDictionary_Click(object sender, EventArgs e)
{
}
}
}
Perhaps you can spot the error?
Perhaps you can spot the error?
I think the error is here:
I figured they'd suck up only ~1 bit each, so I'd get some good metrics / precision for testing my program.
A bool takes one byte, not one bit - so you've drastically underestimated the size of your list. You're actually running into an error with 512MB of bools. As Reed Copsey is editing a little faster than me - I suspect the list is trying to increase its size by allocating an array 2x it's current size [i.e. a 1GB array] and that this is running into some .net limitations.
This is probably a good time to start implementing your splitting logic.
There are limits to the size of an array in .NET. Even if you are running on 64bit platforms, and set gcAllowVeryLargeObjects (in .NET 4.5), you are still limited to 2,146,435,071 items max in a single dimension of the array.
(In pre-4.5, you are limited by 2gb for a single object, no matter how many entries it contains.)
That being said, a bool is represented by one byte, not one bit, so this will be quite a bit larger than you're expecting. That being said, you still only have 536,870,912 in your list when this fails, so theoretically, on a 64bit system with enough memory, the next allocation for growing the list should still be within the limits. However, this requires the program to succesfully allocate a single, contiguous chunk of memory large enough for the requested data (which will be 2x the size of the last chunk).
I am working on my homework assignment and I am completely stuck! What I am trying to do is to use already defined input and save it to the file by using saveDataTo() method and read the input by using readDataFrom() method.
I am stuck on the first part. I am not sure if I have to initialize the data in Program.cs file first?
I don't know and I am stuck. Here is code and hope for some tips how I can accomplish this.
-- EDIT --
I can add instructions for purpose of both saveDataTo() and readDataFrom() methods:
The saveDataTo( ) method takes a parameter of BinaryWriter. The method
writes the values of all 5 properties of an book object to a file
stream associated with the writer (the association is done in the
Main( ) method of Program class). There is no need to open and close
the file stream and binary writer inside this method.
The readDataFrom( ) method takes a parameter of BinaryReader. The
method reads the values of all five properties of the Book object from
a file stream associated with the reader (the association is done in
the Main( ) method of Program class). There is no need to open and
close the file stream and binary reader inside this method.
So that gives me a clue that I should use and assign the properties to be saved in the file there?
-- EDIT --
Updated the code there. I do have a problem with content that is being saved into the file. I am not being showed the price. Why is that?
ff.APublisherNameTitle FirstNameLastName
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Program
{
private const string FILE_NAME = "lab07.dat";
static void Main(string[] args)
{
//char ask;
/*
do
{
Console.Write("Enter Book Title: ");
publication.Title = Console.ReadLine();
Console.Write("Enter Author's First Name: ");
book.AuthorFirstName = Console.ReadLine();
Console.Write("Enter Author's Last Name: ");
book.AuthorLastName = Console.ReadLine();
Console.Write("Enter Publisher's Name: ");
publication.PublisherName = Console.ReadLine();
Console.Write("Enter Book Price: $");
publication.Price = float.Parse(Console.ReadLine());
Console.Write("Would like to enter another book? [Y or N] ");
ask = char.Parse(Console.ReadLine().ToUpper());
}
while (ask == char.Parse("Y"));
*/
Book book = new Book();
book.Price = 10.9F;
book.Title = "Title";
book.PublisherName = "PublisherName";
book.AuthorFirstName = "FirstName";
book.AuthorLastName = "LastName";
FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
BinaryWriter write = new BinaryWriter(fileStream);
book.saveDataTo(write);
write.Close();
fileStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader read = new BinaryReader(fileStream);
book.readDataFrom(read);
read.Close();
}
}
}
Publication.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Publication
{
private float price;
private string publisherName, title;
public float Price
{
get
{
return price;
}
set
{
price = value;
}
}
public string PublisherName
{
get
{
return publisherName;
}
set
{
publisherName = value;
}
}
public string Title
{
get
{
return title;
}
set
{
title = value;
}
}
public void display()
{
Console.WriteLine("{0}\n{1}\n{2}", title, publisherName, price);
}
}
}
Book.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Lab_7
{
class Book : Publication
{
private string authorFirstName, authorLastName;
public string AuthorFirstName
{
get
{
return authorFirstName;
}
set
{
authorFirstName = value;
}
}
public string AuthorLastName
{
get
{
return authorLastName;
}
set
{
authorLastName = value;
}
}
public new void display()
{
}
public string getAuthorName()
{
return authorFirstName + " " + authorLastName;
}
public void readDataFrom(BinaryReader r)
{
Price = r.ReadInt32();
PublisherName = r.ReadString();
Title = r.ReadString();
authorFirstName = r.ReadString();
authorLastName = r.ReadString();
}
public void saveDataTo(BinaryWriter w)
{
w.Write(base.Price);
w.Write(base.PublisherName);
w.Write(base.Title);
w.Write(AuthorFirstName);
w.Write(AuthorLastName);
}
}
}
Regards.
HelpNeeder.
You're asking whether to define the values in Program.cs or Book.cs, right? Well, it is fine to define the values in Program.cs, you just need to make sure all the values are given before writing the data.
So, since the function takes a BinaryWriter parameter that is supposedly initialized beforehand, this should work:
public void saveDataTo(BinaryWriter w)
{
w.Write(getAuthorName());
//etc...
}
But, just remember that you do need to define all the info somewhere (anywhere) before calling save data.
You assign your parameters to 2 different objects, see:
Publication publication = new Publication();
Book book = new Book();
Both are individual instances residing in memory.
You either have to refer the publication to the book like:
Book book = new Book();
Publication publication = (Publication)book;
or just assign the values currently assigned to the publication directly to the book so:
publication.PublisherName = "PublisherName";
becomes
book.PublisherName = "PublisherName";
Apart from that, you're working in C#, not Java. By convention its normal to start your methods with a Capital (Pascal Case)
EDIT
Your now shown the price when reaidng since you write it as a floating field (or double, cant see the definition) and read it as an integer.
Change from r.ReadInt32(); to r.ReadDouble(); or r.ReadSingle()