❔ beginner - is it possible to pass a prop to a function as parameter?
I'm writing my first "as much as possible professional-structured" project, and I'd like to optimise the code by passing parameters to a function for avoid having to write multiple time the same function
I actually have this situation (first 3 tabs) and I tried to make this (tab 4 and 5) but seems not working, maybe it's not possible
https://paste.mod.gg/llauewhtrrrz/4
Hoping it's clear enough
BlazeBin - llauewhtrrrz
A tool for sharing your source code with the world!
77 Replies
It’s not clear
Simplify it down to only the problem at hand
What are you trying to do and what have you attempted that’s not working
ok, I try to simplify
I have this, it call Delete(id) passing an int field that indicate the PrimaryKey for delete the row from a DB
And this is what Delete(id) do
Since I have multiple tables, I'd like to pass to Delete(id) another field, or better property, that replace Anagraphic with another thing
Imagine I have 10 snippets like this with every table, if I don't find a way to pass the table name "Anagraphics" in this case, I'll have to create 10 other snippets like Delete(id)
You can make a generic method that takes a DbSet
void Foo<T>(DbSet<T> set)
Foo(context.SomeTable);
The generic type is determined automatically based on the parameter givenyou mean to put this replacing the current Delete() function?
I tried with this but it generate error CS0452, and in most of cases error descriptions are uninterpretable :/
The type 'T' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'DbSet<Entity>'
add a
where
clause
but if you're a beginner I suggest you don't jump into web frameworks
you clearly don't understand so so many things yet
learn the naming conventions and SOLID, then also DI in ASP.NET Core as well as EF Core basics
static vs instance alsoin which point do you mean?
are WinForms better for learning?
like you clearly don't understand generics here, go learn them. you can't use them without that knowledge!
no just console apps. pick up a book or something
visual apps might work too
but not GUI apps
these have a very specific flavor and require the knowledge of the basics I'd argue
you can't cut corners in this
if you skip some topic, it will later be required and the fact that you skipped it will bite you and you'll have to learn it anyway (if it's one of the basics)
In the past I coded WinForms tools using VB.NET and I made lot of mini tools without problem and without knowing objects, but every time I googled or asked support about code I was always unable to understand the code.
Then I decided to take time and follow a course I found made by 40 videos, it was named as C# Full Course and was splitted into 3 difficulty level arguments
1) Sintax, Var & Constants, Data Type, Casting, Strings, Numbers, User Input, Try & Catch, Logical Operator and Compartor, If Else, Ternary Operator, Switch, For & Foreach, Break & Continue
2) Methods, Methods overload, Array, Multidimensional Array 2D 3D, Irregular Array, Arraylist, List, Hashtable, Dictionary, Stack, Queue
3) OOP, Class Constructor, Getters & Setters, Keyword statis, Hereditary, Virtual & Override meaning
After learned those stuffs I thought I was able to learn code with helping of a community but after this course the code that people suggest to me still appears incomprehensible and totally different from what I studied :/
well it seems you didn't fully understand some things
e.g. why isn't that method static?
or why aren't you at least injecting an instance of that class in the constructor?
why aren't you injecting the db context, which is request-lived
you're using the synchronous versions of the methods
use async await
the local variables should be camel case
etc.
DI is a fundamental asp net core thing, you have to understand it
Cause in the course I followed I didn't heard about this injection, and I also tried to find explanation about DI but I was unable to learn it due to complexity of explanations
About microsoft documentation, well, they make hard to understand also how to print a string
pick up a book on asp net core then, books usually have more beginner friendly explanations than docs
+ they're more systematic, making sure you don't end up with gaps in the knowledge
learn generics separately too, and dependency inversion, which is the basis for dependency injection
your course doesn't seem to have covered those
but like thing is you have to know way more than an average beginner to take on asp net core imo
no, it was well explained but after the course I was still unable to code
so it's ok that you don't know this after that course
idk about books, since I never bought a book having google, maybe I could pay someone for private lessons 😅
look up some free PDF, in my experience radom free books were good enough for me
even though I haven't used them for asp net core
google like "asp net core book pdf"
the main point is that I wanted to learn C# since I always used VB.NET with WinForms without learning coding
well yeah then don't go into asp net core
so knowing C# will not let to be able to code everything in C#?
it's its own area
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
well it's the basis of everything, but they require some other specific knowledge too, and there are in itself frameworks within frameworks, so there are abstractions to learn
also, you kinda think a bit differently depending on the type of program you're writing
games vs gui vs web app have their own different kinda feel and thinking process to them
even though they all share fundamentals like classes methods and abstraction
do you know some reliable guide? maybe I'm missing this and I'm doing it in the wrong way.
I think I made lot of things much harder than this but every single human I know seems to know this C# as if this was extremely easy, but every time I try to learn it I get stuck somewhere 😕
no programming is not easy
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah that's a way, but for a beginner a systematic approach is usually less overwhelming
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I know and it was my learning method referring to everything
But in this case every time I get stuck on something and I google it I got incomprehensible answers
For example I start writing some apps for make practice, and my idea is, if I get stuck on something I ask and I'll learn by answers I get, but it's not so cause I always get piece of codes that I don't understand
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I was able even to learn and make reverse engineering using google and time, but regarding to coding, it's like everyone code in a different way, hard to understand
yeah the method is that you google recursively, until you end up understanding the related subconcepts, in order to understand the main concept
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Before finding this discord I tried to asking my doubts on stackoverflow, but they made hard also the print of a string xD
So I thought to find a discord since it's faster and more efficient
yeah use discord mainly for asking "what do I need to learn in order to understand this"
yeah I think that's the point
in some cases you can also get answers here to some niche topics
I succesfully coded WinForm apps without knowing almost anything of OOP and I think that's was the problem
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
for example, in this case I asked a suggestion on how to proceed and I got an answer saying to use this
void Foo<T>(DbSet<T> set)
Foo(context.SomeTable);
it was like using google, but I got stuck anyway since I got a foreign error on VS
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
because you don't know generics. that T is unconstrained in your method, but it is constrained in the DbSet declaration
yeah, my app is currently working as I coded it, but my exercise was to optimise the code reducing code lines, so I thought to create a generic method instead to having 10 identical methods that make the same thing on 10 different db table
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Considering the course I followed explained those concepts:
1) Sintax, Var & Constants, Data Type, Casting, Strings, Numbers, User Input, Try & Catch, Logical Operator and Compartor, If Else, Ternary Operator, Switch, For & Foreach, Break & Continue
2) Methods, Methods overload, Array, Multidimensional Array 2D 3D, Irregular Array, Arraylist, List, Hashtable, Dictionary, Stack, Queue
3) OOP, Class Constructor, Getters & Setters, Keyword statis, Hereditary, Virtual & Override meaning
Which are next arguments should I learn? or first, did it miss something earlier?
generics
solid
ok
can I ask you how do you learned coding?
I mean which was your global tour, if you studied somewhere, or by yourself, or through private lesson, by books, by google, or multiple from those I wrote.
I learned Java then JavaScript, html, css, then c# by coding a game an getting a feel for things via experimentation
java is from a random book
javascript, html, css through random stuff on the internat
oh, so I suppose that you learned C# very fast having already learned other languages
about java you learned it using only a book like informations source?
my case is not really majority, because I had a solid math background going into programming, so I already the programming thinking ability
the problem solving
but yeah basically I went through half of the book, got the basics, and then proceeded on my own
set
is a reserved keyword so you can't use it as a variable name. If it's blue, it most likely means it's a keyword. You can change it to @set
, or use another name
And please, as I mentioned yesterday, get used to c# in general and how Dependency Injection works. You're still creating it all manually which is time consuming and this delete method is pretty useless since this is all build in by EntityFrameworkyeah I realized that after a while but it gave me error also changing to a random name
I wanted to first end this app.
Btw I seen few videos about DI but they was not clear cause they talked about other foreign arguments.
So today I'm trying to find another C# guide for try learn things that were missing in the course I followed, hoping to find something written differently as in the microsoft documentation.
Not sure what you mean with foreign arguments, but a lot of your app's problems come down to how you set it up and this includes DI. So I would really recommend you get used to it
I'm now reading W3Schools guide about C# but it seems a bit poor and also, it's not so clear
I mean, I followed a course on YT about C# and I learned what a Class is, very easy concept.
Now, the same concept of class, looks abstract to me if I read it from this guide: https://www.w3schools.com/cs/cs_classes.php
W3 is pretty shit and outdated tbh
You're better off reading tutorials specifically from 2022/2023
well it seems those interfaces are pretty related to DI, so I have to get a look into interfaces
They're not required, but very useful in general. Generally you define an interface to show what is allowed to be used, and you implement it in a class. You then unject them
ok, I added it to pre-requirements
all videos talking about DI mention this logger, and interfaces
Ehhhh
ILogger is something you can inject
If that is what you mean
oh so it's just a common example
arghhh I think I'll start from scratch
I found a PDF here https://www.computer-pdf.com/programming/csharp/
it's 1000 pages but it looks like use a random order
Free PDF courses and tutorials on CSharp language
Tutorials on CSharp language, C#, CSharp computer programming language in PDF - page 1
it have access modifiers as chapter 3, then accessing db as chapter 5, but array as chapter 10 and variable types (int long short char etc) only later as chapter 21 🧐
btw I think it's old book also, it report 7.0 as last version, but wiki says 11.00 is
c# 7 is .NET Framework era
That's really old
ok so I'll have to find something about C# 10 at least
maybe also this was related to old C# since here I can do it
now that's clear what I have to do I think I'll open a new thread for not go off topic
Ehh, nothing prevents you from creating multiple methods with the same name
You simply made an overload now
But the app is only gonna start the top one so it does not matter if you have multiple. It uses the one with the string array
yeah I imagine that the entry point will always be the first one
but the fact I don't understand is why the book say I can't create multiple Main methods at this point
If I put the same arguments type and count I can't create multiple methods, regardless if I name it Main or Banana xD
maybe it wanted just to intend that I can't have more entry points
maybe they meant multiple classes with a main with that signature
which is indeed not allowed by default
c# has several specific 'forms' of main it will recognise
none of these take in an
int
parameter
so your second main cannot be confused for a 'real' mainbtw I switched to another post since it will be off topic here
https://discordapp.com/channels/143867839282020352/1063457227064742088
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.