473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

structs in C#

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.
Jul 25 '07 #1
5 1422
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" wrote:
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.
Jul 25 '07 #2
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 [C# MVP]" wrote:
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" wrote:
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.
Jul 25 '07 #3
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" wrote:
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 [C# MVP]" wrote:
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" wrote:
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.
Jul 25 '07 #4
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 [C# MVP]" wrote:
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" wrote:
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 [C# MVP]" wrote:
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" wrote:
>
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.
Jul 25 '07 #5
Vinki wrote:
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;
--
Göran Andersson
_____
http://www.guffa.com
Jul 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to...
6
by: James Pascoe | last post by:
Dear All, Apologies if this is OT. I have a C program which processes an arbitrary number of structs that are stored in a hash table. (The nature of the processing and the layout of the...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
10
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.