structs in C#

G

Guest

Hello Everyone,

I have a huge string and I want to get just part of the string and pass it
in my stored proc. I am trying to do it through a structure. I am not sure if
this is the best way and right way to do it.
Below is the sample code


String x = "This is a test. Test done by xyz";

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct member
{
public fixed char region[5];
public fixed char lastName[10];

}


I want to do something like this that if I call the struct, it should go and
get me the region from the string.
The region is 5 characters long and pass that region as a parameter to the
stored proc.

The above string is a huge string. i am just giving a sample. I just have to
get the parts of the string
and pass them as parameter to the stored proc. Someone suggested me that
structure will be the best appraoch for this
but since it is a unsafe struct. I am little bit reluctant to use it. I am
wondering if someone can give me a better appraoch or f this the best
appraoch then how can use it.


command.Parameters.Add(new OracleParameter("mrnprefix",
OracleType.VarChar)).Value = region from a structure

Thanks.
 
G

Guest

Vinki,
I fail to comprehend what an unsafe struct is going to buy for you in this
kind of situation. What I read you saying is that you have a big string that
you need to manipulate and use portions to create OracleParameters for a SQL
operation.

Is there any reason you just can't have a method into which you pass your
big string and it does the manipulation (whatever it may be) and creates your
parameters?

Keep it simple.

Peter
 
G

Guest

well, that method can do the manipulation, but with struct someone told it is
much simpler. with structs, I can just
say

member myMember; // creating an instance for an struct


command.Parameters.Add(new OracleParameter("Region",
OracleType.VarChar)).Value = myMember.Region;

and this will do the parsing for me and will always give me region.



Peter Bromberg said:
Vinki,
I fail to comprehend what an unsafe struct is going to buy for you in this
kind of situation. What I read you saying is that you have a big string that
you need to manipulate and use portions to create OracleParameters for a SQL
operation.

Is there any reason you just can't have a method into which you pass your
big string and it does the manipulation (whatever it may be) and creates your
parameters?

Keep it simple.

Peter
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com



Vinki said:
Hello Everyone,

I have a huge string and I want to get just part of the string and pass it
in my stored proc. I am trying to do it through a structure. I am not sure if
this is the best way and right way to do it.
Below is the sample code


String x = "This is a test. Test done by xyz";

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct member
{
public fixed char region[5];
public fixed char lastName[10];

}


I want to do something like this that if I call the struct, it should go and
get me the region from the string.
The region is 5 characters long and pass that region as a parameter to the
stored proc.

The above string is a huge string. i am just giving a sample. I just have to
get the parts of the string
and pass them as parameter to the stored proc. Someone suggested me that
structure will be the best appraoch for this
but since it is a unsafe struct. I am little bit reluctant to use it. I am
wondering if someone can give me a better appraoch or f this the best
appraoch then how can use it.


command.Parameters.Add(new OracleParameter("mrnprefix",
OracleType.VarChar)).Value = region from a structure

Thanks.
 
G

Guest

Does "member myMember;" create an instance of your struct? Where is the big
string coming from? Really, man, you can do it any way you want; you can
also spend an inordinate amount of time stubbornly trying to make some custom
solution work. If you are posting to the group with this question it would
seem that you are having difficulty; the solution I proposed is a very easy
way. :)
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com



Vinki said:
well, that method can do the manipulation, but with struct someone told it is
much simpler. with structs, I can just
say

member myMember; // creating an instance for an struct


command.Parameters.Add(new OracleParameter("Region",
OracleType.VarChar)).Value = myMember.Region;

and this will do the parsing for me and will always give me region.



Peter Bromberg said:
Vinki,
I fail to comprehend what an unsafe struct is going to buy for you in this
kind of situation. What I read you saying is that you have a big string that
you need to manipulate and use portions to create OracleParameters for a SQL
operation.

Is there any reason you just can't have a method into which you pass your
big string and it does the manipulation (whatever it may be) and creates your
parameters?

Keep it simple.

Peter
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com



Vinki said:
Hello Everyone,

I have a huge string and I want to get just part of the string and pass it
in my stored proc. I am trying to do it through a structure. I am not sure if
this is the best way and right way to do it.
Below is the sample code


String x = "This is a test. Test done by xyz";

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct member
{
public fixed char region[5];
public fixed char lastName[10];

}


