Jump to content

[EASY] C# Function/void that will access an object given in the arguments.

Go to solution Solved by Mr_KoKa,

You would like to have a pointer, but since it is C# then there is no pointers and you would need to wrap your int into object and pass that object to te function,t hen function can access such obiect's member and modify it, so it won't be only modified inside function but it will remain modified as object aren't passed by value but by reference.

lets say I have an integer declared like this:

 

int something = 0;

 

and I have a function that takes 1 argument like this:

private void Something(int someInt)
{
       someInt++;
}

private void Button1_Clicl()
{
       Something(something);
}

 

How can I make my function directly access the object I gave it in the argument? I don't want my function to +1 someInt from the argument, but the actual int something.

 

Why I need this? Cuz it would make my could shorter multiple times, because currently I'm checking each object through many if loops and if I could do it with a function with the given parameter it would be much easier.

CPU & COOLER - INTEL i9 12900K | NZXT KRAKEN X62

MBO - MSI Z690 EDGE

GPU - ASUS GEFORCE RTX 3080 TUF

RAM - 2x16GB KINGSTON FURY 3200MHZ

PSU - PHANTEKS REVOLT PRO 1000W 80 PLUS GOLD

SSD #1 - CORSAIR SN850 1TB

SSD #2 - CORSAIR SN850X 4TB

SSD #3 - INTEL 660p 2TB

CASE - PHANTEKS ECLIPSE P500A D-RGB

MOUSE #1 - LOGITECH G502 X Wireless

KEYBOARD - AKKO 5108B PLUS

SPEAKERS - SVS PB-1000 + 2x ADAM A3X

HEADSET #1 - SENNHEISER IE-300

HEADSET #2 - FOSTEX TR-X00 PURPLEHREART

AMP/DAC - MAYFLOWER ARC

OS - WINDOWS 10 PRO

DISPLAY 1 - LG OLED42C2

DISPLAY 2 - XIAOMI 2K GAMING

PHONE - SAMSUNG GALAXY S23+ 

Link to post
Share on other sites

You would like to have a pointer, but since it is C# then there is no pointers and you would need to wrap your int into object and pass that object to te function,t hen function can access such obiect's member and modify it, so it won't be only modified inside function but it will remain modified as object aren't passed by value but by reference.

Link to post
Share on other sites

Just now, Rakcom said:

C# uses References

 

10 minutes ago, Mr_KoKa said:

You would like to have a pointer, but since it is C# then there is no pointers and you would need to wrap your int into object and pass that object to te function,t hen function can access such obiect's member and modify it, so it won't be only modified inside function but it will remain modified as object aren't passed by value but by reference.

Wow, that is very nice of C#. Thanks!

CPU & COOLER - INTEL i9 12900K | NZXT KRAKEN X62

MBO - MSI Z690 EDGE

GPU - ASUS GEFORCE RTX 3080 TUF

RAM - 2x16GB KINGSTON FURY 3200MHZ

PSU - PHANTEKS REVOLT PRO 1000W 80 PLUS GOLD

SSD #1 - CORSAIR SN850 1TB

SSD #2 - CORSAIR SN850X 4TB

SSD #3 - INTEL 660p 2TB

CASE - PHANTEKS ECLIPSE P500A D-RGB

MOUSE #1 - LOGITECH G502 X Wireless

KEYBOARD - AKKO 5108B PLUS

SPEAKERS - SVS PB-1000 + 2x ADAM A3X

HEADSET #1 - SENNHEISER IE-300

HEADSET #2 - FOSTEX TR-X00 PURPLEHREART

AMP/DAC - MAYFLOWER ARC

OS - WINDOWS 10 PRO

DISPLAY 1 - LG OLED42C2

DISPLAY 2 - XIAOMI 2K GAMING

PHONE - SAMSUNG GALAXY S23+ 

Link to post
Share on other sites

I thought you can use references...

 

Spoiler

