[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use of unassigned local variable

Posted on 2009-02-19
16
Medium Priority
?
1,233 Views
Last Modified: 2013-12-17
I am trying to assign a variable a value from a table and I get this error message on the value from the table.  What would create this error message.
if (type == "PATIENT")
                            {
                                
 
 
                                // Lookup Patient in MPI Table and obtain MPI of Patient
                                var patientlookup = from MPIREC in db.MPIs
                                                    where recordMPI.MPI1 == MyGlobalVars.initGuid &&
                                                    recordMPI.NPI == MyGlobalVars.docnpi
                                                    select new { MPIREC };
 
 
                                if (patientlookup.Count() == 0)
                                {
                                    // No Record found in NPI - Must be new patient
                                }
                                else
                                {
                                    // Found patient get MPI 
                                    Guid patientmpi;
                                    patientmpi = recordMPI.MPI1;-----squiggly under recordMPI-----
 
                                }
 
                                // Lookup Patient in Patient table based on MPI
                                var patientdemolookup = from PATIENTDEMO in db.PATIENTs
                                                        where recordPatient.MPI == MyGlobalVars.initGuid ;-----squiggly under  recordPatient-------------------
                                                    select new { PATIENTDEMO };

Open in new window

0
Comment
Question by:kwh3856
  • 7
  • 6
  • 3
16 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23682422
Hi Kenny;

the variable patientmpi has not been defined in the program.

DataType patientmpi;

Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23682544
Hi Kenny;

Remember if patientmpi is going to be used outside of the if statement define it as shown below. I looked at the MPI db and did not see a record MPI1 so cannot tell you what type to place before patientmpi in the code snippet below.

private static void UpdateTable(DataClasses1DataContext db, Object record)
{
   
    MPI recordMPI;
    PATIENT recordPatient;
    DataType patientmpi;

Fernando
0
 

Author Comment

by:kwh3856
ID: 23682713
Fernando,
I am confused.  I thought I had defined patientmpi right above the line with the error.
 
if (patientlookup.Count() == 0)
                                {
                                    // No Record found in NPI - Must be new patient
                                }
                                else
                                {
                                    // Found patient get MPI
                                    Guid patientmpi;-----------------------------------------------------------------------patientmpi defined as GUID-----------
                                    patientmpi = recordMPI.MPI1;-----squiggly under recordMPI-----

                                }
 
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 23682838
Its complaining because you are trying to a straight assignment to a Guid type that does not have an initial value.
Depending on how you are storing the guid value in your database, you probably want something more like:

     Guid patientmpi = new Guid(recordMIP.MPI1);

0
 

Author Comment

by:kwh3856
ID: 23682915
Carl,
When I tried that I got these two error messages
The best overloaded method match for 'System.Guid.Guid(byte[])' has some invalid arguments
Argument '1': cannot convert from 'System.Guid' to 'byte[]'
Thanks
Kenny

 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 23682946
What is the data type of the underlying database column?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23682991
Your right Kenny, I missed that. carl_tawn post is correct but do not define it in the if statement because it looks as you will be using it outside of the if code block and it will not be available when you need it. You need to define it as I showed in the last post, like this:

private static void UpdateTable(DataClasses1DataContext db, Object record)
{
   
    MPI recordMPI;
    PATIENT recordPatient;
    DataType patientmpi;

Now depending on the data type of MPI1 in the MPI table change DataType in code above to that type. so if the data type in the db is Guid then it should look like this:

private static void UpdateTable(DataClasses1DataContext db, Object record)
{
   
    MPI recordMPI;
    PATIENT recordPatient;
    Guid patientmpi;

Fernando
0
 

Author Comment

by:kwh3856
ID: 23682992
In the database, it is type unique.
Thanks
Kenny
 
0
 

Author Comment

by:kwh3856
ID: 23683199
Fernando,
I have done that and now I am back to the same error that I started with.
It seems the error has more to do with recordMPI than the patientmpi variable.  It is like it does not understand that I am trying to pull the value out of the recordMPI table.  When I type in recordMPI.   intellisense definately shows me the available fields such as MPI.  Yet the error message seems to say it does not know it is a field.  What I am not understanding here?
Thanks
Kenny
 
 
if (patientlookup.Count() == 0)
                                {
                                    // No Record found in NPI - Must be new patient
                                }
                                else
                                {
                                    // Found patient get MPI
                                    Guid patientmpi;
                                    patientmpi = recordMPI.MPI1;-----squiggly under recordMPI-----

                                }
 
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 23683263
It's the fact that you are trying to assign to an uninitialized Guid that it is complaining about. You to use one of the constructors of the Guid type to convert your database value into something recognisable.

I haven't done much with Linq so i'm not sure if there will be a ToString() method or not. But you basically want to get your Db value into a string and pass that to the guid constructor:

    Guid patientmpi;
    patientmpi = new Guid(recordMPI.MPI1.ToString());      // may need tweaking a little
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23683361
Kenny;

A uniqueidentifier is a 16 byte Guid value then the following should work. Remember to pull the definition of patientmpi out of the if statement if patientmpi will be used outside if the if code block.

I am leaving now for a meeting I have and do not know when I will return, sorry.

if (patientlookup.Count() == 0)
{
    // No Record found in NPI - Must be new patient
}
else
{
    // Found patient get MPI
    Guid patientmpi;
    patientmpi = new Guid(recordMPI.MPI1);
}

Fernando
0
 

Author Comment

by:kwh3856
ID: 23683381
Carl,
I still got the same error message with that line.
Thanks
Kenny
 
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23688995
Hi Kenny;

I just tried this code and it worked fine. Because MPI1 is a uniqueidentifier / GUID it does not need to be cast or converted to a Guid.

// Define the variable
Guid patientmpi;

if (patientlookup.Count() == 0)
{
    // No Record found in NPI - Must be new patient
}
else
{
    // Found patient get MPI
    patientmpi = recordMPI.MPI1;
}

Fernando
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 23693378
Hi Kenny;

Change the top of the function UpdateTable from this:

        private static void UpdateTable(DataClasses1DataContext db, Object record)
        {

            MPI recordMPI;                      // You need to define the variables here
            PATIENT recordPatient;        // You need to define the variables here

To This:

        private static void UpdateTable(DataClasses1DataContext db, Object record)
        {

            MPI recordMPI = null;                      // You need to define the variables here
            PATIENT recordPatient = null;              // You need to define the variables here

It is complaining about that the variable were not initialize before being used. This should get you through this issue.

Fernando
0
 

Author Closing Comment

by:kwh3856
ID: 31548793
Fernando,
Thank you very much.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 23693689
Not a problem, always glad to help.  ;=)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question