Click here to Skip to main content
15,922,145 members
Home / Discussions / C#
   

C#

 
GeneralPreProcessMessage Pin
24-May-02 4:30
suss24-May-02 4:30 
GeneralRe: PreProcessMessage Pin
Joshua Nussbaum24-May-02 6:27
Joshua Nussbaum24-May-02 6:27 
GeneralCrystal Reports Not Working Pin
Gavin_Mannion24-May-02 3:55
Gavin_Mannion24-May-02 3:55 
GeneralMDIChild Activated() event not firing...... Pin
Pranoti24-May-02 1:45
Pranoti24-May-02 1:45 
GeneralRe: MDIChild Activated() event not firing...... Pin
Rocky Moore24-May-02 19:17
Rocky Moore24-May-02 19:17 
GeneralSockets/Objects/Serializable Pin
Paddy24-May-02 1:30
Paddy24-May-02 1:30 
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson24-May-02 7:54
James T. Johnson24-May-02 7:54 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo24-May-02 19:39
tmagoo24-May-02 19:39 
I am having sort of the same problems as you are. Instead I am trying to make class variables equal to the bytes returned from the socket. I sort of understand what James T. Johnson is giving you advise on; but, I don't know how to make a custom object in C#. Since I am using class variables instead of objects, I am leaving you this test code to show you how I convert the byte types to the variables in a class. This example requires the knowledge of byte masking and the use of Reflection. I am hoping to get some feedback plus maybe show you something new dealing with Reflection.

using System;
using System.Reflection;

namespace ConvertTypes
{
///
/// Summary description for ConvertTypes.
///

///

class ConvertClass
{
public int x;
public int y;
public bool z;

public ConvertClass(int i, int j, bool k)
{
x = i;
y = j;
z = k;
}

// Set the converted value
public void SetConvertClass(int i, string sName)
{
if(sName == "x")
x = i;
else
y = i;
}
}


class clsConvertTypes
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
ConvertClass mCls = new ConvertClass(0,0,true);

//Declare 8 Bytes for 2 int's
byte [] bTest = new Byte[8];

// get a Type Object representing ConvertClass
Type t = typeof(ConvertClass);

// get the fields from the Type object (ie int x, int y)
FieldInfo [] fi = t.GetFields();

// Start at index 0 which represents the x field
int iIndex = 0;

//Build the x value as 500000 and the y value as 400000
bTest[0] = 0x00; // 0x0007A120 = 500000
bTest[1] = 0x07;
bTest[2] = 0xA1;
bTest[3] = 0x20;
bTest[4] = 0x00; // 0x00061A80 = 400000
bTest[5] = 0x06;
bTest[6] = 0x1A;
bTest[7] = 0x80;

foreach(FieldInfo FI in fi)
{
if(FI.FieldType == typeof(int))
{
// Declare a temporary integer variable
int iTemp;

// Mask the byte array into a single integer
iTemp = (int)((bTest[iIndex]<<24) | (bTest[iIndex+1] << 16) |
(bTest[iIndex+2] << 8) | bTest[iIndex+3]);

// increment the index by 4 bytes since it is a int
iIndex = iIndex + 4;

// Declare an object which will be used to pass
// parameters to the SetConvertClass method
object[] targs = new Object[2];
targs[0] = iTemp; //First argument
targs[1] = FI.Name; //Second argument (x or y)

// Get all methods associated with ConvertClass
MethodInfo[] mi = t.GetMethods();

foreach(MethodInfo m in mi)
{
//Compare to the name of the method
if(m.Name.CompareTo("SetConvertClass") == 0)
{
Console.WriteLine("Setting the {0} value to {1}",
FI.Name,iTemp);
//Invoke the method using Reflection
m.Invoke(mCls,targs);
}
}
}
else
Console.WriteLine ("Don't do anything with the {0} field",
FI.Name);

}

Console.WriteLine("x is {0} y is {1}",mCls.x,mCls.y);
}

}

}

The Long Way,

Tom McDaniel
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson25-May-02 4:17
James T. Johnson25-May-02 4:17 
GeneralRe: Sockets/Objects/Serializable Pin
Paddy25-May-02 4:39
Paddy25-May-02 4:39 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo26-May-02 3:35
tmagoo26-May-02 3:35 
GeneralRe: Sockets/Objects/Serializable Pin
Paddy27-May-02 2:37
Paddy27-May-02 2:37 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo29-May-02 10:59
tmagoo29-May-02 10:59 
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson29-May-02 18:02
James T. Johnson29-May-02 18:02 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo30-May-02 3:14
tmagoo30-May-02 3:14 
GeneralPlatform Interop - Structures and Arrays Pin
Mikko Puonti24-May-02 0:09
Mikko Puonti24-May-02 0:09 
GeneralXmlDocument with DTD driving me insane! Pin
Daniel Turini23-May-02 10:21
Daniel Turini23-May-02 10:21 
QuestionWindows services???? Pin
Rickard Andersson2023-May-02 4:37
Rickard Andersson2023-May-02 4:37 
AnswerRe: Windows services???? Pin
Nish Nishant23-May-02 17:06
sitebuilderNish Nishant23-May-02 17:06 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 20:48
Rickard Andersson2023-May-02 20:48 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 20:52
sitebuilderNish Nishant23-May-02 20:52 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 21:20
Rickard Andersson2023-May-02 21:20 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 21:20
sitebuilderNish Nishant23-May-02 21:20 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 22:17
Rickard Andersson2023-May-02 22:17 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 22:17
sitebuilderNish Nishant23-May-02 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.