I want to do something like this that if I call the struct, it should go and
get me the region from the string.
The region is 5 characters long and pass that region as a parameter to the
stored proc.

The above string is a huge string. i am just giving a sample. I just have to
get the parts of the string
and pass them as parameter to the stored proc. Someone suggested me that
structure will be the best appraoch for this
but since it is a unsafe struct. I am little bit reluctant to use it. I am
wondering if someone can give me a better appraoch or f this the best
appraoch then how can use it.


command.Parameters.Add(new OracleParameter("mrnprefix",
OracleType.VarChar)).Value = region from a structure

Thanks.
 
G

Guest

Hi Peter,

You are right. This was just a suggestion froma co worker and I thought
of using, but I will go ahead and create a method that will do the
manipulation.

Thanks.

Peter Bromberg said:
Does "member myMember;" create an instance of your struct? Where is the big
string coming from? Really, man, you can do it any way you want; you can
also spend an inordinate amount of time stubbornly trying to make some custom
solution work. If you are posting to the group with this question it would
seem that you are having difficulty; the solution I proposed is a very easy
way. :)
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com



Vinki said:
well, that method can do the manipulation, but with struct someone told it is
much simpler. with structs, I can just
say

member myMember; // creating an instance for an struct


command.Parameters.Add(new OracleParameter("Region",
OracleType.VarChar)).Value = myMember.Region;

and this will do the parsing for me and will always give me region.



Peter Bromberg said:
Vinki,
I fail to comprehend what an unsafe struct is going to buy for you in this
kind of situation. What I read you saying is that you have a big string that
you need to manipulate and use portions to create OracleParameters for a SQL
operation.

Is there any reason you just can't have a method into which you pass your
big string and it does the manipulation (whatever it may be) and creates your
parameters?

Keep it simple.

Peter
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com



:

Hello Everyone,

I have a huge string and I want to get just part of the string and pass it
in my stored proc. I am trying to do it through a structure. I am not sure if
this is the best way and right way to do it.
Below is the sample code


String x = "This is a test. Test done by xyz";

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct member
{
public fixed char region[5];
public fixed char lastName[10];

}


I want to do something like this that if I call the struct, it should go and
get me the region from the string.
The region is 5 characters long and pass that region as a parameter to the
stored proc.

The above string is a huge string. i am just giving a sample. I just have to
get the parts of the string
and pass them as parameter to the stored proc. Someone suggested me that
structure will be the best appraoch for this
but since it is a unsafe struct. I am little bit reluctant to use it. I am
wondering if someone can give me a better appraoch or f this the best
appraoch then how can use it.


command.Parameters.Add(new OracleParameter("mrnprefix",
OracleType.VarChar)).Value = region from a structure

Thanks.
 
G

Guest

Vinki said:
Hello Everyone,

I have a huge string and I want to get just part of the string and pass it
in my stored proc. I am trying to do it through a structure. I am not sure if
this is the best way and right way to do it.
Below is the sample code


String x = "This is a test. Test done by xyz";

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public unsafe struct member
{
public fixed char region[5];
public fixed char lastName[10];

}


I want to do something like this that if I call the struct, it should go and
get me the region from the string.
The region is 5 characters long and pass that region as a parameter to the
stored proc.

The above string is a huge string. i am just giving a sample. I just have to
get the parts of the string
and pass them as parameter to the stored proc. Someone suggested me that
structure will be the best appraoch for this
but since it is a unsafe struct. I am little bit reluctant to use it. I am
wondering if someone can give me a better appraoch or f this the best
appraoch then how can use it.


command.Parameters.Add(new OracleParameter("mrnprefix",
OracleType.VarChar)).Value = region from a structure

Thanks.

That's the most complicated way that I have ever seen of getting a part
of a string.

Just use the Substring method:

string region = x.Substring(0, 5);
string lastName = x.Substring(5, 10);

This will also protect you from reading unexistent data. If the string
for some reason is shorter than you expect, you will get an exception
instead of reading random data outside of the string.


If you want to access the substrings as properties, you can easily
create a structure that does thas:

public structure Member {

private string _data;

public Member(string data) {
_data = data;
}

public string Region { get { return _data.Substring(0, 5); } }
public string LastName { get { return _data.Substring(5, 10); } }

}

Now you can create a Member structure and get the values from it:

Member member = new Member(x);

command.Parameters.Add(new OracleParameter("mrnprefix",
OracleType.VarChar)).Value = member.Region;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top