Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

structs in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vinki - 25 Jul 2007 19:28 GMT
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.
Peter Bromberg [C# MVP] - 25 Jul 2007 19:56 GMT
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
Signature

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

> Hello Everyone,
>
[quoted text clipped - 30 lines]
>
> Thanks.
Vinki - 25 Jul 2007 20:20 GMT
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.

> Vinki,
> I fail to comprehend what an unsafe struct is going to buy for you in this
[quoted text clipped - 44 lines]
> >
> > Thanks.
Peter Bromberg [C# MVP] - 25 Jul 2007 20:52 GMT
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

> well, that method can do the manipulation, but with struct someone told it is
> much simpler. with structs, I can just
[quoted text clipped - 55 lines]
> > >
> > > Thanks.
Vinki - 26 Jul 2007 00:48 GMT
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.

> 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
[quoted text clipped - 67 lines]
> > > >
> > > > Thanks.
Göran Andersson - 26 Jul 2007 16:20 GMT
> Hello Everyone,
>
[quoted text clipped - 30 lines]
>
> 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;

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.