Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Call class with argument/variable? [Solved[

So essentially I want to call a class to run my code but with a custom variable passed in.

PHP Code

<?php 
public class AddItem(a, b)
        {
            
a.Items.Add(b);
        } 

a being a defined listbox and b being an item to add into the defined listbox.

Any suggestions?

EDIT: Just remembered about Methods - I assume this is what I am looking for.

This is what I am testing right now.
PHP Code

<?php 
public void addItems(ListBox lb, string input)
        {
            
lb.Items.Add(input);
        } 
What you posted should be allright? you could make it a static extension method by doing

PHP Code

<?php 
public static void addItems(this ListBox lb, string input)
{
    
lb.Items.Add(input);
} 

so you could use it as

PHP Code

<?php 
var listbox = new ListBox();
listbox.addItems("stuff") 

note that it has to be inside a static class to be used tho.
and also, doing some simple stuff like replacing Items.Add() is pretty useless, as it only replaces one line with another line. but if you do this with methods that have more content, it can be really helpful.
the name is also slightly misleading, as it says "items", instead of "item", but yeah.

EDIT:
as in the first non-working example, you are using classes, they are not really used as methods, but more like groups and/or instances of something. but if you want to execute code when creating an instance of a class, you could use constructors. Here's an example of a simple one. this isnt good for creating some simple listbox adding thing, but if you want a coordinator ie, you could use constructor to set its starting coordinates.

PHP Code

<?php 
public class Coordinator
{
    private 
int _xstart;
    private 
int _ystart;

    public 
Coordinator(int x, int y) //note how it has no return type and how its the same name as the class. overloads are possible. arguments are optional ofc.
    
{
        
_xstart = x;
        
_ystart = y;
    }
} 

EDIT2:
also, i didnt notice the [solved] in title, my bad. but hopefully you got something out of my post :/
eh...


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[Shell] by Nulla MisericordiaFeb 14, 2020 02:23 PM 0 163 Feb 14, 2020 02:23 PM
Last Post: Nulla Misericordia
[.NET] by AjummaApr 3, 2019 09:51 AM 5 332 Apr 3, 2019 09:51 AM
Last Post: itcorp