✅ Can someone help me understand GetType()
Do I have to instantiate an object in order to use GetType()
29 Replies
GetType is an instance method.
You can do
this.GetType() or just GetType().Yeah, you do need to make an object first if you wanna use GetType(). Its an instance method, so you cant call it directly on the class itself, Inside a class though, you can Just write GetType() and itll automatically refer to that object
I will attempt both solutions at once!
@jcotton42 @Gismo
Did I do that incorrectly?
Error output
Okay so it is saying I cannot convert an object into a string I must access its properties. such as .Name
GetType is an instance method, so, would I have to call the class and make an object? If so I could use it, but the requirements want me to have it returned in the class. So I have a catch 22. I cannot use the method without instantating an object and I cannot return an instianted object within a class. This problem is confusing.
I am going to attempt to use the this.
I see your post @Timmy I am on it
You cannot implicitly convert a type to a string, this is because a string in C# is a type, thus, you must convert it through the "ToString" virtual method. The error stems from
string className = GetType();, you can simply convert System.Type (which GetType() returns) to a string through it's ToString() virtual method like so:
that makes sense, I must specify to C# that I want a string and GetType() does not return a string type.
It returns System.Type which is some type I have never heard of but its not a string so I need a convertor hence ToString()
Console.WriteLine(className) is printing the string className to console and then returning void (nothing). What you would want to do is simply return className or GetType().ToString(), and then use Console.WriteLine(certifiedLetter.ToString())So the way to diagnose this error is say okay what is being generated by my code currently, and how do i convert it to the correct type.
In full:
Essentially. If the error shows more then that would be useful to show and I may be able to help further, but yes, it is saying that it cannot convert from type A to type B, and it gives the error code
cs0029 which you can look up and find a few examples (usually).posting now
Lastly, implicitly in coding typically refers to when you simply write it, and it automatically converts from type A to type B without having to cast to another object, or use a function to convert it.
string className = GetType().ToString(); is unecessary here in ToString().output
project "/workspaces/9780357429235_farrell-c-sharp-aaba885a-f662-4800-a980-c6b80f6b520c/chapter10/ex01/student/LetterDemo.csproj"
LetterDemo+Letter
Unless you want clarity, then in that case you can keep it and simply return className instead of
return this.GetType().ToString();
The reason that you see the + inbetween is because it's how C# represents generic types (when you used : Letter). + is not allowed for naming variables or types, so you can split the string by + and return the first index in the array if you would prefer to exclude any inheriting types.
Example:
its complaining about nested functions. Do I have to define Main() last or do I need to utilize a namespace
one sec
@Timmy I think I have a better understanding. Do you have any reccomendations for learning C# that i can review? like youtube channels, udemy or books?
@Timmy @Gismo @jcotton42 Thank you everyone! 🙂
Sorry for the delay lol, did you get the nested function issue sorted?
well kind of
np
What's the error?
Status: FAILED!
Check: 1
Test: Letter
ToString override
Reason: Unable to run tests.
Error : str - AssertionError
Timestamp: 2025-10-16 23:44:46.670457
Status: FAILED!
Check: 1
Test: Letter ToString override
Reason: Unable to run tests.
Error : str - AssertionError
Timestamp: 2025-10-16 23:44:53.968062
it seems that they are indented but i changed the brackets so that only main was in letter demoLearn what you want to learn as in if you want to make games, make games, you'll learn best by doing, and don't be afraid of google, google is your friend. And if you want to get good at interviews and being good at coding for jobs, and less for a hobby, then maybe do leetcode problems.
Personally I would recommend because it's best for motivation and learning things quickly, but doesn't always mean you learn everything you need to.
Myself personally started with Unity and then began moving towards custom game engines because it's what seems fun to me, and it's where I want to pursue most.
Nested classes are when the class is inside a class due to brackets. so do I just have the ltter demo class encompass main, letter, and certified letter
If you notice,
letter.ToString() here is not defined. This is because you defined ToString() in the CertifiedLetter class but not the Letter class.the base class i see.
so move the method to the base class and that takes care of the base class than i can call the method in the child class
Status: FAILED!
Check: 1
Test: Letter
ToString override
Reason: StringAssert.Contains failed. String 'Letter' does not contain string 'Electric Company'. Can not find the approriate Name.
Error : str - AssertionError
Timestamp: 2025-10-16 23:53:22.027279]
Status: FAILED!
Check: 1
Test: Letter ToString override
Reason: StringAssert.Contains failed. String 'CertifiedLetter' does not contain string 'Electric Company'. Can not find the approriate Name.
Error : str - AssertionError
Timestamp: 2025-10-16 23:53:30.837636\
it seems the test uses an electric company whatever. i have a gpt answer that fits the requirements but i think i have learned the main idea of the exercise.
fighting with a picky linter for 4 hours is very undesirable
Apparently this is the answer?
thank you again.
!closeClosed!
is there a command for keeping track of the questions and answers
!summary
No.
That would likely involve AI usage and thus cost money to host, and this entire discord is entirely unsponsored and volounteer driven, so sadly thats not available. A good idea thou.
Wouldn't it be good to simply have a command for posts in the #help channel to mark a post as completed, and include a title and message as the final answer? Then you could sorta lookup posts that were already closed via some other command. That being said, that's essentially stack overflow or google..
Well, most questions we get here are more complicated than that, and the answer is very rarely a single post. So either the asker, a helper or an AI has to write a summary regardless
!close
Closed!