C
C#13mo ago
aca

❔ ✅ Why am I getting error message CS0184?

I am trying to compare the return type of a generic method, which produces the following warning. How does the IDE knows my delegate will never be of that certain return type?
No description
30 Replies
Jimmacle
Jimmacle13mo ago
because what that code is doing is checking if an instance of Type is an instance of int which will never be true you're looking for something like func.Method.ReturnType.IsAssignableTo(typeof(int))
aca
acaOP13mo ago
Could I switch the func.Method.ReturnType and handle casewise for certain datatypes?
Jimmacle
Jimmacle13mo ago
no, because types aren't constants
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle13mo ago
also as an aside that using (Con) looks suspicious, do you intend for this method to dispose of a connection it doesn't "own"?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
aca
acaOP13mo ago
I have the Con defined as my SqlConnection for the class and I am applying methods for handling CRUD functionality
Jimmacle
Jimmacle13mo ago
right, so if you call this you will dispose your sql connection and it won't work for any future calls
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
aca
acaOP13mo ago
I am still a newbie and my teacher also said usually this isn't how pushing into a SQL database is done, but it will has to do for now since I don't know any other way unfortunately Basically I am handling CRUD by opening, executing the SQL and closing the connection in many smaller methods
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
aca
acaOP13mo ago
And I tried to create a generic method accepting a delegate since I either insert a SqlCommand or SqlBulkCopy
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
aca
acaOP13mo ago
Was this how it was done earlier?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
aca
acaOP13mo ago
I have background in engineering so my logic is quite developed and I found this to be the most logical way of doing it with my knowledge so far
Jimmacle
Jimmacle13mo ago
the next level up in terms of abstraction would be using a library like dapper, then even more abstract would be ef core
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
aca
acaOP13mo ago
Makes sense, I just always try to make my methods generic which sometimes isn't working in my favour sadly
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle13mo ago
for example, i have this query using EF core and there is 0 actual SQL involved
No description
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
aca
acaOP13mo ago
I can recognize some LINQ - we have just starting delving into the topic. So basically nowadays one wants to avoid SQL, which is why LINQ is there (to encapsulate queries?)
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle13mo ago
you still want to understand the SQL, but libraries like EF core have the advantage of providing compile time safety to your queries by writing them in C# against C# models using linq
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle13mo ago
yeah, that screenshot is technically 2 queries that are composed of some common setup no sql is generated until one of the awaited methods is called then EF core will take that linq expression and figure out what the SQL query should be based on your model then send it and convert the response to whatever object type you asked for
Jimmacle
Jimmacle13mo ago
compared to something like dapper, where you still hand-write the SQL query but it can handle mapping objects to and from C# for you
No description
aca
acaOP13mo ago
I see - the subject is a bit out of scope for me, but I can understand a large part of it. It seems I just need to be able to put all information regarding the certain topic into a collective understanding Now this I can understand fully Anyways thanks for the help guys, appreciate it. Always great to learn something new !close
Accord
Accord13mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server