Jump to content



Photo

[C#] Can you select a public variable by a variable


  • Please log in to reply
10 replies to this topic

#1 DrakeN2k

DrakeN2k

    Neowinian

  • Joined: 04-December 10

Posted 13 December 2012 - 21:29

hello, i would like to pick a public veritable such as CLASSNAME.>THIS<

where this is a variable.


public PowerObject(string _type, int _number)
{
description = UpgradeList._type;
}

where type is a name of veriable which matches what is in upgradeList.

This c# project is in XNA.


#2 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 5
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 13 December 2012 - 21:34

EDIT: Disregard this post. I'll keep the content, but it's wrong. I misunderstood the question.

Depends one what "UpgradeList" is. If UpgradeList is a class name, then you'll have to make _type static as well as public. If UpgradeList is a variable name, then your code is fine as long as it's public.

HOWEVER, public variables are bad form. You should prefer properties over public variables.

#3 lunamonkey

lunamonkey

    Ten years on Neowin.

  • Joined: 28-May 03
  • Location: Swindon, England

Posted 13 December 2012 - 21:44

It might be better to do some sort of hash table or dictionary where you can do

description = UpgradeList[_type];

or

description = UpgradeList.getByValueKey(_type)

Also, why do you need a function to perform this, why bother wrapping it up?

#4 notchinese

notchinese

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 04-October 12

Posted 13 December 2012 - 21:51

I think he is asking about Reflection. Which, yes, you can do. However depending on what you are trying to accomplish it probably isn't necessary.

#5 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 5
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 13 December 2012 - 22:02

I think he is asking about Reflection. Which, yes, you can do. However depending on what you are trying to accomplish it probably isn't necessary.


Ah indeed you might be correct. I misunderstood the question. :)

#6 OP DrakeN2k

DrakeN2k

    Neowinian

  • Joined: 04-December 10

Posted 14 December 2012 - 10:14

I just want to create around 20 buttons each with pre done text , values , positions of the button.

But I want the button to be a object.

Just want to know the best way to to do this.

#7 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 6
  • Joined: 26-October 05

Posted 14 December 2012 - 15:17

I just want to create around 20 buttons each with pre done text , values , positions of the button.

But I want the button to be a object.

Well it's not difficult to have something be an object, you just create a class that defines what that thing is and instantiate it.

class Button {
	// what a button is and does goes here
}

...

var myButton = new Button(); // button object!

Of course the tricky part is implementing a button that works and responds to mouse clicks. You'll have to define bounds in x and y for the button, and check whether the cursor is within these bounds when the user clicks.

#8 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 5
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 14 December 2012 - 15:27

Of course, there's no point duplicating existing functionality if you can help it:
  • http://social.msdn.microsoft.com/Search/en-US/?query=button%20class&ac=8
  • http://www.mono-project.com/GtkSharp:_Buttons


#9 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 6
  • Joined: 26-October 05

Posted 14 December 2012 - 15:29

@Majesticmerc the project is XNA so integrating windowing toolkits is not trivial.

#10 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 5
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 14 December 2012 - 15:59

@Majesticmerc the project is XNA so integrating windowing toolkits is not trivial.


Fair point. Does XNA not have it's own button class though? I'd have thought that would been immensely useful for UI designs, although my knowledge of XNA is limited so I wouldn't know.

#11 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 6
  • Joined: 26-October 05

Posted 14 December 2012 - 16:12

No, but there are some existing libraries for that like http://nuclexframework.codeplex.com/ .



Click here to login or here to register to remove this ad, it's free!