C
C#17mo 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
Guns17mo 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
Buddy17mo ago
Please paste your code as this your code is unreadable, because it misses indentation. $paste
MODiX
MODiX17mo 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
Guns17mo ago
$paste
MODiX
MODiX17mo 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
Guns17mo ago
pasted here yes ik sry facepalm
Kouhai
Kouhai17mo ago
This code won't even compile
amio
amio17mo ago
The pasted version is just as bad
Guns
Guns17mo ago
my bad please have a look at this one
Guns
Guns17mo ago
BlazeBin - ysyvcuznypfv
A tool for sharing your source code with the world!
Guns
Guns17mo ago
@amio @Kouhai @Buddy
Kouhai
Kouhai17mo ago
Well
public int Count()
{
return stack.Length;
}
public int Count()
{
return stack.Length;
}
What do you think this means?
Guns
Guns17mo ago
number of items in the array?
Kouhai
Kouhai17mo 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
Guns17mo ago
hmm makes sense so i could just return the number of items that have been pushed
Kouhai
Kouhai17mo ago
Exactly, you also should add some safe guards in both Push and Pop
Guns
Guns17mo ago
yes i noticed that
Guns
Guns17mo ago
Guns
Guns17mo ago
what about this part?
Kouhai
Kouhai17mo 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
Guns17mo ago
what do I need to modify?
Kouhai
Kouhai17mo ago
What do you think should be modified? If you want to allow T to be pushed and popped
Guns
Guns17mo ago
hmmmmm i dont see it
Kouhai
Kouhai17mo ago
Do you know how generics work in C#?
Guns
Guns17mo ago
not entirely
Kouhai
Kouhai17mo ago
I don't mean how it internally works, just how to make a class/method generic
Guns
Guns17mo ago
i kinda forgot ngl been a while string[] to T?
Kouhai
Kouhai17mo ago
Well, that won't really work Because string[] is an array of strings
Guns
Guns17mo ago
hmm could you please point where the error is?
Kouhai
Kouhai17mo 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
Guns17mo ago
an int?
Kouhai
Kouhai17mo ago
Yup, exactly
Guns
Guns17mo ago
ohhh i get your point but how does this work with my array?
Kouhai
Kouhai17mo 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
Guns17mo ago
thanks! GOAT