C
C#12mo 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
Buddy12mo ago
$details
MODiX
MODiX12mo 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
Buddy12mo ago
You can't name a method 'new' it is a reserved name
ZSolarDev
ZSolarDevOP12mo ago
oh then how do I use it? im trying to make it so you can use it like new Sprite(stuff here)
Buddy
Buddy12mo ago
Use a class
ZSolarDev
ZSolarDevOP12mo ago
im using a class
Buddy
Buddy12mo ago
Please post the code
ZSolarDev
ZSolarDevOP12mo 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
Buddy12mo ago
use the constructor
ZSolarDev
ZSolarDevOP12mo ago
can you explain
Buddy
Buddy12mo 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
Buddy12mo ago
This can 🙂
ZSolarDev
ZSolarDevOP12mo ago
OOH
Buddy
Buddy12mo ago
That allows you to do exactly what you want.
ZSolarDev
ZSolarDevOP12mo 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
Buddy12mo ago
Correct
ZSolarDev
ZSolarDevOP12mo ago
tysm for responding so quickly
Buddy
Buddy12mo ago
Anytime.
ZSolarDev
ZSolarDevOP12mo ago
i literally have to go in 30 mins :>
Want results from more Discord servers?
Add your server