C
C#2y ago
Guns

need help with a stack class implementation

using System;
namespace CustomStackApp
{
class Program
{
static void Main ()
{
CustomStack<string>
testStack = new
CustomStack<string> 0;
testStack.Push("Jedi-X");
testStack.Pop ();
testStack.Push("Jedi");
testStack.Push("SkillsLab");
while (testStack.Count()
> 0)
Console.WriteLine(testStack.Po
p();
Console. ReadLine();

CustomStack<Jedi>
testStack2 = new
CustomStack<Jedi> (;
testStack2.Push("Skylord", "001");
testStack2.Push("Dark
Vador", "O02");
testStack2.Push("Han
Solo", "003");
}
}
class CustomStack <T>
{
static int MAXSIZE = 10;
string I stack = new
string [MAXSIZE];
int index;
public void Push (string value)
{
stack[index] = value;
index++;
}

public string Pop ()
{ index--;
return stack[index];;
}
public int Count ()
{
return stack.Length;
}
}
class Jedi
{
private string name;
private string ld;
public string Nameld
{
get
{
return $"Jedi -{name} :
{ld}";
}
set
{
Nameld = value;}}

class Jedi
{
private string name;
private string Id;
public string Nameld
{
get
{
return $"Jedi -{name} :
{ld'";
}
set
{
Nameld = value;
}
public Jedi (string _name, string.
Id)
{
name = _name;
ld = _Id;
}
}
using System;
namespace CustomStackApp
{
class Program
{
static void Main ()
{
CustomStack<string>
testStack = new
CustomStack<string> 0;
testStack.Push("Jedi-X");
testStack.Pop ();
testStack.Push("Jedi");
testStack.Push("SkillsLab");
while (testStack.Count()
> 0)
Console.WriteLine(testStack.Po
p();
Console. ReadLine();

CustomStack<Jedi>
testStack2 = new
CustomStack<Jedi> (;
testStack2.Push("Skylord", "001");
testStack2.Push("Dark
Vador", "O02");
testStack2.Push("Han
Solo", "003");
}
}
class CustomStack <T>
{
static int MAXSIZE = 10;
string I stack = new
string [MAXSIZE];
int index;
public void Push (string value)
{
stack[index] = value;
index++;
}

public string Pop ()
{ index--;
return stack[index];;
}
public int Count ()
{
return stack.Length;
}
}
class Jedi
{
private string name;
private string ld;
public string Nameld
{
get
{
return $"Jedi -{name} :
{ld}";
}
set
{
Nameld = value;}}

class Jedi
{
private string name;
private string Id;
public string Nameld
{
get
{
return $"Jedi -{name} :
{ld'";
}
set
{
Nameld = value;
}
public Jedi (string _name, string.
Id)
{
name = _name;
ld = _Id;
}
}
35 Replies
Guns
GunsOP2y ago
Having some issues with this code I’m getting and out of index error when executing And i am not sure how to create an instance of the class with the strongly type class Jedi as a type
Buddy
Buddy2y ago
Please paste your code as this your code is unreadable, because it misses indentation. $paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Guns
GunsOP2y ago
$paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Guns
GunsOP2y ago
pasted here yes ik sry facepalm
Kouhai
Kouhai2y ago
This code won't even compile
amio
amio2y ago
The pasted version is just as bad
Guns
GunsOP2y ago
my bad please have a look at this one
Guns
GunsOP2y ago
BlazeBin - ysyvcuznypfv
A tool for sharing your source code with the world!
Guns
GunsOP2y ago
@amio @Kouhai @Buddy
Kouhai
Kouhai2y ago
Well
public int Count()
{
return stack.Length;
}
public int Count()
{
return stack.Length;
}
What do you think this means?
Guns
GunsOP2y ago
number of items in the array?
Kouhai
Kouhai2y ago
Well, the array size doesn't change does it? So it always holds the same amount of elements no matter if it's actually populated or not. Image this view of the array with 5 elements var arr = new string[5] if we examine the array it's basically [null, null, null, null, null] We then push ["my_string", null, null, null, null] The array length doesn't change
Guns
GunsOP2y ago
hmm makes sense so i could just return the number of items that have been pushed
Kouhai
Kouhai2y ago
Exactly, you also should add some safe guards in both Push and Pop
Guns
GunsOP2y ago
yes i noticed that
Guns
GunsOP2y ago
Guns
GunsOP2y ago
what about this part?
Kouhai
Kouhai2y ago
Well, your implementation isn't generic
class CustomStack <T>
{
static int MAXSIZE = 10;
string[] stack = new string[MAXSIZE];
int index;
public void Push(string value)
{
stack[index] = value;
index++;
}
public string Pop()
{
index--;
return stack[index];
}
public int Count()
{
return stack.Length;
}
}
class CustomStack <T>
{
static int MAXSIZE = 10;
string[] stack = new string[MAXSIZE];
int index;
public void Push(string value)
{
stack[index] = value;
index++;
}
public string Pop()
{
index--;
return stack[index];
}
public int Count()
{
return stack.Length;
}
}
This isn't generic
Guns
GunsOP2y ago
what do I need to modify?
Kouhai
Kouhai2y ago
What do you think should be modified? If you want to allow T to be pushed and popped
Guns
GunsOP2y ago
hmmmmm i dont see it
Kouhai
Kouhai2y ago
Do you know how generics work in C#?
Guns
GunsOP2y ago
not entirely
Kouhai
Kouhai2y ago
I don't mean how it internally works, just how to make a class/method generic
Guns
GunsOP2y ago
i kinda forgot ngl been a while string[] to T?
Kouhai
Kouhai2y ago
Well, that won't really work Because string[] is an array of strings
Guns
GunsOP2y ago
hmm could you please point where the error is?
Kouhai
Kouhai2y ago
Let's step back a bit
class Foo<T>
{
public T Value { get; set; }
}

var bar = new Foo<string>();
bar.Value = "test";

var baz = new Foo<int>();
baz.Value = ????
class Foo<T>
{
public T Value { get; set; }
}

var bar = new Foo<string>();
bar.Value = "test";

var baz = new Foo<int>();
baz.Value = ????
In this example what value(s) can be set to baz's Value?
Guns
GunsOP2y ago
an int?
Kouhai
Kouhai2y ago
Yup, exactly
Guns
GunsOP2y ago
ohhh i get your point but how does this work with my array?
Kouhai
Kouhai2y ago
In a generic class/method T is just a type, you can do whatever you want with So something like T[] is possible
Guns
GunsOP2y ago
thanks! GOAT
Want results from more Discord servers?
Add your server