|
|
|
Probably easy answer but here goes.
How do I go about returning a variable name versus the value of the variable?
<br />
public struct Test {<br />
<br />
public string member;<br />
<br />
}<br />
<br />
Test obj = new Test();<br />
string member = "hello";<br />
<br />
obj.(member); <- obviously this is illegal but bare with me<br />
<br />
How could I have the variable name "member" (not the value "hello") be interpreted as if I manually wrote obj.member
|
|
|
|
|
I think you can get what you want using typeof.
|
|
|
|
|
No, typeof returns the System.Type of the specified type. For example, if you have a class named Foo,
class Foo { .. }
Type theFooType = typeof(Foo);
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
|
Plese help me to display a small dialog box(combobox ) wherein the user can specify the name of a dial-up connection from his system.
Vlada Nesic
|
|
|
|
|
For some reason ResxResourceWriter class is not availble for me in System.Resources namespace. However, I do see ResourceWriter and IResourceWriter. I'm using .Net 2003 on 2003 Server which has both .Net 1.0 and 1.1 installed. My primary framework is 1.1 . I dont think 1.0/1.1 should make difference since the class was available since v1.0.
Any suggestions?
Thanks,
- Malhar
|
|
|
|
|
This may sound like a stupid question, but is your project something other than a Windows Forms app?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
No such thing as a stupid qstn. But you're correct. My application is a Library (DLL) project which will be used by an used as a reference by UI (Windows Forms) project.
|
|
|
|
|
OK. You're probably missing the reference to System.Windows.Forms.dll.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
You know I said "there is no such thing as a stupid question". I was wrong. My initial question was really stupid!! .
I added "using System.Resources" but overlooked that it belongs in the System.Windows.Forms.Dll assembly. I was expecting that the Resources related stuff should be common and not be part of Win forms!!
Anyways, thanks much for your help.
- Malhar
|
|
|
|
|
Hi everybody!
I'm currently working on a class that has some public properties of type double . For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0
public double Alpha
{
set
{
if (value > 0)
this.alpha = value;
}
}
but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an ArgumentOutOfRangeException there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields?
Thanks in advance!
www.troschuetz.de
|
|
|
|
|
You can add a bool variable which indicate if the value has been assigned.
Like this:
<br />
public double Alpha<br />
{<br />
set<br />
{<br />
if (value > 0){<br />
assigned = true;<br />
this.alpha = value;<br />
}<br />
}<br />
}<br />
But im not sure i got your question!
I hope it helps..
VentoEngine corp.
Program your life ^^
|
|
|
|
|
This is a design decision that depends on many factors. However, generally speaking, if it were me and this variable was exposed to the user via the UI, I would put my "feedback" mechanism directly into the UI rather than putting it into the class itself.
That is, if the user defined this value in a textbox, for example, I would write the textbox in such a way that it simply would not allow the user to enter an incorrect value. I would then put something into the help file to explain the behavior. I think that is better than cluttering up your classes with a lot of exception crap and the UI with pop up warning boxes etc.
|
|
|
|
|
At first thanks for the answer.
The class I'm writing is part of a class library, so the variable may be exposed to the user via GUI but didn't necessarily have to. So I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:
public double Alpha
{
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");
this.alpha = value;
}
}
public bool IsValidAlpha(double value)
{
return value > 0;
}
}
if (obj.IsValidAlpha(1.0))
obj.Alpha = 1.0;
else
...
www.troschuetz.de
-- modified at 5:41 Thursday 1st December, 2005
|
|
|
|
|
One solution indeed could be to throw an exception when an invalid value is entered.
But you should capture this error in the user interface before the data is entered into the model you are using.
The exception is only for the other developers that use your model in their applications.
WM.
What about weapons of mass-construction?
|
|
|
|
|
At first thanks for the answer.
The class I'm writing is part of a class library, so I don't write a GUI myself. I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:
public double Alpha
{
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");
this.alpha = value;
}
}
public bool IsValidAlpha(double value)
{
return value > 0;
}
}
if (obj.IsValidAlpha(1.0))
obj.Alpha = 1.0;
else
...
www.troschuetz.de
|
|
|
|
|
You should be calling IsValidAlpha from within the setter instead of duplicating the code.
|
|
|
|
|
|
Hi:
I want to give an arbitrary number of arguments/parameters to a method and have it modify the arguments. C# says I cannot combine params and ref in the method decleration.
Please could someone suggest what I should do?
Thanks, Sam
|
|
|
|
|
Throw the objects you want to pass inside an array or ArrayList (or List<> if you're using .NET 2.0). Pass the array or list as an argument, where you can then modify the elements of the array/list.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
How can I read a specific bit or series of bits (as a bit array) from a file. I can use a file stream and binary reader to get specific bytes but I do not know how to easily convert this into a bit array (at least not very efficiently).
Jim
|
|
|
|
|
To turn a byte into bits, bool is the most obvious choice ( the only one I can think of with 2 states ) and you & the value with 0x1, 0x2, 0x4, 0x8, ox10, etc to strip the bits.
I reckon this is what you've thought of, but I don't know of any more efficient way, excepting that I'd be more inclined to do this when I need the value, and not store it as bits in memory.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I still wonder if there's a quicker way - but speed is not essential for what I am doing - reading mp3 header information.
Jim
|
|
|
|