C
C#11mo ago
ZSolarDev

✅ Im kinda new to c# and I don't know how to use the new function...

I'm trying to make an object with a new() function but I keep getting "Member modifier 'new' must precede the member type and name". I'm using public void new()
19 Replies
Buddy
Buddy11mo ago
$details
MODiX
MODiX11mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Buddy
Buddy11mo ago
You can't name a method 'new' it is a reserved name
ZSolarDev
ZSolarDev11mo ago
oh then how do I use it? im trying to make it so you can use it like new Sprite(stuff here)
Buddy
Buddy11mo ago
Use a class
ZSolarDev
ZSolarDev11mo ago
im using a class
Buddy
Buddy11mo ago
Please post the code
ZSolarDev
ZSolarDev11mo ago
ok
public void new(int x, int y, Texture2D tex, Color tint){
//stuff here
}
public void new(int x, int y, Texture2D tex, Color tint){
//stuff here
}
ofc i cant write anything in the function bc the function wont even work
Buddy
Buddy11mo ago
use the constructor
ZSolarDev
ZSolarDev11mo ago
can you explain
Buddy
Buddy11mo ago
Constructors - C# programming guide - C#
A constructor in C# is called when a class or struct is created. Use constructors to set defaults, limit instantiation, and write flexible, easy-to-read code.
Buddy
Buddy11mo ago
This can 🙂
ZSolarDev
ZSolarDev11mo ago
OOH
Buddy
Buddy11mo ago
That allows you to do exactly what you want.
ZSolarDev
ZSolarDev11mo ago
so this?
public Sprite(int x, int y, Texture2D tex, Color tint){
this.x = x;
this.y = y;
this.tex = tex;
this.tint = tint;
}
public Sprite(int x, int y, Texture2D tex, Color tint){
this.x = x;
this.y = y;
this.tex = tex;
this.tint = tint;
}
Buddy
Buddy11mo ago
Correct
ZSolarDev
ZSolarDev11mo ago
tysm for responding so quickly
Buddy
Buddy11mo ago
Anytime.
ZSolarDev
ZSolarDev11mo ago
i literally have to go in 30 mins :>
Want results from more Discord servers?
Add your server