CPU:Intel Xeon X5660 @ 4.2 GHz RAM:6x2 GB 1600MHz DDR3 MB:Asus P6T Deluxe GPU:Asus GTX 660 TI OC Cooler:Akasa Nero 3


SSD:OCZ Vertex 3 120 GB HDD:2x640 GB WD Black Fans:2xCorsair AF 120 PSU:Seasonic 450 W 80+ Case:Thermaltake Xaser VI MX OS:Windows 10
Speakers:Altec Lansing MX5021 Keyboard:Razer Blackwidow 2013 Mouse:Logitech MX Master Monitor:Dell U2412M Headphones: Logitech G430

Big thanks to Damikiller37 for making me an awesome Intel 4004 out of trixels!

Link to post
Share on other sites

2 minutes ago, Mr_KoKa said:

Another thing I can think of is just return value and assign it back to the variable you want to modify. So oy pass value to function, moify it and return it back and assign it back to the variable.

It would be harder doing it through a function unfortunately...

CPU & COOLER - INTEL i9 12900K | NZXT KRAKEN X62

MBO - MSI Z690 EDGE

GPU - ASUS GEFORCE RTX 3080 TUF

RAM - 2x16GB KINGSTON FURY 3200MHZ

PSU - PHANTEKS REVOLT PRO 1000W 80 PLUS GOLD

SSD #1 - CORSAIR SN850 1TB

SSD #2 - CORSAIR SN850X 4TB

SSD #3 - INTEL 660p 2TB

CASE - PHANTEKS ECLIPSE P500A D-RGB

MOUSE #1 - LOGITECH G502 X Wireless

KEYBOARD - AKKO 5108B PLUS

SPEAKERS - SVS PB-1000 + 2x ADAM A3X

HEADSET #1 - SENNHEISER IE-300

HEADSET #2 - FOSTEX TR-X00 PURPLEHREART

AMP/DAC - MAYFLOWER ARC

OS - WINDOWS 10 PRO

DISPLAY 1 - LG OLED42C2

DISPLAY 2 - XIAOMI 2K GAMING

PHONE - SAMSUNG GALAXY S23+ 

Link to post
Share on other sites

I don't know how complex is your case but according to your example it would look like this:

 

private void Something(int someInt)
{
       someInt++;
       return someInt;
}

private void Button1_Clicl()
{
       something = Something(something);
}

 

Link to post
Share on other sites

7 minutes ago, Mr_KoKa said:

I don't know how complex is your case but according to your example it would look like this:

 


private void Something(int someInt)
{
       someInt++;
       return someInt;
}

private void Button1_Clicl()
{
       something = Something(something);
}

 

Yes it is much more complex, I'm using multiple parameters. I would have bunch of return's if I used a function since I have a lot of if statements.

CPU & COOLER - INTEL i9 12900K | NZXT KRAKEN X62

MBO - MSI Z690 EDGE

GPU - ASUS GEFORCE RTX 3080 TUF

RAM - 2x16GB KINGSTON FURY 3200MHZ

PSU - PHANTEKS REVOLT PRO 1000W 80 PLUS GOLD

SSD #1 - CORSAIR SN850 1TB

SSD #2 - CORSAIR SN850X 4TB

SSD #3 - INTEL 660p 2TB

CASE - PHANTEKS ECLIPSE P500A D-RGB

MOUSE #1 - LOGITECH G502 X Wireless

KEYBOARD - AKKO 5108B PLUS

SPEAKERS - SVS PB-1000 + 2x ADAM A3X

HEADSET #1 - SENNHEISER IE-300

HEADSET #2 - FOSTEX TR-X00 PURPLEHREART

AMP/DAC - MAYFLOWER ARC

OS - WINDOWS 10 PRO

DISPLAY 1 - LG OLED42C2

DISPLAY 2 - XIAOMI 2K GAMING

PHONE - SAMSUNG GALAXY S23+ 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×