|
i want to connect network pc through IP address. how it is possible. i am using namespace.
System.Net.Sockets;
System.Net
|
|
|
|
|
Duplicate Post . Please Ignore
|
|
|
|
|
i am trying to write a method that checks whether an object can be cast to a specific type:
that is, something like this:
bool CheckConfigVariable(object variable, Type expectedType) {
if (variable is expectedType) {
return true;
}
return false;
}
but that doesn't seem to work. I've also tried:
object hello;
hello = (object)variable;
if (typeof(hello) == Type.GetTypeCode(typething)) {
return true;
}
return false;
}
which sort of works, but it can't find the hello variable in the typeof function. How would I go about checking whether the object is a certain type from what is provided in the parameters of the called method?
Mike
|
|
|
|
|
Please try with the code given below.
bool CheckConfigVariable(object variable, Type expectedType)
{
if (variable.GetType() == expectedType)
{
return true;
}
return false;
}
Software - Bundle of bugs covered with features.
|
|
|
|
|
typeof takes the type, I thought, as in typeof(int). I think you need hello.GetType()
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
This isn't exactly doing what i thought it was doing sorry.
variable.GetType() == expectedType is checking whether Object is equal to Type blah.
Its always returning false. I just want it to return if it is compatible with casting it to another object, not if it is the same type.
|
|
|
|
|
OK, perhaps generics will help. Because what you need is access to the type to call 'is' as you were trying to do. Passing a Type doesn't sound like it worked, but if the type is specified and your generic method says something like
return (myvar is T);
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Christian Graus wrote: return (myvar is T);
where T is my parameter of type?
bool CheckConfigVariable(object variable, Type expectedType) {
return (variable is expectedType);
}
i get the error: The type or namespace name 'expectedType' could not be found (are you missing a using directive or an assembly reference?) (CS0246)
|
|
|
|
|
Yeah, but you write a generic method instead.
static bool CheckType<T>(object o)
{
return (o is T);
}
Then you call it like this:
DateTime dt = DateTime.Now;
bool b = CheckType<int>(dt);
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
wow, I didn't know how that worked. Now I'll have to go and do some more indepth reading on Generics.
Just another question on comparing types. How do enums fit into the picture? Is enum a type?
Can I compare a string to an enum (using the method above (using is keyword)), or one of the values in the enumto see if it can be converted?
|
|
|
|
|
You use Enum.Parse to convert a string to an enum. An enum is a type.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thank you so much for your help, it is greatly appreciated.
|
|
|
|
|
Glad to help
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
|
private static bool
F
(
object o
,
System.Type t
)
{
if ( o == null )
{
throw ( new System.ArgumentNullException() ) ;
}
return ( t.IsAssignableFrom ( o.GetType() ) ) ;
}
And may I humbly suggest my TypeTransmogrifier[^] ?
|
|
|
|
|
protected void bn_delete_Click(object sender, EventArgs e)
{
bn_del.Attributes("onclick") = "return confirm('Are you sure you wish to delete these records?');";
GridViewRow row;
foreach (row in GridView1.Rows) {
CheckBox idcb = row.FindControl("CheckBox1");
TextBox id = row.FindControl("id");
if (idcb.Checked) {
DataAccessLayer dal = new DataAccessLayer();
dal.Connect();
dal.deleteDB("DELETE FROM StaffID Where StaffIDID = '" + id.Text + "'");
dal.Close();
getStaff();
}
error message: type and identifier are both required in a foreach
statement
|
|
|
|
|
twistz wrote: foreach (row in GridView1.Rows)
I think you need to do
foreach (GridViewRow row in GridView1.Rows)
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
it states "the name "bn_del" does not exist in the current context",
"a local variable named "row" cannot be declared in this scope because it would give a different meaning to "row", which is already used in a "parent or current" scope to denote something else"
|
|
|
|
|
twistz wrote: the name "bn_del" does not exist in the current context",
Well, that's a whole different problem. What makes you think it should exist ?
twistz wrote: "a local variable named "row" cannot be declared in this scope because it would give a different meaning to "row", which is already used in a "parent or current" scope to denote something else"
That means you kept the old line in that defined that variable. You need to delete it. The error means what it says, you are defining it twice.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
twistz wrote: foreach (row in GridView1.Rows) {
CheckBox idcb = row.FindControl("CheckBox1");
TextBox id = row.FindControl("id");
if (idcb.Checked) {
DataAccessLayer dal = new DataAccessLayer();
dal.Connect();
dal.deleteDB("DELETE FROM StaffID Where StaffIDID = '" + id.Text + "'");
dal.Close();
getStaff();
}
Try it :
foreach (var row in GridView1.Rows)
{
CheckBox idcb = row.FindControl("CheckBox1");
TextBox id = row.FindControl("id");
if (idcb.Checked)
{
DataAccessLayer dal = new DataAccessLayer();
dal.Connect();
dal.deleteDB("DELETE FROM StaffID Where StaffIDID = '" + id.Text + "'");
dal.Close();
getStaff();
}
}
Freshman
|
|
|
|
|
That's var abuse.
|
|
|
|
|
Whatever happened to the 'use strongly typed variable' rule? May as well define eveything as an object
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Ashfield wrote: Whatever happened to the 'use strongly typed variable' rule? May as well define eveything as an object
The variable is actually stronly typed, as the compiler can determine what type it has to be.
However, the code is not well written, as it's not obvious what the type of the variable will be. You can't read directly what the type is, you have to know what type the enumerator of the Rows collection returns. As there is two layers of indirection to follow, the code gets less readable. Also, the code gets more prone to accidental changes, as the declaration depends on other declarations.
If the intention was that the variable should get whatever type the enumerator returns it would be ok, but when the motivation is only laziness, it's not a coding style that can be recommended.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I has a C++ DLL, it has follow code:
typedef struct _SC_CONTEXT{
long hPort;
long dwCardType;
// BYTE params[24];
} SC_CONTEXT;
extern int __stdcall TestFunc(SC_CONTEXT* p)
{
char buf[256];
sprintf(buf, "hPort is : %d, dwCardType is : %d.", p.hPort, p.dwCardType);
// sprintf(buf, "hPort is : %d, dwCardType is : %d, params[0] is : %d.", p->hPort, p->dwCardType, p->params[0]);
MessageBoxA(NULL, buf, "caption1", MB_OK);
return 0;
}
I wrote C# code like :
[StructLayout(LayoutKind.Sequential)]
public struct _SC_CONTEXT
{
public long hPort;
public long dwCardType;
}
namespace MyName
{
class MyClass
{
[DllImport("testDll.dll")]
public static extern int TestFunc(ref _SC_CONTEXT11 a);
}
static void Main(string[] args)
{
unsafe
{
_SC_CONTEXT a;// = new _SC_CONTEXT();
a.hPort = 67;
a.dwCardType = 66;
// a.paras[0] = 255;
TestFunc(ref a);
}
......
}
}
the question is, function TestFunc can not get _SC_CONTEXT::dwCardType's value. In the messagebox, content always is "hPort is : 67, dwCardType is : 0.". I want to know why, and how to correct this bug. As you see, I comment an array member of the struct, if not comment it, how to use struct with array member.
|
|
|
|
|
I've spotted at least one bug immediately: .NET long is 64bit, but in C++ it's 32bit! So you should declare your struct with int instead:
samfromcn wrote: public struct _SC_CONTEXT
{
public long hPort;
public long dwCardType;
}
public struct _SC_CONTEXT
{
public int hPort;
public int dwCardType;
}
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|